Symfony实体加入问题

时间:2017-06-29 17:10:08

标签: php symfony doctrine

我在执行此查询时遇到左连接问题:

$result = $this->createQueryBuilder('a')
                ->select(['a','u'])
                ->leftJoin(User::class, 'u', Join::WITH, 'a.user_id = u.id')
                ->where('a.api_key = :api_key')
                ->setParameter('api_key', $key)
                ->getQuery()
                ->getArrayResult();

这是错误:

[Semantical Error] line 0, col 84 near 'user_id = u.id': Error: Class AppBundle\Entity\ApiKey has no field or association named user_id

user_id作为关联名称存在,所以我不确定为什么会看到此错误?

以下是我的实体:

user.php的

/**
 * User
 *
 * @ORM\Table("users")
 * @ORM\Entity
 * @UniqueEntity(
 *     fields={"username"},
 *     message="This username is already in use."
 * )
 * @UniqueEntity(
 *     fields={"email"},
 *     message="This email address is already in use."
 * )
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 */
class User
{
/**
 * @var integer
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(name="username", type="string", length=225, unique=true)
 * @Assert\NotBlank(message="Please enter a username.")
 * @Assert\Length(
 *     min = 2,
 *     max = 225,
 *     minMessage = "Your username must be at least {{ limit }} characters",
 *     maxMessage = "Your username cannot be longer than {{ limit }} characters"
 * )
 */
protected $username;

/**
 * @ORM\Column(name="email", type="string", length=225, unique=true)
 * @Assert\NotBlank(message="Please enter a email.")
 * @Assert\Email(message = "The email '{{ value }}' is not a valid email.")
 */
protected $email;

/**
 * @ORM\Column(name="salt", type="text")
 */
protected $salt;

/**
 * @ORM\Column(name="password", type="string", length=225)
 * @Assert\NotBlank(message="Please enter a password.")
 * @Assert\Length(
 *     min = 7,
 *     minMessage = "Your password must be at least {{ limit }} characters long."
 * )
 */
protected $password;

/**
 * @Assert\NotBlank(message="Please enter a confirmation password.")
 */
protected $confirmationPassword;

/**
 * @ORM\Column(name="roles", type="json_array")
 */
protected $roles;

/**
 * @ORM\OneToOne(targetEntity="ApiKey", mappedBy="user", cascade={"all"}, fetch="LAZY")
 */
protected $apiKey;

/**
 * @ORM\OneToOne(targetEntity="Nation", mappedBy="user", cascade={"all"}, fetch="LAZY")
 */
protected $nation;
}

ApiKey.php

/**
 * ApiKey
 *
 * @ORM\Table("api_key")
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ApiKeyRepository")
 */
class ApiKey
{
/**
 * @var string
 * @ORM\Column(name="api_key", unique=true, type="string", length=225)
 */
protected $api_key;

/**
 * @var \DateTime
 * @ORM\Column(name="expires", type="datetime")
 */
protected $expires;

/**
 * @ORM\OneToOne(targetEntity="User", inversedBy="apiKey")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * @ORM\Id
 */
protected $user;
}

1 个答案:

答案 0 :(得分:1)

类AppBundle \ Entity \ ApiKey没有名为user_id的字段或关联,但它有一个user字段。

因此,您的查询应如下所示:

$result = $this->createQueryBuilder('a')
                ->select(['a','u'])
                ->leftJoin('a.user', u)
                ->where('a.api_key = :api_key')
                ->setParameter('api_key', $key)
                ->getQuery()
                ->getArrayResult();