utk*_*2k2 5 symfony doctrine-orm
我正在使用两个 ManyToMany 相关实体,即category
和tag
。
实体Tag
(相关详情):
/**
*
* @var string
*
* @ORM\Column(name="tagname", type="string")
*/
protected $tagname;
/**
* @ORM\ManyToMany(targetEntity="Category", mappedBy="tags")
*/
protected $categories;
Run Code Online (Sandbox Code Playgroud)
实体类别(相关详情):
/**
*
* @var string
*
* @ORM\Column(name="CategoryName", type="string",length=200)
*/
protected $categoryname;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="categories")
*/
protected $tags;
Run Code Online (Sandbox Code Playgroud)
我有一个带有选择输入(CategoryType)和多个选择输入(TagType)字段的表单。这两个字段都是 EntityType 字段。TagType 嵌入在 CatgoryType 中。
为此,我无法利用该cascade=persist
功能,而是在我的控制器中手动添加提交的标签。提交时,表单数据会被持久保存在数据库中,没有任何问题。
问题是,提交后,当我在控制器中获取提交的类别(和相关标签)并将其传递给表单时,出现此错误 - Unable to transform value for property path "tagname": Expected a Doctrine\Common\Collections\Collection object.
获取的类别 object( var_dump($category->getTags()->getValues());
)的 var_dump 结果为我提供了一个关联 Tag 对象的数组,其属性为protected 'tagname' => string 'tag1'
。
据我所知,Interface Collection
它与 php 数组非常相似,我的猜测是该tagname
字段需要tagname
ArrayCollection 或 Collection 对象格式中的所有s。我不确定是否有什么具体区别。
但是,我仍然不知道如何在表单中传递已经持久化的类别对象。
以下是 中的categoryname
和tags
字段CategoryType
:
$builder->add('categoryname', EntityType::class, array(
'class' => 'AppBundle:Category',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.id', 'ASC');
},
'choice_label' => 'categoryname',
'expanded' => false,
'multiple' => false,
'label' => 'Choose Category',
));
$builder->add('tags', CollectionType::class, array(
'entry_type' => TagType::class,
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
));
Run Code Online (Sandbox Code Playgroud)
这是 中的嵌入tagname
字段TagType
:
$builder->add('tagname', EntityType::class, array(
'class' => 'AppBundle:Tag',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('t')
->orderBy('t.id', 'ASC');
},
'choice_label' => 'tagname',
'expanded' => false,
'multiple' => true,
'label' => 'Choose Tags',
));
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我的假设是错误的,我需要将tagname
字段设置为数组,或者与任何其他实体创建 ManyToOne 或 ManyToMany 关系,以便它可以是数组集合。只有这样才可以使用标记名作为多选字段。
/**
*
* @var array
*
* @ORM\Column(name="tagname", type="array")
*/
protected $tagname;
Run Code Online (Sandbox Code Playgroud)
或者
/**
* @ORM\ManyToMany(targetEntity="SOME_ENTITY", mappedBy="SOME_PROPERTY")
*/
protected $tagname;
Run Code Online (Sandbox Code Playgroud)
或者
/**
* @ORM\ManyToOne(targetEntity="SOME_ENTITY", mappedBy="SOME_PROPERTY")
*/
protected $tagname;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10356 次 |
最近记录: |