Kira
Kira

Reputation: 147

Left Join in Doctrine and Symfony

Hi I'm trying to do a Left Join in Symfony with Doctrine. I allready tried to do it but my attempts failed. This is the MySQL code what i want to execute.

SELECT alben.name,alben.alben_id
FROM alben
LEFT JOIN video
ON alben.alben_id =video.album

In the Entity "Video" i defined $album like this.

/**
 * @var \Contentuser
 *
 * @ORM\ManyToOne(targetEntity="Album")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="album", referencedColumnName="alben_id")
 * })
 */

I created a VideoRepository in my Entity folder.

$qm = $this->createQueryBuilder()
        ->select("alben.name,alben.alben_id")
        ->from("alben")
        ->leftJoin("video","video","alben.alben_id =video.album");

        return $qm->getQuery()->getResult();

This is the Controller Part:

$em=$this->getDoctrine()->getManager();
    $videoRepo=$em->getRepository('AppBundle:Video');
    $videos=$videoRepo->VideoLeftJoin();

I get this error "Undefined method 'VideoLeftJoin'. The method name must start with either findBy or findOneBy! " So i tried to do add "findBy" but it didn't work.

Thanks in Advance.

Upvotes: 0

Views: 2968

Answers (3)

Jean-Luc Barat
Jean-Luc Barat

Reputation: 1165

Do you miss "WITH" ?

$qm = $this->createQueryBuilder()
->select("alben.name","alben.alben_id") 
->from("alben") 
->leftJoin("video","video", "WITH", "alben.alben_id =video.album");
return $qm->getQuery()->getResult();

You have to tell doctrine there is repository :

in yaml : repositoryClass: AppBundle\Entity\VideoRepo

in annotation :

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\VideoRepository")
 */
class Video
{
    //...
}

Make shure you define VideoLeftJoin method as public in this VideoRepository class.

Upvotes: 1

Akoh  Victor Gutz
Akoh Victor Gutz

Reputation: 620

The error below simply said

Undefined method 'VideoLeftJoin'.

then sugested you use one of the predefined method which is

The method name must start with either findBy or findOneBy!

TO SOLVE THIS ERROR

make sure the function VideoLeftJoin is in your video repository. Below is an example of how to create it.

 public function VideoLeftJoin()
   {
     return $this
        ->createQueryBuilder()
        ->select("alben.name,alben.alben_id")
         ->from("alben")
        ->leftJoin("video","video","alben.alben_id =video.album");
        ->getQuery()
        ->getResult();
   }

Upvotes: 0

Vadim Ashikhman
Vadim Ashikhman

Reputation: 10126

You need to tell doctrine which repository class to use for the corresponding entity class, otherwise doctrine will use its default one. Modify Entity annotation in the Video Entity:

/**
 * @ORM\Entity(repositoryClass="VideoRepositoryClassNameWithNamespace")
 * @ORM\Table
 */
class Video
{
...

Where VideoRepositoryClassNameWithNamespace is the full path to the repository class. You have to define VideoLeftJoin method yourself.

Also, check Database and Doctrine and the Query Builder documentation.

Upvotes: 0

Related Questions