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:
-
<table name="states" idmethod="native"><column name="id" type="SMALLINT" required="true" autoincrement="true" primarykey="true">
-
</column><column name="country_id" type="SMALLINT" required="true">
-
<foreign foreigntable="countries">
-
<reference local="country_id" foreign="id">
-
</reference>
-
</foreign>
-
</column><column name="name" type="VARCHAR" size="40">
-
</column><column name="abbrev" type="VARCHAR" size="2">
-
</column></table>
When you do:
-
symfony propel-init-admin backend states States
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):
-
function __toString()
-
{
-
return $this->name;
-
}
That should solve the problem. It's odd that this comes up. And I couldn't find anything on it when googling.
Share this via del.icio.us, digg, email, etc.
Trackback for "Symfony sfException Call to undefined method BaseModel::__toString"
Sorry, comments for this entry are closed at this time.