Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate more documentation towards attributes #10157

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -32,59 +32,39 @@ The entity class:

namespace Geo\Entity;

/**
* @Entity
*/
use Geo\ValueObject\Point;

#[Entity]
class Location
{
/**
* @Column(type="point")
*
* @var \Geo\ValueObject\Point
*/
private $point;

/**
* @Column(type="string")
*
* @var string
*/
private $address;

/**
* @param \Geo\ValueObject\Point $point
*/
public function setPoint(\Geo\ValueObject\Point $point)
#[Column(type: 'point')]
private Point $point;

#[Column]
private string $address;

public function setPoint(Point $point): void
{
$this->point = $point;
}

/**
* @return \Geo\ValueObject\Point
*/
public function getPoint()
public function getPoint(): Point
{
return $this->point;
}

/**
* @param string $address
*/
public function setAddress($address)
public function setAddress(string $address): void
{
$this->address = $address;
}

/**
* @return string
*/
public function getAddress()
public function getAddress(): string
{
return $this->address;
}
}

We use the custom type ``point`` in the ``@Column`` docblock annotation of the
We use the custom type ``point`` in the ``#[Column]`` attribute of the
``$point`` field. We will create this custom mapping type in the next chapter.

The point class:
Expand All @@ -97,29 +77,18 @@ The point class:

class Point
{

/**
* @param float $latitude
* @param float $longitude
*/
public function __construct($latitude, $longitude)
{
$this->latitude = $latitude;
$this->longitude = $longitude;
public function __construct(
private float $latitude,
private float $longitude,
) {
}

/**
* @return float
*/
public function getLatitude()
public function getLatitude(): float
{
return $this->latitude;
}

/**
* @return float
*/
public function getLongitude()
public function getLongitude(): float
{
return $this->longitude;
}
Expand Down
Expand Up @@ -29,15 +29,15 @@ implement the ``NotifyPropertyChanged`` interface from the
<?php
use Doctrine\Persistence\NotifyPropertyChanged;
use Doctrine\Persistence\PropertyChangedListener;

abstract class DomainObject implements NotifyPropertyChanged
{
private $listeners = array();

public function addPropertyChangedListener(PropertyChangedListener $listener) {
$this->listeners[] = $listener;
}

/** Notifies listeners of a change. */
protected function onPropertyChanged($propName, $oldValue, $newValue) {
if ($this->listeners) {
Expand All @@ -55,12 +55,12 @@ listeners:
.. code-block:: php

<?php
// Mapping not shown, either in annotations, xml or yaml as usual
// Mapping not shown, either in attributes, annotations, xml or yaml as usual
class MyEntity extends DomainObject
{
private $data;
// ... other fields as usual

public function setData($data) {
if ($data != $this->data) { // check: is it actually modified?
$this->onPropertyChanged('data', $this->data, $data);
Expand All @@ -73,5 +73,3 @@ The check whether the new value is different from the old one is
not mandatory but recommended. That way you can avoid unnecessary
updates and also have full control over when you consider a
property changed.


6 changes: 2 additions & 4 deletions docs/en/cookbook/strategy-cookbook-introduction.rst
Expand Up @@ -154,8 +154,8 @@ As you can see, we have a method "setBlockEntity" which ties a potential strateg
* This var contains the classname of the strategy
* that is used for this blockitem. (This string (!) value will be persisted by Doctrine ORM)
*
* This is a doctrine field, so make sure that you use an @column annotation or setup your
* yaml or xml files correctly
* This is a doctrine field, so make sure that you use a
#[Column] attribute or setup your yaml or xml files correctly
* @var string
*/
protected $strategyClassName;
Expand Down Expand Up @@ -251,5 +251,3 @@ This might look like this:

In this example, even some variables are set - like a view object
or a specific configuration object.


31 changes: 20 additions & 11 deletions docs/en/cookbook/validation-of-entities.rst
Expand Up @@ -36,12 +36,12 @@ are allowed to:
public function assertCustomerAllowedBuying()
{
$orderLimit = $this->customer->getOrderLimit();

$amount = 0;
foreach ($this->orderLines as $line) {
$amount += $line->getAmount();
}

if ($amount > $orderLimit) {
throw new CustomerOrderLimitExceededException();
}
Expand All @@ -53,7 +53,21 @@ code, enforcing it at any time is important so that customers with
a unknown reputation don't owe your business too much money.

We can enforce this constraint in any of the metadata drivers.
First Annotations:
First Attributes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the doc below shows other formats. So maybe it should keep annotations as well.

And for Yaml, there is a sentence saying this will happen before Beta 1 though which looks totally wrong in the doc (and it does not say beta 1 of which version)


.. code-block:: php

<?php

#[Entity]
#[HasLifecycleCallbacks]
class Order
{
#[PrePersist, PreUpdate]
public function assertCustomerAllowedBuying() {}
}

As Annotations:

.. code-block:: php

Expand Down Expand Up @@ -83,9 +97,6 @@ In XML Mappings:
</entity>
</doctrine-mapping>

YAML needs some little change yet, to allow multiple lifecycle
events for one method, this will happen before Beta 1 though.

Now validation is performed whenever you call
``EntityManager#persist($order)`` or when you call
``EntityManager#flush()`` and an order is about to be updated. Any
Expand All @@ -101,19 +112,17 @@ validation callbacks.
<?php
class Order
{
/**
* @PrePersist @PreUpdate
*/
#[PrePersist, PreUpdate]
public function validate()
{
if (!($this->plannedShipDate instanceof DateTime)) {
throw new ValidateException();
}

if ($this->plannedShipDate->format('U') < time()) {
throw new ValidateException();
}

if ($this->customer == null) {
throw new OrderRequiresCustomerException();
}
Expand Down
12 changes: 6 additions & 6 deletions docs/en/reference/advanced-configuration.rst
Expand Up @@ -12,6 +12,7 @@ steps of configuration.

use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\ORMSetup;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
Expand All @@ -28,7 +29,7 @@ steps of configuration.

$config = new Configuration;
$config->setMetadataCache($metadataCache);
$driverImpl = ORMSetup::createDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
$driverImpl = new AttributeDriver(['/path/to/lib/MyProject/Entities']);
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCache($queryCache);
$config->setProxyDir('/path/to/myproject/lib/MyProject/Proxies');
Expand Down Expand Up @@ -124,18 +125,17 @@ used in the examples. For information on the usage of the
AnnotationDriver, XmlDriver or YamlDriver please refer to the dedicated
chapters ``Annotation Reference``, ``XML Mapping`` and ``YAML Mapping``.

The annotation driver can be configured with a factory method on
the ``Doctrine\ORM\Configuration``:
The attribute driver can be injected in the ``Doctrine\ORM\Configuration``:

.. code-block:: php

<?php
use Doctrine\ORM\ORMSetup;

$driverImpl = ORMSetup::createDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
$driverImpl = new AttributeDriver(['/path/to/lib/MyProject/Entities']);
$config->setMetadataDriverImpl($driverImpl);

The path information to the entities is required for the annotation
The path information to the entities is required for the attribute
driver, because otherwise mass-operations on all entities through
the console could not work correctly. All of metadata drivers
accept either a single directory as a string or an array of
Expand All @@ -152,7 +152,7 @@ Metadata Cache (***RECOMMENDED***)
$config->getMetadataCache();

Gets or sets the cache adapter to use for caching metadata
information, that is, all the information you supply via
information, that is, all the information you supply via attributes,
annotations, xml or yaml, so that they do not need to be parsed and
loaded from scratch on every single request which is a waste of
resources. The cache implementation must implement the PSR-6
Expand Down