Using master version of Sonata-Admin with Symfony version 2.7.2 results in error:
Argument 1 passed to Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer::__construct() must be an instance of Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList, instance of Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter given
when using sonata_type_model in form as of BC break in Symfony2 2.7.2:
https://github.com/symfony/symfony/commit/6325b4caf6eeeb56cbecffb1af417c2c7f14c1fb
Wrapping variable assigment $this->choiceList in ModelsToArrayTransformer in something like that should do the work:
public function __construct($choiceList)
{
if ($choiceList instanceof LegacyChoiceListAdapter){
$this->choiceList = $choiceList->getAdaptedList();
}else if ($choiceList instanceof ModelChoiceList){
$this->choiceList = $choiceList;
}
else{
throw new \InvalidArgumentException('Argument 1 must be an instance of Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList or Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter');
}
}
confirmed, same problem here with stable version
Same problem, reverting back to 2.7.1 got it working again.
I have the same problem.. i reverted back to symfony 2.7.1 and it's working again... wainting for the fix of this issue!
Same problem
it happened to me on prod with 2.7.2. Worked ok after downgrading to 2.7.1
@Soullivaneuh Can you backport this to 2.2?
Until this is patched you may simply override the ModelType to use a patched ModelsToArrayTransformer.
class PatchedModelType extends \Sonata\AdminBundle\Form\Type\ModelType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['multiple']) {
$builder
->addEventSubscriber(new MergeCollectionListener($options['model_manager']))
->addViewTransformer(new PatchedModelsToArrayTransformer($options['choice_list']), true)
;
} else {
$builder
->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true)
;
}
}
}
class PatchedModelsToArrayTransformer extends \Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer
{
public function __construct($choiceList)
{
if ($choiceList instanceof LegacyChoiceListAdapter) {
$this->choiceList = $choiceList->getAdaptedList();
} else if ($choiceList instanceof ModelChoiceList) {
$this->choiceList = $choiceList;
} else {
throw new \InvalidArgumentException('Argument 1 must be an instance of Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList or Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter');
}
}
}
// services.yml
services:
sonata.admin.form.type.model_choice:
class: MyNamespace\PatchedModelType
tags:
- { name: form.type, alias: sonata_type_model }
This way you avoid changing patching your vendor-files directly (which you should never do) and you still use the latest Symfony 2.7 version. Also, if it eventually gets patched, you can easily disable/remove your patch.
Good luck!
@Soullivaneuh Can you backport this to 2.2?
@hanikesn Only last stable version is supported for maintenance. No backport possible.
Hmm... that means that if we need to update to Symfony 2.7 and put more than a year's work into extending the 2.2 layout, we're now forced to either update everything to work with 2.3's new layout/design or patch all patchable classes/components of Sonata 2.2 like I did above.
Most helpful comment
Until this is patched you may simply override the
ModelTypeto use a patchedModelsToArrayTransformer.This way you avoid changing patching your vendor-files directly (which you should never do) and you still use the latest Symfony 2.7 version. Also, if it eventually gets patched, you can easily disable/remove your patch.
Good luck!