3

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];
7
  • What error do you have? Commented Feb 22, 2013 at 22:14
  • 2
    it is a quiz! "I got an error, and will not show the complete code or point of error" Commented Feb 22, 2013 at 22:21
  • It is in title. Expected argument of type “object or array”, “string” given Commented Feb 22, 2013 at 22:26
  • ... on line ... in file ... Commented Feb 22, 2013 at 22:27
  • I don't understand what you mean by "... on line ... in file ..." Commented Feb 22, 2013 at 22:34

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like propertyPath expects a object or a array. So this line is wrong:

$builder->add('comune', 'hidden', array('property_path' => 'city.id'))

You are passing city.id here and that shouldn't be a string. I'm not a Symfony user and not familiar with the FormBuilder so you have to lookup the manual how to use the property_path option.

Btw. this has nothing to do with doctrine 2, so it would be appropiate to delete that tag.

1 Comment

the 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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.