2

I'm not sure if it's that I'm not doing correctly but this is giving me an error:

I have 2 Entities: Task and TaskUser. They are connected by a onetoMany.

What I want to do is this:

foreach($tasks as $task){
echo $task->getTitle;
echo $task->getTaskUser()->getFirstName();
}

This is my query in the Task Repository:

    public function findTasksByUsers($user = false)
{

    if($user){

    $qb = $this->_em->createQueryBuilder();

    $qb
    ->select('t', 'tu')
    ->from('\Entities\Task', 't')
    ->leftJoin('\Entities\TaskUser', 'tu',  \Doctrine\ORM\Query\Expr\Join::WITH, 't.id = tu.task')
    ->where('tu = :user')
    ->setParameter('user', $user)
    ->orderBy('t.createDate', 'DESC');

    return $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);

    }

}

When I do this loop:

$tasks = $this->em->getRepository('\Entities\Task')->findTasksByUsers($user);

foreach($tasks as $task){
        echo $task->getTitle();
    }

I get the the title of the first task, and then an error like this:

Title of first task
Fatal error: Call to undefined method Entities\TaskUser::getTitle() in D:\sites\db\application\controllers\TasksController.php on line 35

Any idea why this is happening? Thanks!

raygo
  • 1,348
  • 5
  • 18
  • 40
  • Looks like it thinks the type of the returned object is `TaskUser` and not `Task`, and `TaskUser` doesn't have a method `getTitle()`.. – Jim Garrison Feb 21 '15 at 20:48
  • Exactly. But I don't know why the first loop it does give me the title – raygo Feb 21 '15 at 20:49
  • How did you initialize `$tasks` in the first example? – Jim Garrison Feb 21 '15 at 20:50
  • Do you really want a "fetch" join here? Maybe it's confused because the where-clause refers to `tu` while the select clause mentions both entities, resulting in a fetch-join instead of a regular join. Just a guess. – Jim Garrison Feb 21 '15 at 21:05

1 Answers1

2
$qb->select('t', 'tu')

the issue is here as you're selecting both entities.

If you want only Task entity modify your DQL as follows

$qb->select('t')

However, to me, you could only procede that way (in your controller; if you aren't into controller, use DI to access entity manager)

//retrieve all TaskUser object
$em = $this->getDoctrine()->getManager();
$tu_repo = $em->getRepository('YourBundleName:TaskUser');
$tu_array_collection = $tu_repo->findBy(array('user'=>$user));
foreach ($tu_array_collection as $tu) {
  $first_name = $tu->getFirstName();
  foreach ($tu->getTasks() as $task) {
    echo $first_name;
    echo $task->getTitle();
  }
}

of course you may need to adapt your code with right findBy array, and with correct methods from TaskUser entity

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • Didn't know I could do findBy and use a property from an associated entity. How about if for some reason Task and TaskUser have a property called "user"? – raygo Feb 23 '15 at 18:47
  • @raygo: can't understand your question. Could you be more precise? – DonCallisto Feb 23 '15 at 22:38