我有这个问题,我无法解决。我是 Symfony 2 的新手。我解释了我的问题。我有一个 Usuario 类和 Group 类。我已经覆盖了 RegistrationForm 以向用户添加组。
这是我的代码
<?php
namespace Backend\UsuariosBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// agrega tu campo personalizado
$builder->add('name', 'text', array('label'=>'Nombre Completo de usuario:'));
parent::buildForm($builder, $options);
// agrega tu campo personalizado
$builder->add('roles', 'choice',
array('label' => 'Elige un Rol para el usuario', 'required' => true,
'choices' => array( 1 => 'ROLE_ADMIN', 2 => 'ROLE_USER'),
'multiple' => true))
->add('groups', 'entity', array(
'label' => 'Asociar al Grupo de',
'empty_value' => 'Elija un grupo para el usuario',
'class' => 'BackendUsuariosBundle:Group',
'property' => 'name',
))
;
}
public function getName()
{
return 'mi_user_registration';
}
}
我的 Usuario 课程是这样的:
<?php
namespace Backend\UsuariosBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use \FOS\UserBundle\Model\GroupInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="Usuario")
* @UniqueEntity(fields="username")
* @UniqueEntity(fields="email")
*/
class Usuario extends BaseUser
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=50)
*
* @Assert\NotBlank(message="Por favor, introduce tu nombre.", groups={"Registration", "Profile"})
* @Assert\MinLength(limit="3", message="El nombre es demasiado corto.", groups={"Registration", "Profile"})
* @Assert\MaxLength(limit="50", message="El nombre es demasidado largo.", groups={"Registration", "Profile"})
*/
protected $name;
/**
* @ORM\ManyToMany(targetEntity="Backend\UsuariosBundle\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;
public function __construct() {
$this->groups = new ArrayCollection();
parent::__construct();
}
/**
* Set name
*
* @param string $name
* @return Usuario
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Agrega un rol al usuario.
* @throws Exception
* @param Rol $rol
*/
public function addRole( $rol ) {
$this->roles = array();
if($rol == 1) {
array_push($this->roles, 'ROLE_ADMIN');
}
else if($rol == 2) {
array_push($this->roles, 'ROLE_USER');
}
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add groups
*
* @param \FOS\UserBundle\Entity\Group $groups
* @return Usuario
*/
public function addGroup(GroupInterface $groups)
{
$this->groups[] = $groups;
return $this;
}
/**
* Remove groups
*
* @param \FOS\UserBundle\Model\GroupableInterface $groups
*/
public function removeGroup(GroupInterface $groups)
{
$this->groups->removeElement($groups);
}
/**
* Get groups
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getGroups()
{
return $this->groups;
}
}
表单显示正确,但保存时显示此错误: 属性“组”在类“Backend\UsuariosBundle\Entity\Usuario”中不公开。也许您应该创建方法“setGroups()”?
为什么?一些帮助/想法?
谢谢你的帮助。