-1

I have a City Add form:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', null, array(
            'label' => 'City',
        ));
        $builder->add('country', 'genemu_jqueryautocompleter_entity', array(
            'class' => 'X\tBundle\Entity\Country',
            'property' => 'name',
            'route_name' => 'ajax_country',
            'required' => true,
            'label' => 'Country',
        ));
    }

Country is an Entity. When I submit this form - S2 returns an error: cannot save because cannot convert Object to String

For this I'm using magic method __toString() and it returns (string)$this->getId();

But I'm not sure - is this right?

Desicion: Symfony/Doctrine: "Catchable Fatal Error: Object of class <type> could not be converted to string" when persisting

4
  • 1
    can you show us your entities? Do you've a link between your tables?
    – Snroki
    Mar 13, 2013 at 12:10
  • Instead of write solved inside the title, could you post an answer and accept it?
    – j0k
    Mar 13, 2013 at 14:02
  • @j0k, I can not post answer on my question in 6 housr after post, so i wrote Solved in the title. And set link to decision into content.
    – Salavat
    Mar 14, 2013 at 7:23
  • @Salavat We do not use titles to indicate that a question's problem is solved. Answer acceptance is the correct way to do that. It's OK if you can't do it yet (though I expect you can now); just do it when the time comes. In the meantime, it's OK if you get more answers. Those might help other people, too. Mar 14, 2013 at 17:46

2 Answers 2

1

The best thing would be to create a FormType for your Country entity and embed it in the main Form. See How to Embed a Collection of Forms from the Cookbook to explain exactly how to do this.

What you are trying to do is possible, but it makes things get messy very quickly.

0
1

Answer: Symfony/Doctrine: "Catchable Fatal Error: Object of class <type> could not be converted to string" when persisting

So, we use the next thing:

/**
 * @var X\tBundle\Entity\Country;
 *
 * @ORM\ManyToOne(targetEntity="X\tBundle\Entity\Country")
 * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
 */
public $country;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.