From 42ad9b7c72ffa1a6fd6c8d2fb5ee3b51a5a041d1 Mon Sep 17 00:00:00 2001 From: "Jonathan H. Wage" Date: Thu, 25 Feb 2010 22:52:23 -0500 Subject: [PATCH] [DoctrineBundle] Improvements for building entities and getting started --- .../Command/BuildDoctrineCommand.php | 21 ++++ .../Command/BuildEntitiesDoctrineCommand.php | 6 +- .../Command/InitEntityDoctrineCommand.php | 116 ++++++++++++++++++ src/Symfony/Framework/DoctrineBundle/README | 59 ++++++++- 4 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Framework/DoctrineBundle/Command/InitEntityDoctrineCommand.php diff --git a/src/Symfony/Framework/DoctrineBundle/Command/BuildDoctrineCommand.php b/src/Symfony/Framework/DoctrineBundle/Command/BuildDoctrineCommand.php index d2d31e6306..0e97e9a928 100644 --- a/src/Symfony/Framework/DoctrineBundle/Command/BuildDoctrineCommand.php +++ b/src/Symfony/Framework/DoctrineBundle/Command/BuildDoctrineCommand.php @@ -55,6 +55,27 @@ class BuildDoctrineCommand extends DoctrineCommand ->addOption('and-append', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, 'Load data fixtures and append to existing data') ->addOption('and-update-schema', null, null, 'Update schema after rebuilding all classes') ->addOption('connection', null, null, 'The connection to use.') + ->setHelp(' +The doctrine:build task builds your Doctrine development environment. + + php console doctrine:build --all + +The above command will re-build your entities and re-create your database schema. + +If you wanted to only update your schema instead of re-creating it you can run +the following: + + php console doctrine:build --entities --and-update-schema + +Now your entities are re-built and your database schema is up to date! + +You can also use the --and-load and --and-append to +load data fixtures after running another build option. + + php console doctrine:build --all --and-load + +The above will re-build everything and load all bundle data fixtures. +') ; } diff --git a/src/Symfony/Framework/DoctrineBundle/Command/BuildEntitiesDoctrineCommand.php b/src/Symfony/Framework/DoctrineBundle/Command/BuildEntitiesDoctrineCommand.php index 41533e1c60..7e49f2b2ce 100644 --- a/src/Symfony/Framework/DoctrineBundle/Command/BuildEntitiesDoctrineCommand.php +++ b/src/Symfony/Framework/DoctrineBundle/Command/BuildEntitiesDoctrineCommand.php @@ -56,10 +56,12 @@ class BuildEntitiesDoctrineCommand extends DoctrineCommand if (isset($bundleDirs[$namespace])) { - if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Entities')) + if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Resources/config/doctrine/metadata')) { $this->convertMapping($dir, $bundleDirs[$namespace].'/..'); - } else if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Resources/config/doctrine/metadata')) { + } + else if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Entities')) + { $this->convertMapping($dir, $bundleDirs[$namespace].'/..'); } } diff --git a/src/Symfony/Framework/DoctrineBundle/Command/InitEntityDoctrineCommand.php b/src/Symfony/Framework/DoctrineBundle/Command/InitEntityDoctrineCommand.php new file mode 100644 index 0000000000..cc8d3583bd --- /dev/null +++ b/src/Symfony/Framework/DoctrineBundle/Command/InitEntityDoctrineCommand.php @@ -0,0 +1,116 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * Initialize a new Doctrine entity inside a bundle. + * + * @package symfony + * @subpackage console + * @author Fabien Potencier + * @author Jonathan H. Wage + */ +class InitEntityDoctrineCommand extends DoctrineCommand +{ + /** + * @see Command + */ + protected function configure() + { + $this + ->setName('doctrine:init-entity') + ->setDescription('Initialize a new Doctrine entity inside a bundle.') + ->addOption('bundle', null, InputOption::PARAMETER_REQUIRED, 'The bundle to initialize the entity in.') + ->addOption('entity', null, InputOption::PARAMETER_REQUIRED, 'The entity class to initialize.') + ->setHelp(' +The doctrine:init-entity task initializes a new Doctrine entity inside a bundle: + + php console doctrine:init-entity --bundle="Bundle\MyCustomBundle" --entity="User\Group" + +The above would initialize a new entity in the following entity namespace Bundle\MyCustomBundle\Entities\User\Group. + +You can now build your entities and update your database schema: + + php console doctrine:build --entities --and-update-schema + +Now you have a new entity and your database has been updated. + ') + ; + } + + /** + * @see Command + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + if (!preg_match('/Bundle$/', $bundle = $input->getOption('bundle'))) + { + throw new \InvalidArgumentException('The bundle name must end with Bundle. Example: "Bundle\MySampleBundle".'); + } + + $dirs = $this->container->getKernelService()->getBundleDirs(); + + $tmp = str_replace('\\', '/', $bundle); + $namespace = str_replace('/', '\\', dirname($tmp)); + $bundle = basename($tmp); + + if (!isset($dirs[$namespace])) + { + throw new \InvalidArgumentException(sprintf('Unable to initialize the bundle entity (%s not defined).', $namespace)); + } + + $entity = $input->getOption('entity'); + $entityNamespace = $namespace.'\\'.$bundle.'\\Entities'; + $fullEntityClassName = $entityNamespace.'\\'.$entity; + $tmp = str_replace('\\', '/', $fullEntityClassName); + $tmp = str_replace('/', '\\', dirname($tmp)); + $className = basename($tmp); + + $extends = null; + $path = $dirs[$namespace].'/'.$bundle.'/Resources/config/doctrine/metadata/'.str_replace('\\', '.', $fullEntityClassName).'.dcm.xml'; + + $xml = sprintf(' + + + + + + + + + +', + $fullEntityClassName, + str_replace('\\', '_', strtolower($entity)) + ); + + if (!is_dir($dir = dirname($path))) + { + mkdir($dir, 0777, true); + } + + file_put_contents($path, $xml); + $this->runCommand('doctrine:build-entities'); + } +} \ No newline at end of file diff --git a/src/Symfony/Framework/DoctrineBundle/README b/src/Symfony/Framework/DoctrineBundle/README index 3c675cef44..eed79f92e1 100644 --- a/src/Symfony/Framework/DoctrineBundle/README +++ b/src/Symfony/Framework/DoctrineBundle/README @@ -59,7 +59,6 @@ If you want to enable the Doctrine 2 ORM you can do so with the following: doctrine.orm: default_entity_manager: default - metadata_driver: xml # xml, yml, annotation cache_driver: apc # array, apc, memcache, xcache entity_managers: default: @@ -157,6 +156,8 @@ file named **Entry.php** with some code like the following: The Doctrine 2 CLI is integrated with the Symfony 2 CLI so we have all the common commands we need to make working with Doctrine 2 just as easy and fast as before! +### Listing Available Doctrine Commands + $ php console list doctrine Available commands for the "doctrine" namespace: @@ -167,12 +168,45 @@ commands we need to make working with Doctrine 2 just as easy and fast as before :database-tool Create and drop the configured databases. :ensure-production-settings Verify that Doctrine is properly configured for a production environment. :generate-proxies Generates proxy classes for entity classes. + :import-mapping Import the initial mapping information for entities from an existing database. + :init-entity Initialize a new Doctrine entity inside a bundle. :load-data-fixtures Load data fixtures to your database. :run-dql Executes arbitrary DQL directly from the command line. :run-sql Executes arbitrary SQL from a file or directly from the command line. :schema-tool Processes the schema and either apply it directly on EntityManager or generate the SQL output. :version Displays the current installed Doctrine version. +### Schema Tool + +The schema tool in Doctrine 2 allows you to easily drop and your create your +database schemas for your mapping information. + +You can easily create your initial schema from mapping information: + + php console doctrine:schema-tool --create + +Or if you want to then drop your schema you can do: + + php console doctrine:schema-tool --drop + +If you want to re-create it (drop and create) you can use: + + php console doctrine:schema-tool --re-create + +Now the scenario arrises where you want to change your mapping information and +update your database without blowing away everything and losing your existing data. +You can do the following for that: + + php console doctrine:schema-tool --update + +> **TIP** +> The above will not drop anything from your database schema. To drop the remaining +> things from your schema you need to use the **--complete-update** option. +> +> php console doctrine:schema-tool --complete-update + +### Doctrine Build Command + The development workflow is very similar to how it is in Symfony 1.4. You can modify your mapping information and use **doctrine:build --all** to re-build your environment: @@ -187,6 +221,11 @@ schema: Now any changes you made in your mapping information will be reflected in the according databases! Here are all the available options for the **build** task: +> **NOTE** +> Not the key difference here is that you can modify your schema during development +> and just update your database schema without having to blow everything away and +> re-build it all. + $ php console help doctrine:build Usage: Symfony doctrine:build [--all] [--all-classes] [--entities] [--db] [--and-load[="..."]] [--and-append[="..."]] [--and-update-schema] [--dump-sql] [--connection] @@ -198,4 +237,20 @@ according databases! Here are all the available options for the **build** task: --and-load Load data fixtures (multiple values allowed) --and-append Load data fixtures and append to existing data (multiple values allowed) --and-update-schema Update schema after rebuilding all classes - --connection The connection to use. \ No newline at end of file + --connection The connection to use. + +### Doctrine Init Entity Command + +You can easily initialize a new Doctrine entity for a bundle by using the +**doctrine:init-bundle** command: + + $ php console doctrine:init-entity --bundle="Bundle\MySampleBundle" --entity="User\Group" + +Now if you have a look inside the bundle you will see that you have a **Group** class +located here **Bundle/MySampleBundle/Entities/User/Group.php**. + +Now you can customize the mapping information for the entity by editing the metadata +information inside **Bundle/MySampleBundle/Resources/config/doctrine/metadata** and +just update your database schema: + + $ php console doctrine:schema-tool --update \ No newline at end of file