Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Expected argument of type "object or array", "string" given

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];
like image 637
Lughino Avatar asked Dec 18 '25 18:12

Lughino


1 Answers

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! ;-)

like image 162
Lughino Avatar answered Dec 21 '25 16:12

Lughino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!