I'm investigating symfony 2 framework. In my sample app I have Blog entity and BlogEntry entity. They are connected with one to many relationship. This is BlogEntry class:
class BlogEntry
{
....
private $blog;
....
public function getBlog()
{
return $this->blog;
}
public function setBlog(Blog $blog)
{
$this->blog = $blog;
}
}
I want to add method setBlogByBlogId to BlogEntry class, I see it this way:
public function setBlogByBlogId($blogId)
{
if ($blogId && $blog = $this->getDoctrine()->getEntityManager()->getRepository('AppBlogBundle:Blog')->find($blogId))
{
$this->setBlog($blog);
}
else
{
throw \Exception();
}
}
Is this any way to get doctrine in model class? Is this correct from the point of Symfony 2 MVC architecture? Or I should do this in my controller?