Last active
December 11, 2015 00:18
-
-
Save 77web/4515340 to your computer and use it in GitHub Desktop.
Symfony2のFormで現在困ってるところ。
Able - AbleBakerのOneToMany
Able - AbleCharieのOneToMany
AbleBaker - AbleCharieのManyToMany
があって、Ableのフォーム内でAbleBakerを編集したい。
AbleBakerTypeでのAbleCharieの選択肢を同じAbleに属するものに限定したいのだが、collectionとして使った場合にAbleBakerType内で関連付けられてるAbleBakerエンティティを取得する方法が無いため、どのAbleに属しているか特定できない。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Acme\DemoBundle\EntityRepository\AbleCharieRepository; | |
class AbleBakerType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$ableId = $options['data']->getAble()->getId(); //works fine in isolated use, but does not work in collection use | |
$builder | |
->add('ableCharie', 'entity', array( | |
'class' => 'AcmeDemoBundle:AbleCharie', | |
'multiple' => true, | |
'query_builder' => function(AbleCharieRepository $er) use ($ableId) | |
{ | |
return $er->createQueryBuilder('ac')->where('ac.able = :able')->setParameter('able', $ableId); | |
}, | |
)) | |
; | |
} | |
public function setDefaultOptions(OptionResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'Acme\DemoBundle\Entity\AbleBaker', | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Acme\DemoBundle\Form\Type\AbleBakerType; | |
class AbleType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('ableBakers', 'collection', array('type' => new AbleBakerType())) | |
; | |
} | |
public function setDefaultOptions(OptionResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'Acme\DemoBundle\Entity\Able', | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment