diff --git a/en/tutorials/composite-primary-keys.rst b/en/tutorials/composite-primary-keys.rst index 8048fbb78..7c7fa9baa 100644 --- a/en/tutorials/composite-primary-keys.rst +++ b/en/tutorials/composite-primary-keys.rst @@ -198,14 +198,117 @@ We keep up the example of an Article with arbitrary attributes, the mapping look Use-Case 2: Simple Derived Identity ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -TODO +Sometimes you have the requirement that two objects are related by a One-To-One association +and that the dependent class should re-use the primary key of the class it depends on. +One good example for this is a user-address relationship: + +.. code-block:: php + + customer = $customer; + $this->items = new ArrayCollection(); + $this->created = new \DateTime("now"); + } + } + + /** @Entity */ + class Product + { + /** @Id @Column(type="integer") @GeneratedValue */ + private $id; + + /** @Column(type="string") + private $name; + + /** @Column(type="decimal") + private $currentPrice; + + public function getCurrentPrice() + { + return $this->currentPrice; + } + } + + /** @Entity */ + class OrderItem + { + /** @Id @ManyToOne(targetEntity="Order") */ + private $order; + + /** @Id @ManyToOne(targetEntity="Product") */ + private $product; + + /** @Column(type="integer") */ + private $amount = 1; + + /** @Column(type="decimal") */ + private $offeredPrice; + + public function __construct(Order $order, Product $product, $amount = 1) + { + $this->order = $order; + $this->product = $product; + $this->offeredPrice = $product->getCurrentPrice(); + } + } + Performance Considerations ~~~~~~~~~~~~~~~~~~~~~~~~~~ -TODO \ No newline at end of file +Using composite keys always comes with a performance hit compared to using entities with +a simple surrogate key. This performance impact is mostly due to additional PHP code that is +necessary to handle this kind of keys, most notably when using derived identifiers. + +On the SQL side there is not much overhead as no additional or unexpected queries have to be +executed to manage entities with derived foreign keys. \ No newline at end of file