symfony3

Declaring Entities

Declaring a Symfony Entity as YAML

  • AppBundle/Entity/Person.php

    id; } /** * Set name * * @param string $name * * @return Person */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set age * * @param integer $age * * @return Person */ public function setAge($age) { $this->age = $age; return $this; } /** * Get age * * @return int */ public function getAge() { return $this->age; } }
  • AppBundle/Resources/config/doctrine/Person.orm.yml

    AppBundle\Entity\Person: type: entity repositoryClass: AppBundle\Repository\PersonRepository table: persons id: id: type: integer id: true generator: strategy: AUTO fields: name: type: string length: 20 nullable: false column: Name unique: true age: type: integer nullable: false column: Age unique: false lifecycleCallbacks: { }

  • Create the table

    php bin/console doctrine:schema:update —force

Or use the recommended way (assuming you already have doctrine-migrations-bundle installed in your project):

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
  • Create the entity automatically from the YAML

    php bin/console doctrine:generate:entities AppBundle

Declaring a Symfony Entity with annotations

  • AppBundle/Entity/Person.php

    id; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; return $this; } public function getAge() { return $this->age; } public function setAge($age) { $this->age = $age; return $this; } }
  • Generate the entity

    php bin/console doctrine:generate:entities AppBundle/Person

  • To dump the SQL statements to the screen

    php bin/console doctrine:schema:update —dump-sql

  • Create the table

    php bin/console doctrine:schema:update —force

Or use the recommended way (assuming you already have doctrine-migrations-bundle installed in your project):

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow