Skip to content

Commit

Permalink
35.1.Setup: For Dependent Select Fields{Adding the First Select Field}
Browse files Browse the repository at this point in the history
We're going to tackle one of the most annoying things in Symfony's form system, and, I hope, make it as painless as possible... because the end result is pretty cool!
Log in as admin2@thespacebar.com, password engage and then go to /admin/article. Click to create a new article. Here's the goal: on this form, I want to add two new drop-down select elements: a location drop-down - so you can choose where in the galaxy you are - and a second dropdown with more specific location options depending on what you chose for the location. For example, if you select "Near a star" for your location, the next drop-down would update to be a list of stars. Or, if you select "The Solar System", the next drop-down will be a list of planets.

Adding the First Select Field
This is called a "dependent form field", and, unfortunately, it's one of the trickier things to do with the form system - which is exactly why we're talking about it! Let's add the first new field. Find your terminal and run

php bin/console make:entity
Modify the Article entity and create a new field called location. Make it a string field with yes to nullable in the database: the location will be optional. Now run:

php bin/console make:migration
and open the Migrations/ directory to check out that new file.
No surprises here, so let's go back and run it:

php bin/console doctrine:migrations:migrate
Perfect!
  • Loading branch information
petrero committed Feb 9, 2020
1 parent ca14913 commit fa3b41b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Article{
*/
private $author;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $location;

public function __construct(){
$this->comments = new ArrayCollection();
$this->tags = new ArrayCollection();
Expand Down Expand Up @@ -239,4 +244,14 @@ public function validate(ExecutionContextInterface $context, $payload){
->addViolation();
}
}

public function getLocation(): ?string{
return $this->location;
}

public function setLocation(?string $location): self{
$this->location = $location;

return $this;
}
}
31 changes: 31 additions & 0 deletions src/Migrations/Version20200209135332.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20200209135332 extends AbstractMigration {
public function getDescription() : string {
return '';
}

public function up(Schema $schema) : void {
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE article ADD location VARCHAR(255) DEFAULT NULL');
}

public function down(Schema $schema) : void {
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE article DROP location');
}
}

0 comments on commit fa3b41b

Please sign in to comment.