-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

Community
  • 1
  • 1
Salavat
  • 111
  • 1
  • 11
  • 1
    can you show us your entities? Do you've a link between your tables? – Snroki Mar 13 '13 at 12:10
  • Instead of write *solved* inside the title, could you post an answer and accept it? – j0k Mar 13 '13 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 '13 at 07: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. – Andrew Barber Mar 14 '13 at 17:46

2 Answers2

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.

gezpage
  • 441
  • 4
  • 11
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;
Community
  • 1
  • 1
Salavat
  • 111
  • 1
  • 11