注意:未定义的索引:用户问题

时间:2013-12-09 14:23:16

标签: symfony doctrine-orm

为什么我会获得Notice: Undefined index: user in /Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php line 111

查询:

$res = $em->createQueryBuilder()
    ->select(array('u', 'u2p'))
    ->from('HoaxPartnerBundle:FooUsers2FooPartner', 'u2p')
    ->innerJoin('u2p.user', 'u', 'WITH', "u.userid = :userid")
    ->where('u2p.partner = :partner_id')
    ->setParameter('userid', $userid)
    ->setParameter('partner_id', $partner->getId())
    ->getQuery()
    ->getSingleResult();

FooUsers:

/**
 * FooUsers
 *
 * @ORM\Table(name="Foo_users", uniqueConstraints={@ORM\UniqueConstraint(name="search_userid", columns={"userid"})})
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @UniqueEntity(fields="userid")
 */
class FooUsers
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=true)
     */
    private $email;

    /**
     * @var string
     *
     * @Assert\Type("int")
     * @Assert\NotBlank()
     * @Assert\Range(
     *      min = 0,
     *      max = 1,
     *      minMessage = "Min allowed is 0",
     *      maxMessage = "Max allowed is 1"
     * )
     * @ORM\Column(name="status_id", type="integer", nullable=false)
     */
    private $status_id;

    /**
     * @var string
     *
     * @ORM\Column(name="expire_account", type="datetime", nullable=true)
     * @Assert\DateTime()
     * @PartnerAssert\ValidSubscriptionValues()
     */
    private $expire_account;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     * @Assert\DateTime()
     * @Assert\NotBlank()
     */
    private $created;

    /**
     * @var \Hoax\PartnerBundle\Entity\FooUsers2FooPartner
     *
     * @ORM\OneToOne(
     *     targetEntity="\Hoax\PartnerBundle\Entity\FooUsers2FooPartner"
     *     , mappedBy="user"
     * )
     */
    private $toUser;

    /**
     * @ORM\PrePersist
     * @return FooUsers
     */
    public function createdTimeOnPrePersist()
    {
        $this->created = new \DateTime();

        return $this;
    }

    /**
     * @ORM\PrePersist
     * @return FooUsers
     */
    public function statusIdOnPrePersist()
    {
        $this->status_id = 0;

        return $this;
    }

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

    /**
     * Set userid
     *
     * @param string $userid
     * @return FooUsers
     */
    public function setUserid($userid)
    {
        $this->userid = $userid;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set Foo_password
     *
     * @param string $password
     * @return FooUsers
     */
    public function setFooPassword($Foo_password)
    {
        $this->Foo_password = $Foo_password;

        return $this;
    }

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

    /**
     * Get created
     *
     * @return \DateTime 
     */
    public function getCreated()
    {
        // return $this->created->format('Y-m-d H:i:s');
    }

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

        return $this;
    }

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

    /**
     * Set status_id
     *
     * @param int $status_id
     * @return FooUsers
     */
    public function setStatusId($status_id)
    {
        $this->status_id = $status_id;

        return $this;
    }

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

    /**
     * Set expire_account
     *
     * @param \DateTime $expire_account
     * @return FooUsers
     */
    public function setExpireAccount($expire_account)
    {
        $this->expire_account = clone $expire_account;

        return $this;
    }

    /**
     * Get expire_account
     *
     * @return \DateTime 
     */
    public function getExpireAccount()
    {
        return is_object($this->expire_account)
            ? clone $this->expire_account
            // : new \DateTime('now');
            : $this->expire_account;
    }

}

FooUsers2FooPartner:

/**
 * FooUsers2FooPartner
 *
 * @ORM\Table(name="Foo_users2Foo_partner", uniqueConstraints={@ORM\UniqueConstraint(name="uniq_idx", columns={"partner_id", "user_id"})})
 * @ORM\Entity(repositoryClass="Hoax\PartnerBundle\Entity\FooUsers2FooPartnerRepository")
 */
class FooUsers2FooPartner
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \Hoax\PartnerBundle\Entity\FooUsers
     *
     * @ORM\OneToOne(
     *     targetEntity="\Hoax\PartnerBundle\Entity\FooUsers"
     *     , cascade={"persist", "remove"}
     *     , inversedBy="user"
     * )
     * @ORM\JoinColumns({
     *     @ORM\JoinColumn(name="user_id"
     *         , nullable=false
     *         , referencedColumnName="id"
     *     )
     * })
     */
    private $user;

    /**
     * @var \Hoax\PartnerBundle\Entity\FooPartner
     *
     * @ORM\ManyToOne(
     *     targetEntity="\Hoax\PartnerBundle\Entity\FooPartner"
     *     , cascade={"persist", "remove"}
     *     , inversedBy="partner"
     * )
     * @ORM\JoinColumns({
     *     @ORM\JoinColumn(
     *         name="partner_id"
     *         , nullable=false
     *         , referencedColumnName="id"
     *     )
     * })
     */
    private $partner;


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

    /**
     * Set user
     *
     * @param i\Hoax\PartnerBundle\Entity\FooUsers
     * @return FooUsers2FooPartner
     */
    public function setUser(\Hoax\PartnerBundle\Entity\FooUsers $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \Hoax\PartnerBundle\Entity\FooUsers 
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set partner
     *
     * @param \Hoax\PartnerBundle\Entity\FooPartner
     * @return FooUsers2FooPartner
     */
    public function setPartner(\Hoax\PartnerBundle\Entity\FooPartner $partner = null)
    {
        $this->partner = $partner;

        return $this;
    }

    /**
     * Get partner
     *
     * @return \Hoax\PartnerBundle\Entity\FooPartner 
     */
    public function getPartner()
    {
        return $this->partner;
    }
}

我可以在MySQL日志中看到查询,它对我来说很好看:

SELECT ... FROM foo_users2foo_partner v0_ 
  INNER JOIN foo_users v1_
  ON v0_.user_id = v1_.id 
  AND (v1_.userid = 'mgH4MlBg')
  WHERE v0_.partner_id = 1

1 个答案:

答案 0 :(得分:1)

FooUsers2FooPartner中,您的inversedBy不正确,应为toUser,而不是user