具有多个联接的查询生成器的异常

时间:2016-02-28 17:52:55

标签: php doctrine-orm symfony

我试图通过多个连接来融合一个Entinty,但我有一些问题。

执行selct的代码是:     $ em = $ this-> getEntityManager();

try
    {
      $q=$em->createQueryBuilder('i')
            ->from('AppBundle:Images','i')
            ->innerJoin('AppBundle:ImageGroups', 'g')
            ->innerJoin('AppBundle:Users','u')
            ->select('i')
            ->where('i.id=:iid')
            ->andWhere('u.id=:uid')
            ->setParameter(':uid', $user_id)
            ->setParameter(':iid', $image_id)
            ->setMaxResults(1)
            ->getQuery();

      $data=$q->getOneOrNullResult();

      if(empty($data)) return -3;

      /*Delete Image Stuff*/
      /*End Of: Delete Image Stuff*/

      /*Do some stuff here*/

      return true;

    }
    catch (\Exception $e)
    {
      echo $e->getMessage();
      return false;
    }

但由于某种原因,我得到了这个异常消息:

[Syntax Error] line 0, col 74: Error: Expected Literal, got 'JOIN'

你有什么想法为什么会发生这种情况?

图像实体包含:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="images")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ImagesRepository")
*/
class Images
{
  /**
   * @ORM\Column(type="string", length=60)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="CUSTOM")
   * @ORM\CustomIdGenerator(class="AppBundle\Doctrine\AutoIdGenerate")
   */
  private $id;

  /**
  * Filename of the Image
  * @ORM\Column(type="string", length=100)
  */
  private $name;

  /**
  * Filename of the Thumbnail
  * @ORM\Column(type="string", length=100)
  */
  private $name_small;

  /**
  * ImageGroup og the Image
  * @ORM\ManyToOne(targetEntity="AppBundle\Entity\ImageGroups", inversedBy="images")
  */
  private $group;


    /**
     * Get id
     *
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Images
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set nameSmall
     *
     * @param string $nameSmall
     *
     * @return Images
     */
    public function setNameSmall($nameSmall)
    {
        $this->name_small = $nameSmall;

        return $this;
    }

    /**
     * Get nameSmall
     *
     * @return string
     */
    public function getNameSmall()
    {
        return $this->name_small;
    }

    /**
     * Set group
     *
     * @param \AppBundle\Entity\ImageGroups $group
     *
     * @return Images
     */
    public function setGroup(\AppBundle\Entity\ImageGroups $group = null)
    {
        $this->group = $group;

        return $this;
    }

    /**
     * Get group
     *
     * @return \AppBundle\Entity\ImageGroups
     */
    public function getGroup()
    {
        return $this->group;
    }
}

图像组包含:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="image_groups")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ImageGroupsRepository")
 */
class ImageGroups
{

  /**
   * @ORM\Column(type="string", length=60)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="CUSTOM")
   * @ORM\CustomIdGenerator(class="AppBundle\Doctrine\AutoIdGenerate")
   */
  private $id;

  /**
   * @ORM\Column(type="string", length=20)
   */
  private $groupname;

  /**
  * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Users", inversedBy="imagegroups")
  */
  private $users;


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set groupname
     *
     * @param string $groupname
     *
     * @return ImageGroups
     */
    public function setGroupname($groupname)
    {
        $this->groupname = strip_tags($groupname);

        return $this;
    }

    /**
     * Get groupname
     *
     * @return string
     */
    public function getGroupname()
    {
        return $this->groupname;
    }

    /**
     * Set users
     *
     * @param \AppBundle\Entity\Users $users
     *
     * @return ImageGroups
     */
    public function setUsers(\AppBundle\Entity\Users $users = null)
    {
        $this->users = $users;

        return $this;
    }

    /**
     * Get users
     *
     * @return \ppBundle\Entity\Users
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * Add image
     *
     * @param \AppBundle\Entity\Images $image
     *
     * @return ImageGroups
     */
    public function addImage(\AppBundle\Entity\Images $image)
    {
        $this->images[] = $image;

        return $this;
    }

    /**
     * Remove image
     *
     * @param \AppBundle\Entity\Images $image
     */
    public function removeImage(\AppBundle\Entity\Images $image)
    {
        $this->images->removeElement($image);
    }

    /**
     * Get images
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getImages()
    {
        return $this->images;
    }
}

用户实体包含:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
 */
class Users
{
  /**
   * @ORM\Column(type="string", length=60)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="CUSTOM")
   * @ORM\CustomIdGenerator(class="AppBundle\Doctrine\AutoIdGenerate")
   */
  private $id;

  /**
   * @ORM\Column(type="string", length=15,  unique = true)
   */
  private $username;

  /**
   * @ORM\Column(type="string", length=80)
   */
  private $password;

  /**
   * @ORM\Column(type="string", length=60,  unique = true)
   */
  private $token;

  /**
   * @ORM\Column(type="boolean", options={"default"=false}, nullable=true)
   */
  private $activated=false;

  /**
   * @ORM\Column(type="string", length=30,  unique = true)
   */
  private $email;


    /**
     * Get id
     *
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set username
     *
     * @param string $username
     *
     * @return Users
     */
    public function setUsername($username)
    {
        $this->username = strip_tags($username);

        return $this;
    }

    /**
     * Get username
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set password
     *
     * @param string $password
     *
     * @return Users
     */
    public function setPassword($password)
    {
      $this->password = password_hash ($password,CRYPT_BLOWFISH);

      return $this;
    }

    /**
     * Get password
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set token
     *
     * @param string $token
     *
     * @return Users
     */
    public function setToken($token)
    {
        $this->token = $token;

        return $this;
    }

    /**
     * Get token
     *
     * @return string
     */
    public function getToken()
    {
        return $this->token;
    }

    /**
     * Set activated
     *
     * @param boolean $activated
     *
     * @return Users
     */
    public function setActivated($activated)
    {
        $this->activated = $activated;

        return $this;
    }

    /**
     * Get activated
     *
     * @return boolean
     */
    public function getActivated()
    {
        return $this->activated;
    }

    /**
     * Set email
     *
     * @param string $email
     *
     * @return Users
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }
    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }
}

1 个答案:

答案 0 :(得分:2)

通过关系加入实体(或者从教义2.4开始,你可以使用任意连接,但在这种情况下,你有关系,所以使用它们)并在设置参数时删除:

// I presume it's in a repository class
// so the select and from parts are not required
return $this->createQueryBuilder('i')
    ->innerJoin('i.group', 'g')
    ->innerJoin('g.users', 'u')    
    ->where('i.id = :iid')
    ->andWhere('u.id = :uid')    
    ->setParameter('uid', $user_id)
    ->setParameter('iid', $image_id)
    ->setMaxResults(1)
    ->getQuery()
    ->getOneOrNullResult();

// or if it is not in a repository 
$em->createQueryBuilder()
    ->select('i')
    ->from('AppBundle:Images','i')
 // -> ...

您应该阅读docs