簡體   English   中英

Symfony2-表單收集-更新-ManytoMany-Relation

[英]Symfony2 - Collection of Forms - Update -ManytoMany-Relation

我是Symfony2的新手,現在我對一系列表單有疑問。 我有兩個實體。 實體用戶和實體角色。

實體用戶

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=25, unique=true)
 */
private $username;

/**
 * @ORM\Column(type="string", length=32)
 */
private $salt;

/**
 * @ORM\Column(type="string", length=250)
 */
private $password;

/**
 * @ORM\Column(type="string", length=60, unique=true)
 */
private $email;



/**
 * @ORM\Column(name="is_active", type="boolean")
 */
private $isActive;

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

實體角色

/**
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(name="name", type="string", length=30)
 */
private $name;

/**
 * @ORM\Column(name="role", type="string", length=20, unique=true)
 */
private $role;

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
 * @ORM\JoinTable(name="user_role",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 *      )
 */
private $users;

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

控制者

public function editUserAction($id, Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $user = $em->getRepository('PsoLogBundle:User')->find($id);
    $form =  $this->createForm(new NewUserType(),$user);
    $form->handleRequest($request);
    if (!$user) {
        throw $this->createNotFoundException(
            'No news found for id ' . $id
        );
    }


    if ($form->isValid()) {
        $em->flush();
    $this->get('session')->getFlashBag()->add('notice', 'User updated successfully');
    }

    $build['form'] = $form->createView();

    return $this->render('PsoLogBundle:Security:editUser.html.twig', $build);

}

現在,我有了一個包含用戶和角色的Forms集合,該集合保存在DB for the User中。 當我對角色進行更改時,出現錯誤“執行'UPDATE pso_role SET role =?WHERE id =?時發生異常” 帶有參數[“ ROLE_ADMIN”,2]“

問題是Symfony希望在表上為實體角色進行更改。 實際上,它應該是表中用戶-角色關系的新條目。 我在Google和這里的stackoverflow中進行了搜索,但找不到我必須更改的內容。

我認為問題可能出在formbuilder上

$builder ->add('username','text', array('label' => 'Username')) 
     ->add('email','email', array('label' => 'Email')) 
     ->add('isActive','text', array('label' => 'User aktiv')) 
     ->add('Roles', 'collection', array('type' => new RoleType())) 
     ->add('Submit', 'submit', array( 'attr'=> array ( 'formnovalidate' => 'formnovalidate' )));

可能是add('Roles','collection',array('type'=> new RoleType()))行定義了應該更新的實體嗎? 我可以改變這個嗎

如果我不將其與一組表單一起使用,而僅將其與一種表單一起使用,那么一切都會正常運行,因為這樣只會影響一個實體。 如果有人可以給我提示,我會很高興。

現在我在Formbuilder中使用-> add('roles','entity',array('class'=>'PsoLogBundle:Role','property'=>'role'))進行了嘗試

然后我得到錯誤“屬性“ roles”或方法“ setRoles()”,“ __ set()”或“ __call()”都不存在,並且在類“ Pso \\ LogBundle \\ Entity \\ User”中具有公共訪問權限) “

我試圖在實體用戶中插入二傳手

public function setRoles( ArrayCollection $roles)
{
$this->roles = $roles;
return $this;
}    

然后,我得到錯誤ContextErrorException:可捕獲的致命錯誤:傳遞給Pso \\ LogBundle \\ Entity \\ User :: setRoles()的參數1必須是Pso \\ LogBundle \\ Entity \\ ArrayCollection的實例,是給定的Pso \\ LogBundle \\ Entity \\ Role的實例,在第347行的C:\\ xampp \\ htdocs \\ pso \\ vendor \\ symfony \\ symfony \\ src \\ Symfony \\ Component \\ PropertyAccess \\ PropertyAccessor.php中調用,並在C:\\ xampp \\ htdocs \\ pso \\ src \\ Prc \\ LogBundle \\ Entity \\ User.php第99行

我試圖用解決問題

      'multiple' => false,

就像我在這里找到的

但這並不能改變情況。

我在user.php中添加了角色的設置器

// Important
public function setRoles($roles)
{
    foreach($roles as $p)
    {
        $po = new user_role();

        $po->setUser($this);
        $po->setRoles($p);

        $this->addPo($po);
    }

}   

當我這樣做並生成新的setter和getter時,我沒有錯誤,當我更改用戶角色時,但是在DB中,該條目保持不變。

經過許多小時的搜索,我現在可以正常工作了。 之前的二傳手是對的。 另外,我將formbuilder更改為

->add('roles', 'entity', array('class' => 'PsoLogBundle:Role','property' => 'role','multiple' => true,))

現在,當我提交表單時,角色可以正確更改。 感謝evereybody的回答,以便我能找到正確的方向。 我希望現在一切正常。

問候米莎

您應該在您的用戶實體中:

    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
     * @ORM\JoinTable(name="role_assigned_to_user",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     *      )
     */
    private $roles;

在您的角色實體中:

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

在您的角色構造函數中:

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

而不是收集它將多選:

->add('roles', 'entity', array(
            'class' => 'YourBundle:Role',
            'property' => 'name',
        ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM