So I have following:
$builder
->add('name')
->add('address', AddressType::class, [
'property_path' => 'address'
])
// TODO: Refactor to create new entities that are without ID
->add('person', CollectionType::class, [
'entry_type' => PersonType::class,
'allow_add' => true,
'property_path' => 'persons'
])
;
I run my functional test as follows with given parameters and everything works as expected. The entity is getting populated with new address relation & 2 new persons inside:
'entity' => [
'name' => 'Test',
'person' => [
[
'firstname' => 'one', 'lastname' => 'two'
], [
'firstname' => 'three', 'lastname' => 'four'
]
],
'address' => [
'street' => 'test str.',
]
]
Now if I run update on the same formtype with following params:
'entity' => [
'id' => 1,
'name' => 'Test upd',
'person' => [
[
'id' => 1, 'lastname' => 'update'
], [
'firstname' => 'new', 'lastname' => 'person'
]
]
]
What it does is: it correctly updates person with id 1. The problem is with the second one with firstname 'new' - instead of creating a new person and updating relationship it just looks for the remaining relation in 'entity' and updates the person with id 2.
Please help me find a way to properly configure this.
UPDATE: I can see the point in what's happening, most probably It is because the inverse side of relation of "entity" is not defined in PersonType. What would be the proper way to set up this relation field in PersonType, so it won't be recursive?
Related
I am using the Symfony serializer to denormalize JSON data and create an entity with relations.
For example:
$data = [
'first' => 'John',
'last' => 'Smith',
'addresses' => [
[
'street' => 'Street',
'city' => 'City',
'state' => 'State'
]
]
];
$person = $this->serializer->denormalize($data, Person::class);
This works GREAT. However, when I need to update person, I am sending the same data, with the object_to_populate flag set:
$data = [
'first' => 'John',
'last' => 'Smith',
'addresses' => [
[
'street' => 'Street',
'city' => 'City',
'state' => 'State'
]
]
];
$entity = $this->serializer->denormalize($data, Person::class, ['object_to_populate' => $person]);
This causes a duplicate key error (I have a unique key set for street+city+state). How can I have Doctrine UPDATE the relations as well, instead of trying to insert new relations?
There exists a bundle to make your life easier. It is called JMSSerializerBundle. It handle Doctrine relations out of the box.
I'm trying to build a checkbox list in Symfony 3.
This code:
$choices = [
'a' => 'fsssssss',
];
$builder->add('memberships', ChoiceType::class, [
'choices' => $choices,
'expanded' => true,
'multiple' => false
]);
Returns the following error: Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php (line 73)
If I add two more options, the error goes away and the field is shown correctly:
$choices = [
'a' => 'fsssssss',
'd' => 'fsssssss',
'g' => 'fsssssss',
];
$builder->add('memberships', ChoiceType::class, [
'choices' => $choices,
'expanded' => true,
'multiple' => false
]);
Actually, there are many combinations that work and many that don't; I failed to see any pattern.
Am I doing something wrong? Is there some sort of cache I need to clear?
Just to close this question:
If you use a form with the data_class option and a form field represents a property of an entity which should get selected by choices you can use EntityType:
$builder->add('memberships', EntityType::class, [
'class' => MyClass::class,
'choices' => $choices,
'expanded' => true,
'multiple' => false
]);
In my Symfony project I use SonataAdminBundle and A2lixTranslationForm + KnpLabsDoctrineBehaviors bundles for multi-language support.
I have Serial and Episode entities which use standard OneToMany and ManyToOne relationship. I display episodes using sonata_type_collection:
->add('episodes', 'sonata_type_collection', [
'by_reference' => false,
],
[
'edit' => 'inline',
'inline' => 'table',
]
)
By default A2lixTranslation bundle displays translatable fields under each other. However, I need to display each translation field of the episode in its own column. In order to achieve this, I created three virtual fields with the same property_path:
->add('episode_title', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'title' => [
'attr' => [
'pattern' => false,
],
'label' => false,
],
],
'exclude_fields' => ['subTitle', 'description'],
'label' => 'Title',
])
->add('episode_subtitle', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'subTitle' => [
'label' => false,
],
],
'exclude_fields' => ['title', 'description'],
'label' => 'Subtitle',
'required' => false,
])
->add('episode_description', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'description' => [
'field_type' => 'textarea',
'label' => false,
],
],
'exclude_fields' => ['title', 'subTitle'],
'label' => 'Description',
'required' => false,
])
As you can see, I display only one field in each a2lix_translations form.
But I faced the following problem: only the last field value (description in this case) is saved when I add new episode using Add new button of the collection form and then clicking Update button of the parent (Serial) form. Wherein already existing episodes are saved correctly if edited.
Could you please point me why is this happening and how to fix the issue. I suppose each translations form values rewrite the previous ones, but why do this work normally when saving existing records?
Or maybe there is another way to display each field in the own column in sonata collection form, this also will be helpful.
Thank you.
Woocommerce not send meta with custom plug-ins in format .json.
What should I add to the file functions.php?
How to forcibly compel him to send some meta?
Sorry for such a simple question, but I have not found a solution for spaces google
Send the following payload to the API:
$data = [
'name' => 'Product Name',
'type' => 'simple',
'regular_price' => '19.95',
'description' => 'Simple Product,
'categories' => [
['id' => 1]
],
'virtual' => true,
'reviews_allowed' => false,
'meta_data' => [
[
'key' => '_custom_meta_1',
'value' => 'value1'
],
[
'key' => '_custom_meta_2',
'value' => 'value2'
]
]
];
the "meta_data" field, should be an array, containing arrays with "key" and "value" attributes.
I have 2 fields in my form. First Name and Email.
$builder->add('firstName', 'text', [
'label' => 'First Name',
'required' => true,
'attr' => [
'data-msg-required' => 'First name is required'
],
'trim' => true])
->add('emails', 'collection', [
'type' => new RegisterEmail,
'required' => true,
'by_reference' => false,
'label' => false,
'options' => [
'attr' => [
'data-msg-required' => 'Email is required'
]
],
]);
However data-msg required is only displayed for first field but not for second one. I know I can add that directly in twig template but is there anyway I can achieve that through form class only.
You should add it to the default value of the RegisterEmail type, with setDefaults()