Symfony2教义2许多形式不保存实体 [英] Symfony2 Doctrine2 Many To Many Form not Saving Entities
问题描述
用户
和资产
。我希望能够将用户分配给资产页面上的资产。 下面的代码在创建/编辑资产时显示用户列表,但是更改对于用户,复选框不保存,而剩余的数据将被保留。
如果我通过mysql客户端添加一个条目到users_assets,这些更改显示在资产清单。
用户
class User extends BaseUser
{
/ **
* @ ORM\ManyToMany(targetEntity =Asset,inversedBy =users)
* /
private $资产;
}
资产
class Asset
{
/ **
* @ ORM\ManyToMany(targetEntity =User,mappedBy =资产)
* /
private $ users;
}
资产类型
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ form = $ builder
- > add ('users',null,array(
'expanded'=> true,
'multiple'=> true
))
- > getForm();
return $ form;
}
由于某种原因,切换教义映射以使其正常工作:
资产:
/ **
* @ORM \ManyToMany(targetEntity =Adaptive\UserBundle\Entity\User,inversedBy =assets)
* @ ORM\JoinTable(name =user_assets)
* /
私人$用户;
用户:
/ **
* @ ORM\ManyToMany(targetEntity =Splash\SiteBundle\Entity\Asset,mappedBy =users)
* /
私人资产;
现在,当我保存资产时,会保存与之关联的用户。我不需要将builder-> add定义为实体或集合。我只是传递它,它使用映射信息来填写实体信息:
AssetType:
- > ; add('users',null,array('expanded'=>true,multiple=>true))
不完全确定为什么我需要在资产与用户上拥有inversedBy和JoinTable信息,但现在似乎正在工作!
感谢您的建议!!!
I am having some trouble with a many to many relationship. I have Users
and Assets
. I would like to be able to assign users to an asset on the asset page.
The code below displays a list of users when creating/editing an asset, however changes made to the user checkboxes do not save, while the rest of the data is persisted.
If I add an entry to users_assets through the mysql client, these changes are shown in the asset list.
User
class User extends BaseUser
{
/**
* @ORM\ManyToMany(targetEntity="Asset", inversedBy="users")
*/
private $assets;
}
Asset
class Asset
{
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="assets")
*/
private $users;
}
AssetType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$form = $builder
->add('users', null, array(
'expanded' => true,
'multiple' => true
))
->getForm();
return $form;
}
For some reason I had to switch the doctrine mappings to get this to work:
Asset:
/**
* @ORM\ManyToMany(targetEntity="Adaptive\UserBundle\Entity\User", inversedBy="assets")
* @ORM\JoinTable(name="user_assets")
*/
private $users;
User:
/**
* @ORM\ManyToMany(targetEntity="Splash\SiteBundle\Entity\Asset", mappedBy="users")
*/
private $assets;
Now when I save the asset it saves the users associated. I did not need to define builder->add as an entity or collection. I simply pass it null and it uses the mapping info to fill in the entity info:
AssetType:
->add('users', null, array('expanded' => "true", "multiple" => "true"))
Not exactly sure why I needed to have the inversedBy and JoinTable info on the Asset vs The User but it seems to be working now!
Thanks For The Suggestions!!!
这篇关于Symfony2教义2许多形式不保存实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!