From e8eb547ff40b0ccb15b41cb35bbe8b862c52dc60 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 23 Mar 2018 13:55:53 -0400 Subject: [PATCH 1/2] updating the doctrine reverse engineering for 4.0 --- doctrine/reverse_engineering.rst | 119 ++++++------------------------- 1 file changed, 20 insertions(+), 99 deletions(-) diff --git a/doctrine/reverse_engineering.rst b/doctrine/reverse_engineering.rst index e0f147c5b58..6faf0ad6e5b 100644 --- a/doctrine/reverse_engineering.rst +++ b/doctrine/reverse_engineering.rst @@ -4,14 +4,6 @@ How to Generate Entities from an Existing Database ================================================== -.. caution:: - - The feature explained in this article doesn't work in modern Symfony - applications that have no bundles. The workaround is to temporarily create - a bundle. See `doctrine/doctrine#729`_ for details. Moreover, this feature - to generate entities from existing databases will be completely removed in - the next Doctrine version. - When starting work on a brand new project that uses a database, two different situations comes naturally. In most cases, the database model is designed and built from scratch. Sometimes, however, you'll start with an existing and @@ -64,112 +56,41 @@ table fields. .. code-block:: terminal - $ php bin/console doctrine:mapping:import --force AppBundle xml + $ php bin/console doctrine:mapping:import App annotation --path=src/Entity This command line tool asks Doctrine to introspect the database and generate -the XML metadata files under the ``src/Resources/config/doctrine`` -folder of your bundle. This generates two files: ``BlogPost.orm.xml`` and -``BlogComment.orm.xml``. +new PHP classes with annotation metadata into ``src/Entity``. This generates two +files: ``BlogPost.php`` and ``BlogComment.php``. .. tip:: - It's also possible to generate the metadata files in YAML format by changing - the last argument to ``yml``. + It's also possible to generate the metadata files into XML or YAML: + + .. code-block:: terminal -The generated ``BlogPost.orm.xml`` metadata file looks as follows: + $ php bin/console doctrine:mapping:import App xml --path=config/doctrine -.. code-block:: xml +Generating the Getters & Setters or PHP Classes +----------------------------------------------- - - - - - - - - - - - +The generated PHP classes now have properties and annotation metadata, but they +do *not* have any getter or setter methods. If you generated XML or YAML metadata, +you don't even have the PHP classes! -Once the metadata files are generated, you can ask Doctrine to build related -entity classes by executing the following command. +To generate the missing getter/setter methods (or to *create* the classes if neceesary), +run: .. code-block:: terminal - // generates entity classes with annotation mappings - $ php bin/console doctrine:mapping:convert annotation ./src - -.. caution:: - - If you want to use annotations, you must remove the XML (or YAML) files - after running this command. This is necessary as - :ref:`it is not possible to mix mapping configuration formats ` - -For example, the newly created ``BlogComment`` entity class looks as follow:: - - // src/Entity/BlogComment.php - namespace App\Entity; - - use Doctrine\ORM\Mapping as ORM; - - /** - * @ORM\Table(name="blog_comment") - * @ORM\Entity - */ - class BlogComment - { - /** - * @var integer $id - * - * @ORM\Column(name="id", type="bigint") - * @ORM\Id - * @ORM\GeneratedValue(strategy="IDENTITY") - */ - private $id; - - /** - * @var string $author - * - * @ORM\Column(name="author", type="string", length=100, nullable=false) - */ - private $author; - - /** - * @var text $content - * - * @ORM\Column(name="content", type="text", nullable=false) - */ - private $content; - - /** - * @var datetime $createdAt - * - * @ORM\Column(name="created_at", type="datetime", nullable=false) - */ - private $createdAt; - - /** - * @var BlogPost - * - * @ORM\ManyToOne(targetEntity="BlogPost") - * @ORM\JoinColumn(name="post_id", referencedColumnName="id") - */ - private $post; - } - -As you can see, Doctrine converts all table fields to pure private and annotated -class properties. The most impressive thing is that it also discovered the -relationship with the ``BlogPost`` entity class based on the foreign key constraint. -Consequently, you can find a private ``$post`` property mapped with a ``BlogPost`` -entity in the ``BlogComment`` entity class. + // generates getter/setter methods + $ php bin/console make:entity --regenerate App .. note:: - If you want to have a one-to-many relationship, you will need to add - it manually into the entity or to the generated XML or YAML files. - Add a section on the specific entities for one-to-many defining the - ``inversedBy`` and the ``mappedBy`` pieces. + If you want to have a OneToMany relationship, you will need to add + it manually into the entity (e.g. add a ``comments`` property to ``BlogPost``) + or to the generated XML or YAML files. Add a section on the specific entities + for one-to-many defining the ``inversedBy`` and the ``mappedBy`` pieces. The generated entities are now ready to be used. Have fun! From 61bdd260b772e93732e7cc801f3707de20cecba1 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sun, 25 Mar 2018 13:55:17 -0400 Subject: [PATCH 2/2] Updating arg: we need to pass the full namespace (with double slashes) --- doctrine/reverse_engineering.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doctrine/reverse_engineering.rst b/doctrine/reverse_engineering.rst index 6faf0ad6e5b..6c4d7b10612 100644 --- a/doctrine/reverse_engineering.rst +++ b/doctrine/reverse_engineering.rst @@ -56,7 +56,7 @@ table fields. .. code-block:: terminal - $ php bin/console doctrine:mapping:import App annotation --path=src/Entity + $ php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity This command line tool asks Doctrine to introspect the database and generate new PHP classes with annotation metadata into ``src/Entity``. This generates two @@ -68,7 +68,7 @@ files: ``BlogPost.php`` and ``BlogComment.php``. .. code-block:: terminal - $ php bin/console doctrine:mapping:import App xml --path=config/doctrine + $ php bin/console doctrine:mapping:import App\\Entity xml --path=config/doctrine Generating the Getters & Setters or PHP Classes -----------------------------------------------