我使用FOSUserBundle和'group'功能
首先在我的User.php中 有这样的吸气者和设定者
/**
* Add groups
*
* @param \Acme\UserBundle\Entity\Group $groups
* @return User
*/
public function addGroup(\Acme\UserBundle\Entity\Group $groups)
{
$this->groups[] = $groups;
return $this;
}
/**
* Remove groups
*
* @param \Acme\UserBundle\Entity\Group $groups
*/
public function removeGroup(\Acme\UserBundle\Entity\Group $groups)
{
$this->groups->removeElement($groups);
}
/**
* Get groups
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getGroups()
{
return $this->groups;
}
但是,发生此错误
FatalErrorException: Compile Error: Declaration of Acme\UserBundle\Entity\User::addGroup() must be compatible with that of FOS\UserBundle\Model\GroupableInterface::addGroup()
在FOS \ UserBundle \ Model \ GroupableInterface
中public function addGroup(GroupInterface $group);
在我的Acme \ UserBundle \ Entity \ User
中public function addGroup(\Acme\UserBundle\Entity\Group $groups)
{
$this->groups[] = $groups;
return $this;
}
如何调整参数类型或更正此错误?
现在,我已经评论了这三个功能。
但现在,我使用Sonataadmin捆绑包并在
中@ SonataAdminBundle /管理/ UserAdmin.php
protected function configureFormFields(FormMapper $formMapper){
$formMapper
->with('General')
->add('groups','entity',array('property' => 'name',
'multiple' => true,
'class' => 'UserBundle:Group',
))
}
它正确显示此表单,但当我按下提交按钮进行注册时,
显示
Error: Call to a member function contains() on a non-object in ~~~/FOS/UserBundle/Model/User.php line 572
/ pOS/UserBundle/Model/user.php中的有功能这个
public function addGroup(GroupInterface $group)
{
var_dump($group);# I added to check
if (!$this->getGroups()->contains($group)) {
$this->getGroups()->add($group);
}
return $this;
}
var_dump($ group)显示
object(Acme\UserBundle\Entity\Group)#923 (3) { ["id":protected]=> int(2) ["name":protected]=> string(12) "TeacherGroup" ["roles":protected]=> array(0) { } }
我猜它有正确的组信息..
如何解决此问题?
我的整个user.php
// src / Acme / UserBundle / Entity / User.php
namespace Acme\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use FOS\UserBundle\Model\GroupableInterface;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
* @ORM\HasLifecycleCallbacks
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
/**
*
* @ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\Lesson", mappedBy="teacher")
*/
private $LessonAsTeacher;
/**
*
* @ORM\OneToMany(targetEntity="Acme\UserBundle\Entity\Lesson", mappedBy="student*removethis : name of the variable in Lesson.php*")
*/
private $LessonAsStudent;
/**
*
* @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\Sex", inversedBy="sex*removethis : name of the variable in user.php*")
* @ORM\JoinColumn(name="sex", referencedColumnName="id",nullable=false)
*/
private $sex;
/**
* @ORM\ManyToMany(targetEntity="Acme\UserBundle\Entity\Group")
* @ORM\JoinTable(name="fos_user_user_group",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Please enter your first name.", groups={"Registration", "Profile"})
* @Assert\MinLength(limit="0", message="The name is too short.", groups={"Registration", "Profile"})
* @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
*/
protected $firstname;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Please enter your last name.", groups={"Registration", "Profile"})
* @Assert\MinLength(limit="0", message="The name is too short.", groups={"Registration", "Profile"})
* @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
*/
protected $lastname;
/**
* @ORM\Column(type="date")
*/
protected $birthday;
/**
* @var \DateTime
*
* @ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updatedAt", type="datetime")
*/
private $updatedAt;
public function __construct()
{
parent::__construct();
// your own logic
}
public function getFirstname()
{
return $this->firstname;
}
public function setFirstname($name)
{
$this->firstname = $name;
return $this;
}
public function getLastname()
{
return $this->lastname;
}
public function setLastname($name)
{
$this->lastname = $name;
return $this;
}
public function getSex()
{
return $this->sex;
}
public function setSex($sex)
{
$this->sex = $sex;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set age
*
* @param integer $age
* @return User
*/
public function setAge($age)
{
$this->age = $age;
return $this;
}
/**
* Get age
*
* @return integer
*/
public function getAge()
{
return $this->age;
}
/**
* Set birthday
*
* @param \DateTime $birthday
* @return User
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* @return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* @ORM\PrePersist
*/
public function prePersist()
{
$this->createdAt = new \DateTime;
$this->updatedAt = new \DateTime;
}
/**
* @ORM\PreUpdate
*/
public function preUpdate()
{
$this->updatedAt = new \DateTime;
}
/**
* 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;
}
/**
* Get groups
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getGroups()
{
return $this->groups;
}
/**
* Add groups
*
* @param \Acme\UserBundle\Entity\Group $groups
* @return User
*/
// public function addGroup(\Acme\UserBundle\Entity\Group $groups)
// {
// $this->groups[] = $groups;
// return $this;
// }
/**
* Remove groups
*
* @param \Acme\UserBundle\Entity\Group $groups
*/
// public function removeGroup(\Acme\UserBundle\Entity\Group $groups)
// {
// $this->groups->removeElement($groups);
// }
}
我的整个Group.php
namespace Acme\UserBundle\Entity;
use FOS\UserBundle\Entity\Group as BaseGroup;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_group")
*/
class Group extends BaseGroup
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
答案 0 :(得分:4)
问题是你像这样覆盖getGroups()
:
/**
* Get groups
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getGroups()
{
return $this->groups;
}
但您从未初始化$this->groups
,因此当您致电$this->getGroups()->contains()
时,它会说$this->getGroups()
是non-object
,这是真的!
要解决这个问题,你有2个解决方案。
您可以在groups
的构造函数中初始化User
:
public function __construct()
{
parent::__construct();
&this->groups = new ArrayCollection();
}
或者,如果getGroups()
尚未初始化,您可以更改groups
函数以返回新对象:
public function getGroups()
{
return $this->groups ?: $this->groups = new ArrayCollection();
}
在这两种情况下,请不要忘记在use Doctrine\Common\Collections\ArrayCollection;
的开头添加User.php
。
答案 1 :(得分:4)
我得到了同样的错误:
FatalErrorException: Compile Error: Declaration of Acme\UserBundle\Entity\User::addGroup() must be compatible with that of FOS\UserBundle\Model\GroupableInterface::addGroup()
搜索后我发现(FOS Git Issue 988)这个特殊错误是一个Doctrine'Bug'或者某事。
Doctrine根本不应该生成该方法,因为在基类中有addGroup()的实现。 我通过修改
中的签名来解决这个问题public function addGroup(\MyBundleNamespace\Entity\Group $groups)
public function removeGroup(\MyBundleNamespace\Entity\Group $groups)
到
public function addGroup(\FOS\UserBundle\Model\GroupInterface $groups)
public function removeGroup(\FOS\UserBundle\Model\GroupInterface $groups)
这样可以防止Doctrine使用错误的签名生成这两个函数。
版本信息:
Symfony 2.2.1
DoctrineORM 2.2.3
FOSUserBundle 1.3.1
PHP 5.4.4-14
也许这可以帮助别人!
答案 2 :(得分:0)
我遇到了这个问题,简单的解决方案是将groups变量的声明从public更改为protected:
/**
* @var \Doctrine\Common\Collections\Collection
*/
protected $groups;
答案 3 :(得分:0)
解决方案很简单:在您自己的userBundle中删除您自己的实体中的所有setter和getter,并始终运行doctrine:generate:specyfic bundle的实体,而不是所有。