2013-10-23 53 views
0

實體:的Symfony2 + SonataAdminBundle:錯誤上更新用戶的角色(許多到許多)

用戶:

class User implements AdvancedUserInterface, \Serializable 
{ 

    ... 

    /** 
    * @ORM\ManyToMany(targetEntity="Role", inversedBy="users") 
    * @ORM\JoinTable(name="users_roles") 
    * 
    */ 
    private $roles; 

    ... 

} 

角色:

class Role implements RoleInterface 
{ 

    ... 

    /** 
    * @ORM\ManyToMany(targetEntity="User", mappedBy="roles") 
    */ 
    private $users; 

    public function __construct() 
    { 
     $this->users = new ArrayCollection(); 
    } 

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

    ... 

} 

管理員類別:

UsersAdmin:

<?php 

namespace Lan\ConsoleBundle\Admin; 

use Sonata\AdminBundle\Admin\Admin; 
use Sonata\AdminBundle\Form\FormMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Show\ShowMapper; 

use Knp\Menu\ItemInterface as MenuItemInterface; 

class UsersAdmin extends Admin 
{ 
    protected function configureShowField(ShowMapper $showMapper) 
    { 
     $showMapper 
      ->add('id', null, array('label' => 'ID')) 
      ->add('username', null, array('label' => 'Name')) 
      ->add('password', null, array('label' => 'Password')) 
      ->add('email', null, array('label' => 'Mail')) 
      ->add('is_active', null, array('label' => 'Active', 'required' => false)) 
      ->add('roles'); 
    } 

    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
      ->with('General') 
       ->add('username', null, array('label' => 'Name')) 
       ->add('password', null, array('label' => 'Password')) 
       ->add('email', null, array('label' => 'Mail')) 
      ->add('is_active', 'checkbox', array('label' => 'Active', 'required' => false)) 
      ->end() 
      ->with('Roles') 
       ->add('roles', 'sonata_type_model',array('expanded' => true, 'compound' => true, 'multiple' => true)) 
      ->end(); 
    } 
} 

RolesAdmin:

<?php 

namespace Lan\ConsoleBundle\Admin; 

use Sonata\AdminBundle\Admin\Admin; 
use Sonata\AdminBundle\Form\FormMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Show\ShowMapper; 

use Knp\Menu\ItemInterface as MenuItemInterface; 

class RolesAdmin extends Admin 
{ 
    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
      ->add('name', null, array('label' => 'Заголовок')) 
      ->add('role', null, array('label' => 'Роль')); 
    } 
} 

截圖: http://img577.imageshack.us/img577/3565/jyte.png

更新用戶後,我收到此錯誤信息:

FatalErrorException: Error: Call to a member function add() on a non-object in \vendor\sonata-project\doctrine-orm-admin-bundle\Sonata\DoctrineORMAdminBundle\Model\ModelManager.php line 560

我覺得這個錯誤是因爲對象而不是'Role'值被傳遞給函數Role-> __toString()。我怎麼解決這個問題?

+0

我有同樣的問題。你找到答案了嗎? – lvarayut

回答

0

嘗試一些角落找尋此代碼:

protected function configureFormFields(FormMapper $formMapper) 
{ 
    $formMapper 
     ->add('name', null, array('label' => 'Заголовок')) 
     ->add('Product' , 'entity' , array(
        'class' => 'LanConsoleBundle:Role' , 
        'property' => 'name' , 
        'expanded' => true , 
        'multiple' => true ,)) 
} 
+0

不起作用。錯誤:「期望一個Doctrine \ Common \ Collections \ Collection對象500內部服務器錯誤 - TransformationFailedException」:( – TroyashkA

+0

也許我需要datatransformer,但我不知道如何使用SonataORMAdmin來做到這一點http://sonata-project.org /bundles/doctrine-orm-admin/master/doc/reference/form_types_and_transformers.html – TroyashkA