3

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?

2
  • I'd say your code is too complicated to doctrine to define the relationship. Not giving any directions here, just saying it's too complicated. It's probably worth to read the manual again how to create a relationship. It's probably even technically correct as you do, but just too verbose. Commented Jun 27, 2012 at 0:53
  • what is to complicated to doctrine? one-to-many relationship? I think, I do not understand you quite well. Commented Jun 27, 2012 at 8:16

1 Answer 1

5

You should use your blogId to query your repository for the blog object before trying to set it on your BlogEntry entity.

Once you have the blog object, you can simply call setBlog($blog) on your BlogEntry entity.

You can either do this in your controller, or you can create a Blog Service (a Blog Manager) to do it for you. I would recommend doing it in a service:

Define your service in Your/Bundle/Resources/config/services.yml:

services
    service.blog:
    class: Your\Bundle\Service\BlogService
    arguments: [@doctrine.orm.default_entity_manager]

Your/Bundle/Service/BlogService.php:

class BlogService
{

    private $entityManager;

    /*
     * @param EntityManager $entityManager
     */
    public function __construct($entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /*
     * @param integer $blogId
     *
     * @return Blog
     */
    public function getBlogById($blogId)

        return $this->entityManager
                    ->getRepository('AppBlogBundle:Blog')
                    ->find($blogId);
    }
}

And then in your controller you can simply go:

$blogId = //however you get your blogId
$blogEntry = //however you get your blogEntry

$blogService = $this->get('service.blog');
$blog = $blogService->getBlogById($blogId);

$blogEntry->setBlog($blog);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer, Cris. But what is the difference between using BlogService and in controller write something like if ($blog = $blogservice->getBlogById($blogId) and using if ($blog = $doctrine->getEntityManager->getRepository('...')->find($blogId)). In both cases we must check blog existance in controller and then assign it to $blogEntry. I want to hide this check and assignment somewhere outside controller class to make my controllers as tiny as possible.
I think that amount of code in your controller is fine, it's concise and clear and gets the job done. The advantage of having the getBlogById() method in your service is that you can re-use it anywhere else in your application with just one line of code.
The difference between the two approaches you mention is the service option doesn't require any repetition of code. If you call doctrine directly in your controller, when you need to do the same thing again somewhere else you will need to duplicate the $doctrine->getEntityManager()... code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.