Forms Symfony角色作为实体形成问题
我有一个自定义的角色处理程序,例如作为实体和多对多关系:Forms Symfony角色作为实体形成问题,forms,symfony,doctrine,entity,symfony-2.1,Forms,Symfony,Doctrine,Entity,Symfony 2.1,我有一个自定义的角色处理程序,例如作为实体和多对多关系: <?php namespace Digital\UserBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\Role\RoleInterface; /** * Role Entity * * @ORM\Entity * @ORM\Table( name="app_
<?php
namespace Digital\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\RoleInterface;
/**
* Role Entity
*
* @ORM\Entity
* @ORM\Table( name="app_roles" )
*/
class Role implements RoleInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", name="name", unique=true, length=100)
*/
private $role;
/**
* @var Role
* @ORM\ManyToOne(targetEntity="Role")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
**/
private $parent;
/**
* @param $role
*/
public function __construct($role)
{
$this->role = (string)$role;
}
/**
* @return string
*/
public function __toString()
{
return $this->role;
}
/**
* @param $role
*/
public function setRole($role)
{
$this->role = $role;
}
/**
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* @param Role $parent
*
* @return $this
*/
public function setParent(Role $parent)
{
$this->parent = $parent;
return $this;
}
/**
* @return Role
*/
public function getParent()
{
return $this->parent;
}
/**
* @return bool
*/
public function hasParent()
{
return null !== $this->parent;
}
}
可捕获的致命错误:传递给Digital\UserBundle\Entity\User::setRoles()的参数1必须是给定对象的数组类型
任何提示都将非常感谢
这也不起作用:
$builder->add('roles', 'collection', array(
'type' => new RoleType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
或:
我能让这个工作的壁橱是:
public function addRole($role)
{
$this->normaliseRole($role);
// if (!$this->roles->contains($role)) {
if (!$this->hasRole($role)) {
$this->getRolesCollection()->add($role);
}
return $this;
}
$builder->add('roles', 'collection', array( 'options' => array('data_class' => 'Digital\UserBundle\Entity\Role'),
'allow_add' => true, 'allow_delete' => true, 'by_reference' => false));
通过这种方式,我可以提交没有错误的表单,但是任何更改都失败了,因为我必须添加cascade persist,并且该角色已经在角色表中,doctrine正在尝试再次插入它;(您是否尝试过使用
集合
类型?实体
类型意味着一个相关对象
答案是:
$builder->add('rolesCollection', 'entity', array(
'multiple' => true,
'expanded' => true,
'by_reference' => false,
'label' => 'Roles',
'class' => 'Digital\UserBundle\Entity\Role',
));
玩“角色集合”而不是“角色”
/**
* @param Collection $collection
*/
public function setRolesCollection($collection)
{
$this->setRoles($collection->toArray());
return $this;
}
是的,我确实试过:
$builder->add('roles','collection',array('type'=>newroletype(),'allow\u add'=>true,'allow\u delete'=>true,'by\u reference'=>false,));
但这会为separate字段中的每个角色提供输入文本…并要求我在表单提交时尝试将角色插入到app_roles表中,但实际情况并非如此。。
public function addRole($role)
{
$this->normaliseRole($role);
// if (!$this->roles->contains($role)) {
if (!$this->hasRole($role)) {
$this->getRolesCollection()->add($role);
}
return $this;
}
$builder->add('roles', 'collection', array( 'options' => array('data_class' => 'Digital\UserBundle\Entity\Role'),
'allow_add' => true, 'allow_delete' => true, 'by_reference' => false));
$builder->add('rolesCollection', 'entity', array(
'multiple' => true,
'expanded' => true,
'by_reference' => false,
'label' => 'Roles',
'class' => 'Digital\UserBundle\Entity\Role',
));
/**
* @param Collection $collection
*/
public function setRolesCollection($collection)
{
$this->setRoles($collection->toArray());
return $this;
}