Search code examples
symfonyoopdoctrineleft-joinentity

How can I left loin a table / entity with doctrine (Symfony)?


in mySQL this is my table pages:

id  unique_id    name      template     icon    slug
1   jc62368231   Felder                  2     fields

and my table icons:

id  name
1   adjust
2   anchor

In my Controller.php I get the content from Pages via doctrine:

$pages = $this->getDoctrine()->getRepository(Pages::class)->findAll();

Here is my output:

   array:1 [▼
      0 => Pages {#4930 ▼
        -id: 5
        -unique_id: "jc62368231"
        -name: "Felder"
        -template: ""
        -icon: "2"
        -slug: "fields"
      }
    ]

What I want to do is join the table icons to the pages table. To achieve this result:

      array:1 [▼
      0 => Pages {#4930 ▼
        -id: 5
        -unique_id: "jc62368231"
        -name: "Felder"
        -template: ""
        -icon: "anchor"
        -slug: "fields"
      }
    ]

This is my approach:

$pages = $this->getDoctrine()->getRepository(Pages::class)->join();

 /**
  * @return Pages[]
  */

  public function join('icons')
  {
    ->leftJoin('icons.id', 'icon')
    ->getQuery()
    ->getResult()
    ;
  }

But I get this error:

[Semantical Error] line 0, col 60 near 'icon': Error: Class App\Entity\Pages has no association named id


Solution

  • The content of your join() method is a non sense. You're calling method of an object without using an object.

    The join isn't used that way. You have to use entityAlias.memberName

    Try this approach, if you want to use it in your controller :

    $pages = $this->getDoctrine()
                  ->getRepository(Pages::class)
                  ->createQueryBuilder('p') //Alias p for Page
                  ->leftJoin('p.icon', 'i') //you are joining the icon member
                  ->getQuery()
                  ->getResult();
    

    But I suggest you to use a Repository class instead :

    In /src/Repository/PagesRepository.php :

    class PagesRepository extends EntityRepository
    {
        public function GetIcons()
        {
            $query = $this->createQueryBuilder('p') //Alias p for Page
                          ->leftJoin('p.icon', 'i') //you are joining the icon member
                          ->getQuery()
                          ->getResult();
    
            return (query);
        }
    }
    

    In your controller :

    $pages = $this->getDoctrine()->getRepository(Pages::class)->GetIcons();
    

    In your entity :

    /*
     * @ORM\Entity(repositoryClass="App\Repository\PagesRepository")
     */
    class Pages
    {
        //Your entity
    }
    

    An unique page has an unique icon.

    An icon can have many pages.

    From the page context, this is a ManyToOne relation (many pages can have one icon)

    In your entity Page, the definition of the icon should be written that way :

    /**
     * @var \Icons
     *
     * @ORM\ManyToOne(targetEntity="Icons")
     * @ORM\JoinColumn(name="icon", referencedColumnName="id")
     */
    private $icon;
    

    For more informations, check the documentation