@@ -31,169 +31,18 @@ You can map indexed associations by adding:
3131The code and mappings for the Market entity looks like this:
3232
3333.. configuration-block ::
34- .. code-block :: attribute
35-
36- <?php
37- namespace Doctrine\Tests\Models\StockExchange;
38-
39- use Doctrine\Common\Collections\ArrayCollection;
40- use Doctrine\Common\Collections\Collection;
41-
42- #[Entity]
43- #[Table(name: 'exchange_markets')]
44- class Market
45- {
46- #[Id, Column(type: 'integer'), GeneratedValue]
47- private int|null $id = null;
48-
49- #[Column(type: 'string')]
50- private string $name;
51-
52- /** @var Collection<string, Stock> */
53- #[OneToMany(targetEntity: Stock::class, mappedBy: 'market', indexBy: 'symbol')]
54- private Collection $stocks;
55-
56- public function __construct(string $name)
57- {
58- $this->name = $name;
59- $this->stocks = new ArrayCollection();
60- }
61-
62- public function getId(): int|null
63- {
64- return $this->id;
65- }
66-
67- public function getName(): string
68- {
69- return $this->name;
70- }
71-
72- public function addStock(Stock $stock): void
73- {
74- $this->stocks[$stock->getSymbol()] = $stock;
75- }
34+ .. literalinclude :: working-with-indexed-associations/Market.php
35+ :language: attribute
7636
77- public function getStock(string $symbol): Stock
78- {
79- if (!isset($this->stocks[$symbol])) {
80- throw new \InvalidArgumentException("Symbol is not traded on this market.");
81- }
82-
83- return $this->stocks[$symbol];
84- }
85-
86- /** @return array<string, Stock> */
87- public function getStocks(): array
88- {
89- return $this->stocks->toArray();
90- }
91- }
92-
93- .. code-block :: annotation
94-
95- <?php
96- namespace Doctrine\Tests\Models\StockExchange;
37+ .. literalinclude :: working-with-indexed-associations/Market-annotations.php
38+ :language: annotation
9739
98- use Doctrine\Common\Collections\ArrayCollection;
40+ .. literalinclude :: working-with-indexed-associations/market.xml
41+ :language: xml
9942
100- /**
101- * @Entity
102- * @Table(name="exchange_markets")
103- */
104- class Market
105- {
106- /**
107- * @Id @Column(type="integer") @GeneratedValue
108- * @var int
109- */
110- private int|null $id = null;
111-
112- /**
113- * @Column(type="string")
114- * @var string
115- */
116- private string $name;
43+ .. literalinclude :: working-with-indexed-associations/market.yaml
44+ :language: yaml
11745
118- /**
119- * @OneToMany(targetEntity="Stock", mappedBy="market", indexBy="symbol")
120- * @var Collection<int, Stock>
121- */
122- private Collection $stocks;
123-
124- public function __construct($name)
125- {
126- $this->name = $name;
127- $this->stocks = new ArrayCollection();
128- }
129-
130- public function getId(): int|null
131- {
132- return $this->id;
133- }
134-
135- public function getName(): string
136- {
137- return $this->name;
138- }
139-
140- public function addStock(Stock $stock): void
141- {
142- $this->stocks[$stock->getSymbol()] = $stock;
143- }
144-
145- public function getStock($symbol): Stock
146- {
147- if (!isset($this->stocks[$symbol])) {
148- throw new \InvalidArgumentException("Symbol is not traded on this market.");
149- }
150-
151- return $this->stocks[$symbol];
152- }
153-
154- /** @return array<string, Stock> */
155- public function getStocks(): array
156- {
157- return $this->stocks->toArray();
158- }
159- }
160-
161- .. code-block :: xml
162-
163- <?xml version =" 1.0" encoding =" UTF-8" ?>
164- <doctrine-mapping xmlns =" http://doctrine-project.org/schemas/orm/doctrine-mapping"
165- xmlns : xsi =" https://www.w3.org/2001/XMLSchema-instance"
166- xsi : schemaLocation =" http://doctrine-project.org/schemas/orm/doctrine-mapping
167- https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd" >
168-
169- <entity name =" Doctrine\Tests\Models\StockExchange\Market" >
170- <id name =" id" type =" integer" >
171- <generator strategy =" AUTO" />
172- </id >
173-
174- <field name =" name" type =" string" />
175-
176- <one-to-many target-entity =" Stock" mapped-by =" market" field =" stocks" index-by =" symbol" />
177- </entity >
178- </doctrine-mapping >
179-
180- .. code-block :: yaml
181-
182- Doctrine\Tests\Models\StockExchange\Market :
183- type : entity
184- id :
185- id :
186- type : integer
187- generator :
188- strategy : AUTO
189- fields :
190- name :
191- type:string
192- oneToMany :
193- stocks :
194- targetEntity : Stock
195- mappedBy : market
196- indexBy : symbol
19746
19847Inside the ``addStock() `` method you can see how we directly set the key of the association to the symbol,
19948so that we can work with the indexed association directly after invoking ``addStock() ``. Inside ``getStock($symbol) ``
0 commit comments