Skip to content

Added attribute code block examples #15720

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

Merged
merged 1 commit into from
Aug 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 79 additions & 7 deletions doctrine/associations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,32 @@ the ``Product`` entity (and getter & setter methods):
}
}

.. code-block:: php-attributes

// src/Entity/Product.php
namespace App\Entity;

// ...
class Product
{
// ...

#[ORM\ManyToOne(targetEntity: Category::class, inversedBy: "products")]
private $category;

public function getCategory(): ?Category
{
return $this->category;
}

public function setCategory(?Category $category): self
{
$this->category = $category;

return $this;
}
}

.. code-block:: yaml

# src/Resources/config/doctrine/Product.orm.yml
Expand Down Expand Up @@ -248,6 +274,38 @@ class that will hold these objects:
// addProduct() and removeProduct() were also added
}

.. code-block:: php-attributes

// src/Entity/Category.php
namespace App\Entity;

// ...
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

class Category
{
// ...

#[ORM\OneToMany(targetEntity: Product::class, mappedBy: "category")]
private $products;

public function __construct()
{
$this->products = new ArrayCollection();
}

/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}

// addProduct() and removeProduct() were also added
}

.. code-block:: yaml

# src/Resources/config/doctrine/Category.orm.yml
Expand Down Expand Up @@ -597,16 +655,30 @@ on that ``Product`` will be set to ``null`` in the database.

But, instead of setting the ``category_id`` to null, what if you want the ``Product``
to be *deleted* if it becomes "orphaned" (i.e. without a ``Category``)? To choose
that behavior, use the `orphanRemoval`_ option inside ``Category``::
that behavior, use the `orphanRemoval`_ option inside ``Category``:

// src/Entity/Category.php
.. configuration-block::

// ...
.. code-block:: php-annotations

// src/Entity/Category.php

// ...

/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category", orphanRemoval=true)
*/
private $products;

.. code-block:: php-attributes

// src/Entity/Category.php

// ...

#[ORM\OneToMany(targetEntity: Product::class, mappedBy: "category", orphanRemoval=true)]
private $products;

/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category", orphanRemoval=true)
*/
private $products;

Thanks to this, if the ``Product`` is removed from the ``Category``, it will be
removed from the database entirely.
Expand Down