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:
  1. <table name="states" idmethod="native"><column name="id" type="SMALLINT" required="true" autoincrement="true" primarykey="true">
  2. </column><column name="country_id" type="SMALLINT" required="true">
  3. <foreign -key foreigntable="countries">
  4. <reference local="country_id" foreign="id">
  5. </reference>
  6. </foreign>
  7. </column><column name="name" type="VARCHAR" size="40">
  8. </column><column name="abbrev" type="VARCHAR" size="2">
  9. </column></table>

When you do:

CODE:
  1. 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):

PHP:
  1. function __toString()
  2. {
  3. return $this->name;
  4. }

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

Post a Comment

Close
E-mail It