1
0
mirror of synced 2025-01-31 04:21:44 +03:00

Add tons of xml and yaml configuration blocks to basic- and association-mapping chapters

This commit is contained in:
Benjamin Eberlei 2011-06-12 23:07:21 +02:00
parent 6816816101
commit b5827ea83f
2 changed files with 449 additions and 147 deletions

View File

@ -133,16 +133,36 @@ follows:
As an example, consider this mapping:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
/** @OneToOne(targetEntity="Shipping") */
private $shipping;
.. code-block:: xml
<doctrine-mapping>
<entity class="Product">
<one-to-one field="shipping" target-entity="Shipping" />
</entity>
</doctrine-mapping>
.. code-block:: yaml
Product:
type: entity
oneToOne:
shipping:
targetEntity: Shipping
This is essentially the same as the following, more verbose,
mapping:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
/**
@ -151,10 +171,33 @@ mapping:
*/
private $shipping;
.. code-block:: xml
<doctrine-mapping>
<entity class="Product">
<one-to-one field="shipping" target-entity="Shipping">
<join-column name="shipping_id" referenced-column-name="id" />
</one-to-one>
</entity>
</doctrine-mapping>
.. code-block:: yaml
Product:
type: entity
oneToOne:
shipping:
targetEntity: Shipping
joinColumn:
name: shipping_id
referencedColumnName: id
The @JoinTable definition used for many-to-many mappings has
similar defaults. As an example, consider this mapping:
.. code-block:: php
.. code-configuration::
.. code-block:: php
<?php
class User
@ -165,10 +208,28 @@ similar defaults. As an example, consider this mapping:
//...
}
.. code-block:: xml
<doctrine-mapping>
<entity class="User">
<many-to-many field="groups" target-entity="Group" />
</entity>
</doctrine-mapping>
.. code-block:: yaml
User:
type: entity
manyToMany:
groups:
targetEntity: Group
This is essentially the same as the following, more verbose,
mapping:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
class User
@ -185,6 +246,39 @@ mapping:
//...
}
.. code-block:: xml
<doctrine-mapping>
<entity class="User">
<many-to-many field="groups" target-entity="Group">
<join-table name="User_Group">
<join-columns>
<join-column id="User_id" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column id="Group_id" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>
.. code-block:: yaml
User:
type: entity
manyToMany:
groups:
targetEntity: Group
joinTable:
name: User_Group
joinColumns:
User_id:
referencedColumnName: id
inverseJoinColumns:
Group_id
referencedColumnName: id
In that case, the name of the join table defaults to a combination
of the simple, unqualified class names of the participating
classes, separated by an underscore character. The names of the
@ -276,6 +370,7 @@ Or you can trigger the validation manually:
.. code-block:: php
<?php
use Doctrine\ORM\Tools\SchemaValidator;
$validator = new SchemaValidator($entityManager);
@ -306,7 +401,9 @@ example of a ``Product`` that has one ``Shipping`` object
associated to it. The ``Shipping`` side does not reference back to
the ``Product`` so it is unidirectional.
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
/** @Entity */
@ -329,6 +426,27 @@ the ``Product`` so it is unidirectional.
// ...
}
.. code-block:: xml
<doctrine-mapping>
<entity class="Product">
<one-to-one field="shipping" target-entity="Shipping">
<join-column name="shipping_id" referenced-column-name="id" />
</one-to-one>
</entity>
</doctrine-mapping>
.. code-block:: yaml
Product:
type: entity
oneToOne:
shipping:
targetEntity: Shipping
joinColumn:
name: shipping_id
referencedColumnName: id
Note that the @JoinColumn is not really necessary in this example,
as the defaults would be the same.
@ -354,7 +472,9 @@ Here is a one-to-one relationship between a ``Customer`` and a
``Cart``. The ``Cart`` has a reference back to the ``Customer`` so
it is bidirectional.
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
/** @Entity */
@ -384,6 +504,35 @@ it is bidirectional.
// ...
}
.. code-block:: xml
<doctrine-mapping>
<entity name="Customer">
<one-to-one field="cart" target-entity="Cart" mapped-by="customer" />
</entity>
<entity name="Cart">
<one-to-one field="customer" target-entity="Customer" inversed-by="cart">
<join-column name="customer_id" referenced-column-name="id" />
</one-to-one>
</entity>
</doctrine-mapping>
.. code-block:: yaml
Customer:
oneToOne:
cart:
targetEntity: Cart
mappedBy: customer
Cart:
oneToOne:
customer:
targetEntity Customer
inversedBy: cart
joinColumn:
customer_id:
referencedColumnName: id
Note that the @JoinColumn is not really necessary in this example,
as the defaults would be the same.

View File

@ -16,9 +16,11 @@ object-relational mapping metadata:
- XML
- YAML
This manual usually uses docblock annotations in all the examples
that are spread throughout all chapters. There are dedicated
chapters for XML and YAML mapping, respectively.
This manual usually mentions docblock annotations in all the examples
that are spread throughout all chapters, however for many examples
alternative YAML and XML examples are given aswell. There are dedicated
reference chapters for XML and YAML mapping, respectively that explain them
in more detail. There is also an Annotation reference chapter.
.. note::
@ -56,7 +58,9 @@ annotations support namespaces and nested annotations among other
things. The Doctrine 2 ORM defines its own set of docblock
annotations for supplying object-relational mapping metadata.
**NOTE** If you're not comfortable with the concept of docblock
.. note::
If you're not comfortable with the concept of docblock
annotations, don't worry, as mentioned earlier Doctrine 2 provides
XML and YAML alternatives and you could easily implement your own
favourite mechanism for defining ORM metadata.
@ -69,7 +73,9 @@ In order to mark a class for object-relational persistence it needs
to be designated as an entity. This can be done through the
``@Entity`` marker annotation.
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
/** @Entity */
@ -78,11 +84,27 @@ to be designated as an entity. This can be done through the
//...
}
.. code-block:: xml
<doctrine-mapping>
<entity name="MyPersistentClass">
<!-- ... ->
</entity>
</doctrine-mapping>
.. code-block:: yaml
MyPersistentClass:
type: entity
# ...
By default, the entity will be persisted to a table with the same
name as the class name. In order to change that, you can use the
``@Table`` annotation as follows:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
/**
@ -94,6 +116,21 @@ name as the class name. In order to change that, you can use the
//...
}
.. code-block:: xml
<doctrine-mapping>
<entity name="MyPersistentClass" table="my_persistent_class">
<!-- ... ->
</entity>
</doctrine-mapping>
.. code-block:: yaml
MyPersistentClass:
type: entity
table: my_persistent_class
# ...
Now instances of MyPersistentClass will be persisted into a table
named ``my_persistent_class``.
@ -172,7 +209,9 @@ since it is the most flexible.
Example:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
/** @Entity */
@ -185,6 +224,25 @@ Example:
//...
}
.. code-block:: xml
<doctrine-mapping>
<entity name="MyPersistentClass">
<field name="id" type="integer" />
<field name="name" length="50" />
</entity>
</doctrine-mapping>
.. code-block:: yaml
MyPersistentClass:
type: entity
fields:
id:
type: integer
name:
length: 50
In that example we mapped the field ``id`` to the column ``id``
using the mapping type ``integer`` and the field ``name`` is mapped
to the column ``name`` with the default mapping type ``string``. As
@ -193,12 +251,31 @@ as the field names. To specify a different name for the column, you
can use the ``name`` attribute of the Column annotation as
follows:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
/** @Column(name="db_name") */
private $name;
.. code-block:: xml
<doctrine-mapping>
<entity name="MyPersistentClass">
<field name="name" column="db_name" />
</entity>
</doctrine-mapping>
.. code-block:: yaml
MyPersistentClass:
type: entity
fields:
name:
length: 50
column: db_name
The Column annotation has some more attributes. Here is a complete
list:
@ -340,7 +417,9 @@ Every entity class needs an identifier/primary key. You designate
the field that serves as the identifier with the ``@Id`` marker
annotation. Here is an example:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
class MyPersistentClass
@ -350,6 +429,26 @@ annotation. Here is an example:
//...
}
.. code-block:: xml
<doctrine-mapping>
<entity name="MyPersistentClass">
<id name="id" type="integer" />
<field name="name" length="50" />
</entity>
</doctrine-mapping>
.. code-block:: yaml
MyPersistentClass:
type: entity
id:
id:
type: integer
fields:
name:
length: 50
Without doing anything else, the identifier is assumed to be
manually assigned. That means your code would need to properly set
the identifier property before passing a new entity to
@ -359,7 +458,9 @@ A common alternative strategy is to use a generated value as the
identifier. To do this, you use the ``@GeneratedValue`` annotation
like this:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
class MyPersistentClass
@ -371,6 +472,30 @@ like this:
private $id;
}
.. code-block:: xml
<doctrine-mapping>
<entity name="MyPersistentClass">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
<field name="name" length="50" />
</entity>
</doctrine-mapping>
.. code-block:: yaml
MyPersistentClass:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
length: 50
This tells Doctrine to automatically generate a value for the
identifier. How this value is generated is specified by the
``strategy`` attribute, which is optional and defaults to 'AUTO'. A
@ -417,10 +542,13 @@ The Sequence Generator can currently be used in conjunction with
Oracle or Postgres and allows some additional configuration options
besides specifying the sequence's name:
.. code-block:: php
.. configuration-block::
.. code-block:: php
<?php
class User {
class User
{
/**
* @Id
* @GeneratedValue(strategy="SEQUENCE")
@ -429,6 +557,31 @@ besides specifying the sequence's name:
protected $id = null;
}
.. code-block:: xml
<doctrine-mapping>
<entity name="User">
<id name="id" type="integer">
<generator strategy="SEQUENCE" />
<sequence-generator sequence-name="tablename_seq" allocation-size="100" initial-value="1" />
</id>
</entity>
</doctrine-mapping>
.. code-block:: yaml
MyPersistentClass:
type: entity
id:
id:
type: integer
generator:
strategy: SEQUENCE
sequenceGenerator:
sequenceName: tablename_seq
allocationSize: 100
initialValue: 1
The initial value specifies at which value the sequence should
start.