Symfony2 Expected argument of type "object or array", "string" given
the problem was that when you pass a string or a number, symfony expects an object, so we need to implement a DataTransformer to turn a string into object and vice versa.
http://symfony.com/doc/master/cookbook/form/data_transformers.html
Problem solved! ;-)
Related videos on Youtube
Lughino
Always passionate about of computing and solving logic problems. In recent years I have gained experience in leadership in medium sized team. From the development side, I discovered a love for the MEAN stack, where the coupled javascript server side and NoSQL technologies have been adopted in all my projects.
Updated on May 25, 2022Comments
-
Lughino almost 2 years
I'm trying desperately to save the form with entity City OneToMany Anagrafic. I entered the "property_path" CityType the form and I returned error,
Expected argument of type “object or array”, “string” given
I do not understand what I'm doing wrong!
class Anagrafic { /** * @ORM\ManyToOne(targetEntity="City", inversedBy="anagrafics", cascade={"persist"}) * @ORM\JoinColumn(name="city_id", referencedColumnName="id") */ private $city; //.. //.. class City { /** * @ORM\OneToMany(targetEntity="Anagrafic", mappedBy="city", cascade={"persist"}) */ private $anagrafics; //... //... class CityType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('comune', 'hidden', array('property_path' => 'city.id')) //.. //.. class AnagraficType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('city', new CityType())
EDIT: Sorry for the incomplete information, this is the exception:
CRITICAL - Symfony\Component\Form\Exception\UnexpectedTypeException: Expected argument of type "object or array", "string" given (uncaught exception) at /var/www/MyBusiness0_1/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php line 342 /var/www/MyBusiness0_1/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php at line 342 for ($i = 0; $i <= $lastIndex; ++$i) { if (!is_object($objectOrArray) && !is_array($objectOrArray)) { throw new UnexpectedTypeException($objectOrArray, 'object or array'); } $property = $this->elements[$i];
-
cheesemacfly about 11 yearsWhat error do you have?
-
Fusselchen about 11 yearsit is a quiz! "I got an error, and will not show the complete code or point of error"
-
Lughino about 11 yearsIt is in title.
Expected argument of type “object or array”, “string” given
-
Bram Gerritsen about 11 years... on line ... in file ...
-
Lughino about 11 yearsI don't understand what you mean by "... on line ... in file ..."
-
Bram Gerritsen about 11 yearsNormally when you get a exception, parse error, syntax error etc. there will be the file and linenumber listed on which the error occured. If you post the corresponding code maybe we could help you.
-
Lughino about 11 yearsSorry, I update the question!
-
-
Lughino about 11 yearsthe fact is that if you do not use the "property_path", I receive this error:
An exception occurred while executing 'INSERT INTO City (city, zip, region_id, state_id) VALUES (?, ?, ?, ?)' with params {"1":"8094","2":null,"3":null,"4":null}: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'zip' cannot be null
Try to save a new entity City, but City is a list of cities already predefined and the relationship City OneToMany Anagrafic should not have to create a new entity! What is wrong?