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

Experiment with literalinclude #11667

Merged
merged 1 commit into from
Oct 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.xml
: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
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Doctrine\Tests\Models\StockExchange;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table;
use InvalidArgumentException;

/**
* @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;

/**
* @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();
}
}
68 changes: 68 additions & 0 deletions docs/en/tutorials/working-with-indexed-associations/Market.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\StockExchange;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table;
use InvalidArgumentException;

#[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;
}

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();
}
}
16 changes: 16 additions & 0 deletions docs/en/tutorials/working-with-indexed-associations/market.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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>
15 changes: 15 additions & 0 deletions docs/en/tutorials/working-with-indexed-associations/market.yaml
Original file line number Diff line number Diff line change
@@ -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