0

我使用 FOSUserBundle 和“组”功能

起初在我的 User.php 中有这样的 getter 和 setter

/**
 * 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 bundle 并在

@SonataAdminBundle/Admin/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

在 /FOS/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;
  }
}
4

4 回答 4

4

问题是您getGroups()像这样覆盖:

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

但是你从来没有初始化$this->groups,所以当你调用时$this->getGroups()->contains(),它说那$this->getGroups()是 a 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.

于 2013-04-17T15:42:43.340 回答
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' 或 sth。

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

也许这可以帮助别人!

于 2013-04-25T16:10:12.903 回答
0

我遇到了这个问题,简单的解决方案是将groups变量的声明从public更改为protected:

 /**
  * @var \Doctrine\Common\Collections\Collection
  */
 protected $groups;
于 2014-02-19T03:14:12.067 回答
0

解决方案很简单:在您自己的 userBundle 中从您自己的实体中删除所有 setter 和 getter,并始终为特定捆绑运行教义:生成:实体,而不是全部。

于 2015-08-20T12:16:19.220 回答