Skip to content

Commit 42ad9b7

Browse files
jwagefabpot
authored andcommitted
[DoctrineBundle] Improvements for building entities and getting started
1 parent d15f232 commit 42ad9b7

File tree

4 files changed

+198
-4
lines changed

4 files changed

+198
-4
lines changed

src/Symfony/Framework/DoctrineBundle/Command/BuildDoctrineCommand.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ protected function configure()
5555
->addOption('and-append', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, 'Load data fixtures and append to existing data')
5656
->addOption('and-update-schema', null, null, 'Update schema after rebuilding all classes')
5757
->addOption('connection', null, null, 'The connection to use.')
58+
->setHelp('
59+
The <info>doctrine:build</info> task builds your Doctrine development environment.
60+
61+
<info>php console doctrine:build --all</info>
62+
63+
The above command will re-build your entities and re-create your database schema.
64+
65+
If you wanted to only update your schema instead of re-creating it you can run
66+
the following:
67+
68+
<info>php console doctrine:build --entities --and-update-schema</info>
69+
70+
Now your entities are re-built and your database schema is up to date!
71+
72+
You can also use the <info>--and-load</info> and <info>--and-append</info> to
73+
load data fixtures after running another build option.
74+
75+
<info>php console doctrine:build --all --and-load</info>
76+
77+
The above will re-build everything and load all bundle data fixtures.
78+
')
5879
;
5980
}
6081

src/Symfony/Framework/DoctrineBundle/Command/BuildEntitiesDoctrineCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
5656

5757
if (isset($bundleDirs[$namespace]))
5858
{
59-
if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Entities'))
59+
if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Resources/config/doctrine/metadata'))
6060
{
6161
$this->convertMapping($dir, $bundleDirs[$namespace].'/..');
62-
} else if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Resources/config/doctrine/metadata')) {
62+
}
63+
else if (is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Entities'))
64+
{
6365
$this->convertMapping($dir, $bundleDirs[$namespace].'/..');
6466
}
6567
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Symfony\Framework\DoctrineBundle\Command;
4+
5+
use Symfony\Components\Console\Input\InputArgument;
6+
use Symfony\Components\Console\Input\InputOption;
7+
use Symfony\Components\Console\Input\InputInterface;
8+
use Symfony\Components\Console\Output\OutputInterface;
9+
use Symfony\Components\Console\Output\Output;
10+
use Symfony\Framework\WebBundle\Util\Filesystem;
11+
use Doctrine\Common\Cli\Configuration;
12+
use Doctrine\Common\Cli\CliController as DoctrineCliController;
13+
14+
/*
15+
* This file is part of the symfony framework.
16+
*
17+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
18+
*
19+
* This source file is subject to the MIT license that is bundled
20+
* with this source code in the file LICENSE.
21+
*/
22+
23+
/**
24+
* Initialize a new Doctrine entity inside a bundle.
25+
*
26+
* @package symfony
27+
* @subpackage console
28+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
29+
* @author Jonathan H. Wage <jonwage@gmail.com>
30+
*/
31+
class InitEntityDoctrineCommand extends DoctrineCommand
32+
{
33+
/**
34+
* @see Command
35+
*/
36+
protected function configure()
37+
{
38+
$this
39+
->setName('doctrine:init-entity')
40+
->setDescription('Initialize a new Doctrine entity inside a bundle.')
41+
->addOption('bundle', null, InputOption::PARAMETER_REQUIRED, 'The bundle to initialize the entity in.')
42+
->addOption('entity', null, InputOption::PARAMETER_REQUIRED, 'The entity class to initialize.')
43+
->setHelp('
44+
The <info>doctrine:init-entity</info> task initializes a new Doctrine entity inside a bundle:
45+
46+
<comment>php console doctrine:init-entity --bundle="Bundle\MyCustomBundle" --entity="User\Group"</comment>
47+
48+
The above would initialize a new entity in the following entity namespace <info>Bundle\MyCustomBundle\Entities\User\Group</info>.
49+
50+
You can now build your entities and update your database schema:
51+
52+
<comment>php console doctrine:build --entities --and-update-schema</comment>
53+
54+
Now you have a new entity and your database has been updated.
55+
')
56+
;
57+
}
58+
59+
/**
60+
* @see Command
61+
*/
62+
protected function execute(InputInterface $input, OutputInterface $output)
63+
{
64+
if (!preg_match('/Bundle$/', $bundle = $input->getOption('bundle')))
65+
{
66+
throw new \InvalidArgumentException('The bundle name must end with Bundle. Example: "Bundle\MySampleBundle".');
67+
}
68+
69+
$dirs = $this->container->getKernelService()->getBundleDirs();
70+
71+
$tmp = str_replace('\\', '/', $bundle);
72+
$namespace = str_replace('/', '\\', dirname($tmp));
73+
$bundle = basename($tmp);
74+
75+
if (!isset($dirs[$namespace]))
76+
{
77+
throw new \InvalidArgumentException(sprintf('Unable to initialize the bundle entity (%s not defined).', $namespace));
78+
}
79+
80+
$entity = $input->getOption('entity');
81+
$entityNamespace = $namespace.'\\'.$bundle.'\\Entities';
82+
$fullEntityClassName = $entityNamespace.'\\'.$entity;
83+
$tmp = str_replace('\\', '/', $fullEntityClassName);
84+
$tmp = str_replace('/', '\\', dirname($tmp));
85+
$className = basename($tmp);
86+
87+
$extends = null;
88+
$path = $dirs[$namespace].'/'.$bundle.'/Resources/config/doctrine/metadata/'.str_replace('\\', '.', $fullEntityClassName).'.dcm.xml';
89+
90+
$xml = sprintf('<?xml version="1.0" encoding="UTF-8"?>
91+
92+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
93+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
94+
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
95+
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
96+
97+
<entity name="%s" table="%s">
98+
<id name="id" type="integer" column="id">
99+
<generator strategy="AUTO"/>
100+
</id>
101+
</entity>
102+
103+
</doctrine-mapping>',
104+
$fullEntityClassName,
105+
str_replace('\\', '_', strtolower($entity))
106+
);
107+
108+
if (!is_dir($dir = dirname($path)))
109+
{
110+
mkdir($dir, 0777, true);
111+
}
112+
113+
file_put_contents($path, $xml);
114+
$this->runCommand('doctrine:build-entities');
115+
}
116+
}

src/Symfony/Framework/DoctrineBundle/README

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ If you want to enable the Doctrine 2 ORM you can do so with the following:
5959

6060
doctrine.orm:
6161
default_entity_manager: default
62-
metadata_driver: xml # xml, yml, annotation
6362
cache_driver: apc # array, apc, memcache, xcache
6463
entity_managers:
6564
default:
@@ -157,6 +156,8 @@ file named **Entry.php** with some code like the following:
157156
The Doctrine 2 CLI is integrated with the Symfony 2 CLI so we have all the common
158157
commands we need to make working with Doctrine 2 just as easy and fast as before!
159158

159+
### Listing Available Doctrine Commands
160+
160161
$ php console list doctrine
161162

162163
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
167168
:database-tool Create and drop the configured databases.
168169
:ensure-production-settings Verify that Doctrine is properly configured for a production environment.
169170
:generate-proxies Generates proxy classes for entity classes.
171+
:import-mapping Import the initial mapping information for entities from an existing database.
172+
:init-entity Initialize a new Doctrine entity inside a bundle.
170173
:load-data-fixtures Load data fixtures to your database.
171174
:run-dql Executes arbitrary DQL directly from the command line.
172175
:run-sql Executes arbitrary SQL from a file or directly from the command line.
173176
:schema-tool Processes the schema and either apply it directly on EntityManager or generate the SQL output.
174177
:version Displays the current installed Doctrine version.
175178

179+
### Schema Tool
180+
181+
The schema tool in Doctrine 2 allows you to easily drop and your create your
182+
database schemas for your mapping information.
183+
184+
You can easily create your initial schema from mapping information:
185+
186+
php console doctrine:schema-tool --create
187+
188+
Or if you want to then drop your schema you can do:
189+
190+
php console doctrine:schema-tool --drop
191+
192+
If you want to re-create it (drop and create) you can use:
193+
194+
php console doctrine:schema-tool --re-create
195+
196+
Now the scenario arrises where you want to change your mapping information and
197+
update your database without blowing away everything and losing your existing data.
198+
You can do the following for that:
199+
200+
php console doctrine:schema-tool --update
201+
202+
> **TIP**
203+
> The above will not drop anything from your database schema. To drop the remaining
204+
> things from your schema you need to use the **--complete-update** option.
205+
>
206+
> php console doctrine:schema-tool --complete-update
207+
208+
### Doctrine Build Command
209+
176210
The development workflow is very similar to how it is in Symfony 1.4. You can modify
177211
your mapping information and use **doctrine:build --all** to re-build your
178212
environment:
@@ -187,6 +221,11 @@ schema:
187221
Now any changes you made in your mapping information will be reflected in the
188222
according databases! Here are all the available options for the **build** task:
189223

224+
> **NOTE**
225+
> Not the key difference here is that you can modify your schema during development
226+
> and just update your database schema without having to blow everything away and
227+
> re-build it all.
228+
190229
$ php console help doctrine:build
191230
Usage:
192231
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:
198237
--and-load Load data fixtures (multiple values allowed)
199238
--and-append Load data fixtures and append to existing data (multiple values allowed)
200239
--and-update-schema Update schema after rebuilding all classes
201-
--connection The connection to use.
240+
--connection The connection to use.
241+
242+
### Doctrine Init Entity Command
243+
244+
You can easily initialize a new Doctrine entity for a bundle by using the
245+
**doctrine:init-bundle** command:
246+
247+
$ php console doctrine:init-entity --bundle="Bundle\MySampleBundle" --entity="User\Group"
248+
249+
Now if you have a look inside the bundle you will see that you have a **Group** class
250+
located here **Bundle/MySampleBundle/Entities/User/Group.php**.
251+
252+
Now you can customize the mapping information for the entity by editing the metadata
253+
information inside **Bundle/MySampleBundle/Resources/config/doctrine/metadata** and
254+
just update your database schema:
255+
256+
$ php console doctrine:schema-tool --update

0 commit comments

Comments
 (0)