Php 使用即时加载时出现OneToMany关系错误
我为我的用户使用FOSUserBundle,Alice为fixture生成和FOSRestBundle。我有两个实体:User和Shift。一个用户可以有很多班次。以下是我的实体类,这是我尝试以有效用户身份登录时遇到的错误: [2015-08-11 18:50:47]request.CRITICAL:未捕获的PHP异常Symfony\Component\Debug\Exception\ContextErrorException:“注意:未定义的索引:user”位于/var/www/vendor/doctrine/orm/lib/doctrine/orm/Persisters/basicnitypersister.PHP第1758行{“异常”:“[对象] 编辑:BasicEntityPersister的第1758行正在查看mappedBy值。但仍然不确定它有什么问题 编辑:还找到了此相关主题 如果我删除fetch=“EAGER”值,我就可以登录,否则我就会出现上面的错误。我已经搜索了将近3个小时了,对此一无所知 我的桌子如果有帮助:Php 使用即时加载时出现OneToMany关系错误,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我为我的用户使用FOSUserBundle,Alice为fixture生成和FOSRestBundle。我有两个实体:User和Shift。一个用户可以有很多班次。以下是我的实体类,这是我尝试以有效用户身份登录时遇到的错误: [2015-08-11 18:50:47]request.CRITICAL:未捕获的PHP异常Symfony\Component\Debug\Exception\ContextErrorException:“注意:未定义的索引:user”位于/var/www/vendor/
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;
}
}
错误:
在/var/www/vendor/Doctrine/ORM/lib/Doctrine/ORM/Query/Parser.php行483中,$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,然后向上迁移。Dan Mironis上面的注释是问题的一部分。另一部分是我如何定义我的装置。在做了这个更新之后,我在原始帖子中的查询按预期工作。我只是在s上随机分配了一个用户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\Entity\Shift:
移位{1..50}:
用户标识:
中断:
开始时间:
结束时间:
致:
AppBundle\Entity\Shift:
移位{1..20}:
用户:@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')>
shift{21..40}
用户:@user3
中断:
开始时间:
结束时间:
移位{41..60}:
用户:@user4
中断:
开始时间:
结束时间:
移位{61..80}:
用户:@user5
中断:
开始时间:
结束时间:
通过从用户实体中删除fetch=“EAGER”修复了登录问题,但仍然存在一些问题。在我的存储库中处理联接查询时,无论我的leftJoin语句是什么样子,我都会收到以下错误:QueryException::semanticalError(“u,AppBundle:Shift”附近的“第0行,第62列”:错误:Class AppBundle\Entity\Shift没有名为users的关联,因此,鉴于您提供的更新,Shift表中有一个名为user\u id的列,而不是user?的列(谢谢),查询如何选择所有班次,并将每个班次的用户加入到结果中?因此,结果中的每个班次实体都具有用户属性,即用户对象。检查原始帖子,我将尝试执行的查询添加到其底部,并添加了相应的错误。
AppBundle\Entity\Shift:
shift{1..50}:
userId: <numberBetween(1, 2)>
break: <numberBetween(1, 8)>
startTime: <dateTimeBetween('8AM', '12PM')>
endTime: <dateTimeBetween($startTime, '+8 hours')>
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')>