我有表格,我添加开发人员,开发人员与实体团队有关系ManyToOne,并且我可以为开发人员制作selecet团队,但我希望有可能创建没有团队的开发人员我该怎么做?我需要没有团队的开发人员,因为拥有团队的开发人员有角色= ROLE_DEVELOPER,没有团队的开发人员有角色= ROLE_FREELANCER 这是我的逻辑:
if ($form->isValid()) {
$data = $form->getData();
$team_field = $data->getTeams();
$pass = $this->hf->generatePassword();
$entity->setPassword(sha1($pass));
$entity->setAddedBy($this->getUser());
if(!empty($team_field)){
$entity->setRoles('ROLE_DEVELOPER');
}
if(empty($team_field)){
$entity->setRoles('ROLE_FREELANCER');
}
但我需要空($ team_field)
此实体开发人员
class Developer extends CustomUser
{
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, unique=false, nullable=true)
* @Assert\Length(min=3, max=255)
*/
protected $email;
/**
* @ORM\ManyToOne(targetEntity="Teams", inversedBy="developers")
* @ORM\JoinColumn(name="team_id", nullable = true, referencedColumnName="id")
* */
protected $teams;
此实体团队
class Teams extends STeam
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Developer", mappedBy="teams", cascade={"persist", "remove"})
*/
protected $developers;
和这个表格
->add('teams', 'entity', array(
'class' => 'ArtelProfileBundle:Teams',
'property' => 'company',
'required' => 'false',
));
请帮助
答案 0 :(得分:3)
试试这个(http://symfony.com/doc/current/reference/forms/types/entity.html#placeholder和http://symfony.com/doc/current/reference/forms/types/entity.html#empty-data)
->add('teams', 'entity', array(
'class' => 'ArtelProfileBundle:Teams',
'placeholder' => 'Choose an option',
'empty_data' => null,
'property' => 'company',
'required' => 'false'
));
如果用户没有选择任何内容,则会以空值提交。