I’m creating a website which will contain multiple pages and multiple images. The relationship is that many pages can have many images. Therefore, I would usually create a join table called page_images which contain the page_id and the image_id. I would also need a position field in the join table so that the images for each page can be ordered.
I’ve been looking at Doctrine today and using Symfony2 entities to create the database. I tried using a many to many relationship using the metadata however this created the join table for me which only consisted of page_id and image_id. I wasn’t sure how to add the position field or any other field, is that possible?
The alternative I assume would be to create a one to many relationship from the page table to the page_images table and also from the images table to the page_images table meaning I would also need a PageImage Entity class.
Can someone please advise what would be the best practice for this scenario? I’ve been looking at http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html but the documentation doesn’t mention if you can add additional fields to the join table.
More info:
class Image {
/**
*
* @ORM\OneToMany(targetEntity="PageImage", mappedBy="image")
*/
private $page_images;
}
class Page {
/**
*
* @ORM\OneToMany(targetEntity="PageImage", mappedBy="page")
*/
private $page_images;
}
class PageImage {
/**
* @var integer
*
* @ORM\ManyToOne(targetEntity="Page", inversedBy="page_images")
* @ORM\JoinColumn(name="page_id", referencedColumnName="id")
*/
private $page;
}
You already have the response : you need to have 2 ManyToOne relations from PageImage entity.
Then you will be able to add extra fields in this entity.