【问题标题】:Doctrine expected Doctrine\ORM\Query\Lexer::T_WITH, got ','Doctrine 预期 Doctrine\ORM\Query\Lexer::T_WITH, got ','
【发布时间】:2018-06-12 18:42:43
【问题描述】:

在我的虚拟机中的 Ubuntu 服务器上,此查询(参见实体代码后的方法)运行良好,但在我的在线服务器上,Doctrine 显示此错误:

[语法错误] line 0, col 92: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got ','

这是配置文件实体中的代码:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Swagger\Annotations as SWG;

/**
 * Description of  Profile
 *
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProfileRepository")
 * @SWG\Definition(@SWG\Xml(name="profile"))
 */
class Profile
{
    /**
     * @var int $id
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $lastName
     *
     * @ORM\Column(name="lastName", type="string", length=100, nullable=false)
     * @Assert\NotBlank()
     * @Assert\Type(type="string")
     */
    private $lastName;

    /**
     * @var string $firstName
     *
     * @ORM\Column(name="firstName", type="string", length=100, nullable=false)
     * @Assert\NotBlank()
     * @Assert\Type(type="string")
     */
    private $firstName;

    /**
     * @var Company $company;
     *
     * @ORM\ManyToOne(targetEntity="Company")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true)
     */
    private $company;

    /**
     * @var User $user
     *
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
     */
    private $user;

    /**
     * @var ProfileVariety $profileVariety
     *
     * @ORM\ManyToOne(targetEntity="ProfileVariety")
     * @ORM\JoinColumn(name="profileVariety_id", referencedColumnName="id", nullable=true)
     */
    private $profileVariety;
}

配置文件存储库中的代码:

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * Description of ProfileRepository
 *
 * @package AppBundle\Repository
 */
class ProfileRepository extends EntityRepository
{
    public function findByEmail($email)
    {
        return $this->createQueryBuilder('profileByUserEmail')
            ->select('p')
            ->from('AppBundle\Entity\Profile', 'p')
            ->join('AppBundle\Entity\User', 'u')
            ->where('u.id = p.user')
            ->andWhere('u.email = :email')
            ->setParameter(':email', $email)
            ->getQuery()
            ->getResult();
    }
}

你能解释一下这个问题吗?

【问题讨论】:

    标签: php symfony doctrine-orm doctrine php-7


    【解决方案1】:

    你应该直接加入p.user

    ->select('p')
    ->from('AppBundle\Entity\Profile', 'p')
    ->join('p.user', 'u')
    ->where('u.email = :email')
    

    如果你显式加入另一个实体类,Doctrine 期望你用 WITH 关键字指定 JOIN 条件,而不是在 WHERE 条件中:

    ->select('p')
    ->from('AppBundle\Entity\Profile', 'p')
    ->join('AppBundle\Entity\User', 'u', 'WITH', 'u = p.user')
    ->where('u.email = :email')
    

    【讨论】:

    • 值得注意的是,当您定义了Profile 中的关系时,第一个示例是有效的,您定义了user。第二个是在实体中没有定义关系的情况。
    • @Łukasz 确实!
    猜你喜欢
    • 2013-12-26
    • 2012-08-17
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2011-10-13
    • 2020-06-27
    相关资源
    最近更新 更多