11

Trying to conver my sql queries into dql, seems im doing something wrong.

I need basic joins, something like this.

SELECT a.id, a.title, u.name FROM articles JOIN users ON a.author_id = u.id

Tried

SELECT a.id, a.title, u.name FROM Article a JOIN User u WITH a.author_id = u.id

Getting error

[Semantical Error] line 0, col 34 near 'Article a JOIN': Error: Class 'Article' is not defined.

How should i define? Could you give me please a right solution?

Edit:

Article Entity

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="articles")
 */
class Article
{
    /**
     * @var integer $id
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $title;


    /**
     * @ORM\Column(type="integer", name="author_id")
     */
    protected $authorId;

    /**
     * @ORM\Column(type="datetime", name="creation_date")
     */
    protected $creationDate;

    /**
     * @ORM\Column(type="string", name="short_content")
     */
    protected $shortContent;

    /**
     * @ORM\Column(type="string")
     */
    protected $content;

    public function getId()
    {
        return $this->id;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getAuthorId()
    {
        return $this->authorId;
    }

    public function getCreationDate()
    {
        return $this->creationDate;
    }

    public function getShortContent()
    {
        return $this->shortContent;
    }

    public function getContent()
    {
        return $this->content;
    }
}

User Entity

<?php
namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="bigint")
     */
    protected $phone;

    /**
     * @ORM\Column(type="string")
     */
    protected $gender;

    /**
     * @ORM\Column(type="string")
     */
    protected $about;

    public function getPhone()
    {
        return $this->phone;
    }

    public function getGender()
    {
        return $this->gender;
    }

    public function getAbout()
    {
        return $this->about;
    }
}
Tigran Muradyan
  • 402
  • 1
  • 8
  • 24
  • Does the Article entity even exist? Read [from here on creating an Entity class](http://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class) – Genti Saliu Sep 14 '15 at 18:24
  • Genti, edited my post – Tigran Muradyan Sep 14 '15 at 18:32
  • One more thing to check in case you are using [multiple entity managers](http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html) would be to verify the bundle to entity manager mappings have been set. – Genti Saliu Sep 14 '15 at 18:42
  • Also, you should be referring to your entities in DQL by their namespace, i.e. `AppBundle:Article`, `AppBundle:User`. – Genti Saliu Sep 14 '15 at 18:44
  • Bu there is one entity managers.I just need basic join of 2 tables... – Tigran Muradyan Sep 14 '15 at 18:49
  • SELECT a.id, a.title, u.username FROM AppBundle:Article a JOIN AppBundle:User u WITH a.author_id = u.id Getting Error [Semantical Error] line 0, col 87 near 'author_id = ': Error: Class AppBundle\Entity\Article has no field or association named author_id – Tigran Muradyan Sep 14 '15 at 18:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89618/discussion-between-genti-saliu-and-tigran-muradyan). – Genti Saliu Sep 14 '15 at 18:50

1 Answers1

8

Refer to the entities in your DQL by their namespaces, i.e. AppBundle:Article and AppBundle:User, that should make the error go away.

Use association mapping (old docs) instead of authorId in your entity, this way Doctrine will take care of loading the author:

/** 
 * @ORM\ManyToOne(targetEntity="User") 
 * @ORM\JoinColumn(name="author_id", referencedColumnName="id") 
 **/ 
private $author;

public function getAuthor() {
    return $this->author;
}

public function setAuthor($author) {
   $this->author = $author;
}

Your query would be reduced to:

SELECT a FROM AppBundle:Article a ORDER BY a.creationDate DESC

Once you have loaded an article, you can conveniently access the author:

...
$author = $article->getAuthor();
Genti Saliu
  • 2,643
  • 4
  • 23
  • 43
  • Genti Saliu: Your link for "association mapping" is not working anymore. Would you mind replacing it? Thank you. – k00ni Jun 18 '19 at 15:07