使用预先加载{One}时出现OneToMany关系错误

时间:2015-08-11 19:12:46

标签: php symfony doctrine-orm

我正在为我的用户使用FOSUserBundle,使用Alice进行夹具生成,以及使用FOSRestBundle。我有2个实体:User和Shift。用户可以有很多班次。下面是我的实体类,这是我尝试以有效用户身份登录时出现的错误:

[2015-08-11 18:50:47] request.CRITICAL:未捕获的PHP异常Symfony \ Component \ Debug \ Exception \ ContextErrorException:“注意:未定义的索引:user”at / var / www / vendor / doctrine / orm / lib / Doctrine / ORM / Persisters / BasicEntityPersister.php第1758行{“exception”:“[object]

编辑:BasicEntityPersister的第1758行正在查看mappedBy值。但仍然不确定它有什么问题。

编辑:还发现了这个相关主题Doctrine: Value of mappedBy ignored in OneToMany Association

如果我删除了fetch =“EAGER”值,我可以登录,否则我会收到上述错误。我现在已经冲了好近3个小时了,不能做出正面或反面。

我的表格如果有帮助:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `username_canonical` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email_canonical` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `enabled` tinyint(1) NOT NULL,
  `salt` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `last_login` datetime DEFAULT NULL,
  `locked` tinyint(1) NOT NULL,
  `expired` tinyint(1) NOT NULL,
  `expires_at` datetime DEFAULT NULL,
  `confirmation_token` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `password_requested_at` datetime DEFAULT NULL,
  `roles` longtext COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:array)',
  `credentials_expired` tinyint(1) NOT NULL,
  `credentials_expire_at` datetime DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `phone` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNIQ_8D93D64992FC23A8` (`username_canonical`),
  UNIQUE KEY `UNIQ_8D93D649A0D96FBF` (`email_canonical`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `shift` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user` int(11) NOT NULL,
  `break` double NOT NULL,
  `start_time` datetime NOT NULL,
  `end_time` datetime NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

用户类:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="AppBundle\Data\Repository\UserRepository")
 * @ExclusionPolicy("all")
 */
class User extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     * @Expose
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="phone", type="string", length=16)
     * @Expose
     */
    private $phone;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created_at", type="datetime")
     * @Expose
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="updated_at", type="datetime")
     * @Expose
     */
    private $updatedAt;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Shift", mappedBy="user", fetch="EAGER")
     * @Expose
     */
    private $shifts;


    public function __construct()
    {
        $this->shifts = new ArrayCollection();
        parent::__construct();
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set phone
     *
     * @param string $phone
     * @return User
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

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

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return User
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     * @return User
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * Add shifts
     *
     * @param \AppBundle\Entity\Shift $shifts
     * @return User
     */
    public function addShift(\AppBundle\Entity\Shift $shifts)
    {
        $this->shifts[] = $shifts;

        return $this;
    }

    /**
     * Remove shifts
     *
     * @param \AppBundle\Entity\Shift $shifts
     */
    public function removeShift(\AppBundle\Entity\Shift $shifts)
    {
        $this->shifts->removeElement($shifts);
    }

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

轮班:

<?php

namespace AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;

/**
 * Shift
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Data\Repository\ShiftRepository")
 * @ExclusionPolicy("all")
 */
class Shift
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="user", type="integer")
     * @ORM\ManyToOne(targetEntity="User", inversedBy="shifts", fetch="EAGER")
     * @Expose
     */
    private $user;

    /**
     * @var float
     *
     * @ORM\Column(name="break", type="float")
     * @Expose
     */
    private $break;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="start_time", type="datetime")
     * @Expose
     */
    private $startTime;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="end_time", type="datetime")
     * @Expose
     */
    private $endTime;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created_at", type="datetime")
     * @Expose
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="updated_at", type="datetime")
     * @Expose
     */
    private $updatedAt;


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

    /**
     * Set user
     *
     * @param User $user
     * @return Shift
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set break
     *
     * @param float $break
     * @return Shift
     */
    public function setBreak($break)
    {
        $this->break = $break;

        return $this;
    }

    /**
     * Get break
     *
     * @return float 
     */
    public function getBreak()
    {
        return $this->break;
    }

    /**
     * Set startTime
     *
     * @param \DateTime $startTime
     * @return Shift
     */
    public function setStartTime($startTime)
    {
        $this->startTime = $startTime;

        return $this;
    }

    /**
     * Get startTime
     *
     * @return \DateTime 
     */
    public function getStartTime()
    {
        return $this->startTime;
    }

    /**
     * Set endTime
     *
     * @param \DateTime $endTime
     * @return Shift
     */
    public function setEndTime($endTime)
    {
        $this->endTime = $endTime;

        return $this;
    }

    /**
     * Get endTime
     *
     * @return \DateTime 
     */
    public function getEndTime()
    {
        return $this->endTime;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return Shift
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     * @return Shift
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
}

加入查询我需要执行不起作用,并抛出以下错误:

$shiftsQuery = $this->createQueryBuilder('s1')
            ->addSelect('u')
            ->from('AppBundle:Shift', 's2')
            ->leftJoin('s1.user', 'u')
            ->where('u = :user')
            ->andWhere('s1.user = :user AND s2.user != :user')
            ->andWhere('s2.startTime BETWEEN s1.startTime AND s1.endTime OR s2.endTime BETWEEN s1.startTime AND s1.endTime')
            ->setParameter('user', $user)
            ->groupBy('s1.id')
        ;

        $result = $shiftsQuery
            ->getQuery()
            ->setFetchMode('AppBundle\Entity\User', 'shifts', \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER)
            ->getResult();

错误:

Doctrine \ ORM \ Query \ QueryException :: semanticalError('line 0,col 62 near'u,AppBundle:Shift':Error:Class AppBundle \ Entity \ Shift没有名为user'的对象',对象(QueryException))in /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php 483行

2 个答案:

答案 0 :(得分:1)

在Shift.php实体中,$ user属性没有更正注释,请尝试:

/**
 * @var User
 * @ORM\ManyToOne(targetEntity="User", inversedBy="shifts", fetch="EAGER")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * @Expose
 **/
private $user;

然后制作php doctine:schema:update --force或doctine:migrations:diff然后向上迁移。

答案 1 :(得分:0)

Dan Mironis上面的注释评论是问题的一部分。另一部分是我如何定义我的灯具。进行此更新后,我原始帖子中的查询按预期工作。我只是在轮班上随机分配用户ID,所以我从以下位置更新了它:

AppBundle\Entity\Shift:
    shift{1..50}:
        userId: <numberBetween(1, 2)>
        break: <numberBetween(1, 8)>
        startTime: <dateTimeBetween('8AM', '12PM')>
        endTime: <dateTimeBetween($startTime, '+8 hours')>

为:

的appbundle \实体\移位:     移{1..20}:         user:@ user2         打破:         开始时间:         结束时间:

shift{21..40}:
    user: @user3
    break: <numberBetween(1, 8)>
    startTime: <dateTimeBetween('now', '+3 days')>
    endTime: <dateTimeBetween('now', '+3 days')>

shift{41..60}:
    user: @user4
    break: <numberBetween(1, 8)>
    startTime: <dateTimeBetween('now', '+3 days')>
    endTime: <dateTimeBetween('now', '+3 days')>

shift{61..80}:
    user: @user5
    break: <numberBetween(1, 8)>
    startTime: <dateTimeBetween('now', '+3 days')>
    endTime: <dateTimeBetween('now', '+3 days')>