Discussion:
Doctrine left join doesnt prevent lazy loading.
Feras Salim
13 years ago
Permalink
Hi everyone,
I just started exploring Symfony2 and I am amazed how many great features
it has. I started doing the blog tutorial at:
http://tutorial.symblog.co.uk/
but using version 2.1 instead of 2.0

My problem is that i have the following entity for Blog:

/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="blog")
*/
protected $comments;

and the following in the Comment entity:

/**
* @var string $blog
*
* @ORM\ManyToOne(targetEntity="Blog", inversedBy="comments")
* @ORM\JoinColumn(name="blog_id", referencedColumnName="id")
*/
private $blog;


I created the function to get the latest blogs and join the comments so i
dont have to lazy load comments for each blog and avoid multiple calls to
the comments table like so:

$qb = $this->createQueryBuilder('b')
->select('b')
->leftJoin('b.comments', 'c')
->addOrderBy('b.created', 'DESC');


but when i run in the twig template the result from that query as:*blog.comments
*I got the first query to retrieve the blogs correct with the join in it
but then instead of using the joined value for comments i get a call to the
comments table for each blog entry? How do I bypass that functionality for
that certain case but keep the relationship so i can get all comments for a
blog from the view blog page?

Thanks!
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Christophe COEVOET
13 years ago
Permalink
...
This is logical: you are not asking to have the comments in the result
set. It should be ->select('b', 'c'), or ->addSelect('c')
--
Christophe | Stof
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Feras Salim
13 years ago
Permalink
Thanks a lot man, that fixed it!
...
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Benoît Boucart
13 years ago
Permalink
Thanks. Do you know how it comes I get only one result when I try your
solution?

For example:
$dql = "SELECT u, fb FROM SportBundle:Project u LEFT JOIN
u.feedbacks fb WHERE u.id = :project_id ORDER BY fb.creation_date ASC";
...
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Loading...