I have two database tables
- Articles
- Archive
Each article can have multiple records in Archive table.
This is how Archive entity looks like (shown only needed to understand the question)
/**
* Archive
*
* @ORM\Table(name="archive")
* @ORM\Entity
*/
class Archive
{
.....................................................
/**
* @var Articles
*
* @ORM\ManyToOne(targetEntity="Articles")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="article_id", referencedColumnName="id")
* })
*/
private $article;
.....................................................
/**
* Set article
*
* @param Articles $article
* @return Archive
*/
public function setArticle(\Entities\Articles $article = null)
{
$this->article = $article;
return $this;
}
/**
* Get article
*
* @return Articles
*/
public function getArticle()
{
return $this->article;
}
....................................................
}
In this entity there is a reference to Article entity BUT in Article entity there is no reference to Archive entity.
So the question is – do I need reference to Archive in Article entity and what benefits and downsides it will have?
As I understand having a lot of references is bad (http://docs.doctrine-project.org/en/latest/reference/best-practices.html#constrain-relationships-as-much-as-possible). So where is that point where I can tell do I need it or not?
It’s primarily a decision of architecture and convenience.
The first case is mostly just about how you want to represent your data model. Is an article something that should know of its items in archive? Or does only the archive need to know about articles? How is their relation in the data model?
The second case is more about programming convenience: If you need to often access archived items for an article, it can be more convenient to have
$article->getArchives()(or whatever) vs. having to fetch them via a repository or a query. This may go against the data model, so you need to weigh both cases and decide for yourself how to model it and what makes most sense.