Sonata AdminBundleを使用していますが、select listの選択された値を設定したいと思います。
フィールドを持つカテゴリがあります: CategoryID, parentID, tag
。親 ID は NULL にすることができます。
フォームを作成するコード:
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
// If edit -> get tag and id from tag
if($this->subject->getCategoryId() !== null)
{
$tag_current = $this->subject->getCategoryId()->getTag();
$tag_current_id = $this->subject->getCategoryId()->getParentid()->getCategoryid();
}
$formMapper
->add('tag', 'text', array('label' => 'Tag'))
->add('name', 'text', array('label' => 'Naam'))
->add('nameEN', 'text', array('label' => 'Naam Engels', 'required' => false, 'data' => 'test'))
->add('parentcategory', 'entity', array(
'class' => 'DX\MyBundle\Entity\Category',
'empty_data' => null,
'empty_value' => "GEEN PARENT CATEGORIE",
'required' => false,
'data' => $tag_current_id,
'query_builder' => function(EntityRepository $er) use ($tag_current) {
return $er->createQueryBuilder('c')
->where('c.parentid IS NULL')
->andWhere('c.tag != :tag')
->andWhere('c.tag != :tag_current')
->setParameter('tag', 'FOTOGRAAF')
->setParameter('tag_current', $tag_current);
}
))
;
}
ご覧のとおり、結果なしでデータ プロパティを設定しようとしました。ページの読み込み時には常に空の値が選択されます。 NO PARENT CATEGORY
My$tag_current_id
は整数で、この場合は11
. そして、私の選択リストは次のようになります。
<select id="s54ff22c20ca39_parentcategory" name="s54ff22c20ca39[parentcategory]" class="select2-offscreen" tabindex="-1" title="Parentcategory">
<option value="">GEEN PARENT CATEGORIE</option>
<option value="1">THEMA</option>
<option value="11">FREEM SELECTIE</option>
</select>
しかし、まだ選択されていません。私は何を間違っていますか?