SonataAdminBundle:FatalErrorException:错误:在非对象上调用成员函数add()

时间:2014-01-24 15:51:03

标签: symfony doctrine-orm sonata-admin

我使用SonataAdminBundle创建了一个后端系统,以便管理具有多对多关系的UserRole个实体。 一切顺利,直到我编辑了用户的角​​色。

enter image description here

出现错误:FatalErrorException: Error: Call to a member function add() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/projectName/vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php line 560

我尝试了以下命令:

php app/console doctrine:generate:entities projectBundle

php app/console doctrine:schema:update --force

php app/console cache:clear

没有变化...... :(


实体

用户

<?php
//.. Declare namespace and use

class User implements UserInterface, \Serializable
{

    // ...
    // Declare variables
    public function __construct()
    {
        $this->roles = new \Doctrine\Common\Collections\ArrayCollection();
    }

    function getId()
    {
        return $this->id;
    }

    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getLastName()
    {
        return $this->lastName;
    }

    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setDateCreated($dateCreated)
    {
        $this->dateCreated = $dateCreated;

        return $this;
    }


    public function getDateCreated()
    {
        return $this->dateCreated;
    }


    public function addRole(Role $role)
    {
        // Link each role with the user
        $role->addUser($this);
        $this->roles->add($role);

        return $this;
    }

    public function removeRole(Role $role)
    {
        // Link each role with the user
        $role->removeUser($this);
        $this->roles->removeElement($role);
    }


    public function getRoles()
    {
        return $this->roles->toArray();
    }

    public function getSalt()
    {
        return null;
    }

    public function eraseCredentials()
    {

    }

    public function equals(User $user)
    {
        return $user->getUsername() == $this->getUsername();
    }

    /**
     * @ORM\PrePersist
     */
    public function setDateCreatedValue()
    {
        $this->setDateCreated(new \DateTime());
    }

    function __toString()
    {
        return $this->getName();
    }


    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->username,
            $this->password,
        ));
    }


    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->username,
            $this->password,
            ) = unserialize($serialized);
    }



}

角色

<?php

//.. Declare namespace and use

class Role implements RoleInterface
{

    // .. Declare variables
    public function __construct()
    {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getId()
    {
        return $this->id;
    }

    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDateCreated($dateCreated)
    {
        $this->dateCreated = $dateCreated;

        return $this;
    }

    public function getDateCreated()
    {
        return $this->dateCreated;
    }

    public function addUser(User $user)
    {
        $user->addRole($this);
        $this->users->add($user);

        return $this;
    }

    public function removeUser(User $user)
    {
        $user->removeRole($this);
        $this->users->removeElement($user);
    }

    public function getUsers()
    {
        return $this->users;
    }
    /**
     * @ORM\PrePersist
     */
    public function setDateCreatedValue()
    {
        $this->setDateCreated(new \DateTime());
    }

    function __toString()
    {
        return $this->getName();
    }

    public function getRole(){
        return $this->getName();
    }
}

SonataAdminClass

UserAdmin

<?php

//.. Declare namespace and use

class UserAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('name', 'text', array('label' => 'Name'))
            ->add('lastName', 'text', array('label' => 'Last name'))
            ->add('email') //if no type is specified, SonataAdminBundle tries to guess it
            ->add('username')
            ->add('password')
            ->add('roles','sonata_type_model',array(
                'expanded' => true,
                'compound' => true,
                'multiple' => true,
                'by_reference' => true
            ));
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('name')
            ->add('lastName')
            ->add('roles')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('name')
            ->add('lastName')
            ->add('email')
            ->add('roles','sonata_type_model')
        ;
    }


}

RoleAdmin

<?php

//.. Declare namespace and use

class RoleAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('name', 'text', array('label' => 'Role name'))
            ->add('description', 'text', array('label' => 'Role description'))
        ;
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('name')
            ->add('description')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('name')
            ->add('description')
        ;
    }
}

我一直在努力,但仍然无法弄明白。任何帮助都会非常感激!!

1 个答案:

答案 0 :(得分:0)

尝试更改此

$this->roles->add($role);

到这个

$this->roles[] = $role;