diff --git a/docs/en/tutorials/working-with-indexed-associations.rst b/docs/en/tutorials/working-with-indexed-associations.rst index c4099eb5589..81032ee8e4a 100644 --- a/docs/en/tutorials/working-with-indexed-associations.rst +++ b/docs/en/tutorials/working-with-indexed-associations.rst @@ -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 - - */ - #[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 */ - public function getStocks(): array - { - return $this->stocks->toArray(); - } - } - - .. code-block:: annotation - - - */ - 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 */ - public function getStocks(): array - { - return $this->stocks->toArray(); - } - } - - .. code-block:: xml - - - - - - - - - - - - - - - - .. 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)`` diff --git a/docs/en/tutorials/working-with-indexed-associations/Market-annotations.php b/docs/en/tutorials/working-with-indexed-associations/Market-annotations.php new file mode 100644 index 00000000000..798b49d1d02 --- /dev/null +++ b/docs/en/tutorials/working-with-indexed-associations/Market-annotations.php @@ -0,0 +1,74 @@ + + */ + 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 */ + public function getStocks(): array + { + return $this->stocks->toArray(); + } +} diff --git a/docs/en/tutorials/working-with-indexed-associations/Market.php b/docs/en/tutorials/working-with-indexed-associations/Market.php new file mode 100644 index 00000000000..bb16d3902b3 --- /dev/null +++ b/docs/en/tutorials/working-with-indexed-associations/Market.php @@ -0,0 +1,68 @@ + */ + #[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; + } + + 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 */ + public function getStocks(): array + { + return $this->stocks->toArray(); + } +} diff --git a/docs/en/tutorials/working-with-indexed-associations/market.xml b/docs/en/tutorials/working-with-indexed-associations/market.xml new file mode 100644 index 00000000000..3fc9fa2a857 --- /dev/null +++ b/docs/en/tutorials/working-with-indexed-associations/market.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + diff --git a/docs/en/tutorials/working-with-indexed-associations/market.yaml b/docs/en/tutorials/working-with-indexed-associations/market.yaml new file mode 100644 index 00000000000..b7c8132e090 --- /dev/null +++ b/docs/en/tutorials/working-with-indexed-associations/market.yaml @@ -0,0 +1,15 @@ +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