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

Merge 2.20.x up into 2.21.x #11687

Merged
merged 9 commits into from
Oct 16, 2024
93 changes: 0 additions & 93 deletions docs/en/_exts/configurationblock.py

This file was deleted.

113 changes: 0 additions & 113 deletions docs/en/make.bat

This file was deleted.

167 changes: 8 additions & 159 deletions docs/en/tutorials/working-with-indexed-associations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,169 +31,18 @@ You can map indexed associations by adding:
The code and mappings for the Market entity looks like this:

.. configuration-block::
.. code-block:: attribute

<?php
namespace Doctrine\Tests\Models\StockExchange;

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

#[Entity]
#[Table(name: 'exchange_markets')]
class Market
{
#[Id, Column(type: 'integer'), GeneratedValue]
private int|null $id = null;

#[Column(type: 'string')]
private string $name;

/** @var Collection<string, Stock> */
#[OneToMany(targetEntity: Stock::class, mappedBy: 'market', indexBy: 'symbol')]
private Collection $stocks;

public function __construct(string $name)
{
$this->name = $name;
$this->stocks = new ArrayCollection();
}

public function getId(): int|null
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function addStock(Stock $stock): void
{
$this->stocks[$stock->getSymbol()] = $stock;
}
.. literalinclude:: working-with-indexed-associations/Market.php
:language: attribute

public function getStock(string $symbol): Stock
{
if (!isset($this->stocks[$symbol])) {
throw new \InvalidArgumentException("Symbol is not traded on this market.");
}

return $this->stocks[$symbol];
}

/** @return array<string, Stock> */
public function getStocks(): array
{
return $this->stocks->toArray();
}
}

.. code-block:: annotation

<?php
namespace Doctrine\Tests\Models\StockExchange;
.. literalinclude:: working-with-indexed-associations/Market-annotations.php
:language: annotation

use Doctrine\Common\Collections\ArrayCollection;
.. literalinclude:: working-with-indexed-associations/market.xml
:language: xml

/**
* @Entity
* @Table(name="exchange_markets")
*/
class Market
{
/**
* @Id @Column(type="integer") @GeneratedValue
* @var int
*/
private int|null $id = null;

/**
* @Column(type="string")
* @var string
*/
private string $name;
.. literalinclude:: working-with-indexed-associations/market.yaml
:language: yaml

/**
* @OneToMany(targetEntity="Stock", mappedBy="market", indexBy="symbol")
* @var Collection<int, Stock>
*/
private Collection $stocks;

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

public function getId(): int|null
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function addStock(Stock $stock): void
{
$this->stocks[$stock->getSymbol()] = $stock;
}

public function getStock($symbol): Stock
{
if (!isset($this->stocks[$symbol])) {
throw new \InvalidArgumentException("Symbol is not traded on this market.");
}

return $this->stocks[$symbol];
}

/** @return array<string, Stock> */
public function getStocks(): array
{
return $this->stocks->toArray();
}
}

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Doctrine\Tests\Models\StockExchange\Market">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>

<field name="name" type="string"/>

<one-to-many target-entity="Stock" mapped-by="market" field="stocks" index-by="symbol" />
</entity>
</doctrine-mapping>

.. code-block:: yaml

Doctrine\Tests\Models\StockExchange\Market:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type:string
oneToMany:
stocks:
targetEntity: Stock
mappedBy: market
indexBy: symbol

Inside the ``addStock()`` method you can see how we directly set the key of the association to the symbol,
so that we can work with the indexed association directly after invoking ``addStock()``. Inside ``getStock($symbol)``
Expand Down
Loading
Loading