Add tons of xml and yaml configuration blocks to basic- and association-mapping chapters
This commit is contained in:
parent
6816816101
commit
b5827ea83f
@ -133,16 +133,36 @@ follows:
|
|||||||
|
|
||||||
As an example, consider this mapping:
|
As an example, consider this mapping:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
/** @OneToOne(targetEntity="Shipping") */
|
/** @OneToOne(targetEntity="Shipping") */
|
||||||
private $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,
|
This is essentially the same as the following, more verbose,
|
||||||
mapping:
|
mapping:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
@ -151,10 +171,33 @@ mapping:
|
|||||||
*/
|
*/
|
||||||
private $shipping;
|
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
|
The @JoinTable definition used for many-to-many mappings has
|
||||||
similar defaults. As an example, consider this mapping:
|
similar defaults. As an example, consider this mapping:
|
||||||
|
|
||||||
.. code-block:: php
|
.. code-configuration::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
class User
|
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,
|
This is essentially the same as the following, more verbose,
|
||||||
mapping:
|
mapping:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
class User
|
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
|
In that case, the name of the join table defaults to a combination
|
||||||
of the simple, unqualified class names of the participating
|
of the simple, unqualified class names of the participating
|
||||||
classes, separated by an underscore character. The names of the
|
classes, separated by an underscore character. The names of the
|
||||||
@ -276,6 +370,7 @@ Or you can trigger the validation manually:
|
|||||||
|
|
||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
|
<?php
|
||||||
use Doctrine\ORM\Tools\SchemaValidator;
|
use Doctrine\ORM\Tools\SchemaValidator;
|
||||||
|
|
||||||
$validator = new SchemaValidator($entityManager);
|
$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
|
associated to it. The ``Shipping`` side does not reference back to
|
||||||
the ``Product`` so it is unidirectional.
|
the ``Product`` so it is unidirectional.
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
/** @Entity */
|
/** @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,
|
Note that the @JoinColumn is not really necessary in this example,
|
||||||
as the defaults would be the same.
|
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
|
``Cart``. The ``Cart`` has a reference back to the ``Customer`` so
|
||||||
it is bidirectional.
|
it is bidirectional.
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
/** @Entity */
|
/** @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,
|
Note that the @JoinColumn is not really necessary in this example,
|
||||||
as the defaults would be the same.
|
as the defaults would be the same.
|
||||||
|
|
||||||
|
@ -16,9 +16,11 @@ object-relational mapping metadata:
|
|||||||
- XML
|
- XML
|
||||||
- YAML
|
- YAML
|
||||||
|
|
||||||
This manual usually uses docblock annotations in all the examples
|
This manual usually mentions docblock annotations in all the examples
|
||||||
that are spread throughout all chapters. There are dedicated
|
that are spread throughout all chapters, however for many examples
|
||||||
chapters for XML and YAML mapping, respectively.
|
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::
|
.. note::
|
||||||
|
|
||||||
@ -56,7 +58,9 @@ annotations support namespaces and nested annotations among other
|
|||||||
things. The Doctrine 2 ORM defines its own set of docblock
|
things. The Doctrine 2 ORM defines its own set of docblock
|
||||||
annotations for supplying object-relational mapping metadata.
|
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
|
annotations, don't worry, as mentioned earlier Doctrine 2 provides
|
||||||
XML and YAML alternatives and you could easily implement your own
|
XML and YAML alternatives and you could easily implement your own
|
||||||
favourite mechanism for defining ORM metadata.
|
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
|
to be designated as an entity. This can be done through the
|
||||||
``@Entity`` marker annotation.
|
``@Entity`` marker annotation.
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
/** @Entity */
|
/** @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
|
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
|
name as the class name. In order to change that, you can use the
|
||||||
``@Table`` annotation as follows:
|
``@Table`` annotation as follows:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?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
|
Now instances of MyPersistentClass will be persisted into a table
|
||||||
named ``my_persistent_class``.
|
named ``my_persistent_class``.
|
||||||
|
|
||||||
@ -172,7 +209,9 @@ since it is the most flexible.
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
/** @Entity */
|
/** @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``
|
In that example we mapped the field ``id`` to the column ``id``
|
||||||
using the mapping type ``integer`` and the field ``name`` is mapped
|
using the mapping type ``integer`` and the field ``name`` is mapped
|
||||||
to the column ``name`` with the default mapping type ``string``. As
|
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
|
can use the ``name`` attribute of the Column annotation as
|
||||||
follows:
|
follows:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
/** @Column(name="db_name") */
|
/** @Column(name="db_name") */
|
||||||
private $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
|
The Column annotation has some more attributes. Here is a complete
|
||||||
list:
|
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
|
the field that serves as the identifier with the ``@Id`` marker
|
||||||
annotation. Here is an example:
|
annotation. Here is an example:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
class MyPersistentClass
|
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
|
Without doing anything else, the identifier is assumed to be
|
||||||
manually assigned. That means your code would need to properly set
|
manually assigned. That means your code would need to properly set
|
||||||
the identifier property before passing a new entity to
|
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
|
identifier. To do this, you use the ``@GeneratedValue`` annotation
|
||||||
like this:
|
like this:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
class MyPersistentClass
|
class MyPersistentClass
|
||||||
@ -371,6 +472,30 @@ like this:
|
|||||||
private $id;
|
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
|
This tells Doctrine to automatically generate a value for the
|
||||||
identifier. How this value is generated is specified by the
|
identifier. How this value is generated is specified by the
|
||||||
``strategy`` attribute, which is optional and defaults to 'AUTO'. A
|
``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
|
Oracle or Postgres and allows some additional configuration options
|
||||||
besides specifying the sequence's name:
|
besides specifying the sequence's name:
|
||||||
|
|
||||||
.. code-block:: php
|
.. configuration-block::
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
class User {
|
class User
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* @Id
|
* @Id
|
||||||
* @GeneratedValue(strategy="SEQUENCE")
|
* @GeneratedValue(strategy="SEQUENCE")
|
||||||
@ -429,6 +557,31 @@ besides specifying the sequence's name:
|
|||||||
protected $id = null;
|
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
|
The initial value specifies at which value the sequence should
|
||||||
start.
|
start.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user