0

我遇到了一个我不确定如何实施的问题。我正在尝试实现一个基于网络的录音机。目前我有这部分的 3 个实体。Book PageRecording

每一页都映射到一本书,如下所示:

class Page
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Book")
     * @ORM\JoinColumn(name="bookID", referencedColumnName="id")
     */
    protected $bookID;

    /**
     * @ORM\Id
     * @ORM\Column(name="pageNumber", type="integer")
     */
    protected $pageNumber;

我坚持的是每个页面可以有多个录音,我不确定每个录音是否也需要映射到一本书,因为每个页面都已经映射到一本书。

class Recording
{
    /**
     * @ORM\Id
     * @ORM\Column(name="recordingID",type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */

    protected $recordingID;

    /**
     * @ORM\ManyToOne(targetEntity="Page")
     * @ORM\JoinColumn(name="pageID", referencedColumnName="pageNumber")
     */
    protected $pageID;
4

1 回答 1

1

You do not need to map the recordings to the corresponding book entity because it will inherit this relation from its mapping to the page entity. If you have a recording entity and you would want to retrieve its book entity, you would be able to do something along the lines of

$oRecording = $this->_em->find($recording_id);
$oBook = $oRecording->getPage()->getBook();

In the database you should get for the page table

id | page_number | book_id
----------------
1 | 3 |12
2 | 4 |15

And in the recording table

id | page_id
----------------
1 | 2
2 | 2

So recording 2 has page_id = 2 and therefore book_id 15. In this way you do not duplicate data in your database.

于 2013-01-04T08:46:27.630 回答