3.19.2008

Symfony sfException Call to undefined method BaseModel::__toString

Working on some projects in Symfony recently. I ran into a problem when I used the symfony admin generator to create a "backend" module for a table that has a foreign key relationship.

Symfony was looking for me to have a __toString() function in my model so it could create the drop down menu. So for instance, let's say you have tables like countries and states in your schema.xml. Country_id in the states table is a foreign key to the Countries table like so:
[xml]


[/xml]

When you do:
[code]
symfony propel-init-admin backend states States
[/code]
Symfony is going to create the edit and create actions of the States screens with a drop-down menu of countries that the state is a part of. The value of the country drop down is the country_id. the value needs to be set in a __toString() function in your Country propel model. The easiest thing is to just return a simple string name (if that's in your db model):

[php]

function __toString()
{
return $this->name;
}
[/php]

That should solve the problem. It's odd that this comes up. And I couldn't find anything on it when googling.

1 comment:

dan brown said...

Very helpful. Thank you.

Post a Comment