Ask your Symfony questions! Pay money and get answers fast! (more info)

Getting Data in a form class Symfony

  • SOLVED

Hi experts,
i'm very new to symfony. I'm trying to fill my form with data from the database.
Everything works fine. But instead of showing the foreign-key number, i want to show the name of the foreign key. If I try to use methods from doctrine, I alway get an exception:
500 | Internal Server Error | Doctrine_Table_Exception
Unknown method GruppenTable::getSemester


So I looked up in the BaseGruppen.class which methods exists, because i thought of an typo-failure.
But there are listed the following functions:
* @method string getName() Returns the current record's "name" value
* @method integer getSemesterId() Returns the current record's "semester_id" value
* @method Semester getSemester() Returns the current record's "Semester" value
* @method Gruppen setName() Sets the current record's "name" value
* @method Gruppen setSemesterId() Sets the current record's "semester_id" value
* @method Gruppen setSemester() Sets the current record's "Semester" value

So I do something else wrong, here is my Code:

class StudentenForm extends BaseStudentenForm
{
public function configure()
{
$choices = new sfWidgetFormChoice(array(
Doctrine::getTable('Semester')->find(Doctrine::getTable('Gruppen')->getSemesterId())->getName() => Doctrine::getTable('Gruppen')->getName()
));

[.... unset other collumns ....]

}
}



my Action.class (not nessecary i think:
class studentenActions extends sfActions
{
public function executeEdit(sfWebRequest $request){
$this->forward404Unless($student = Doctrine::getTable('studenten')->find(array($request->getParameter('id'))), sprintf('StudentenObjekt mit der id %s existiert nicht.', $request->getParameter('id')));
$this->form= new StudentenForm($student);
$this->edit= true;
/*$this->processForm($request, $this->form);
$this->setTemplate('edit');*/
}
}



Thank you in advance

Answers (3)

2010-05-04

Wojciech Sznapka answers:

Try to use sfWidgetFormDoctrineChoice instead sfWidgetFormChoice. Please paste your schema for this for form object and referenced object too.



class StudentenForm extends BaseStudentenForm
{
public function configure()
{
[...]
$this->widgetSchema['gruppe'] = new sfWidgetFormDoctrineChoice(
array(
'model' => 'Gruppen',
'method' => 'getNameWithSemester'
)
);
[...]
}
}


in Gruppen.class.php (in lib/model/doctrine)

class Gruppen extends BaseGruppen
{
[...]
public function getNameWithSemester()
{
return $this->name . ' ' . $this->Semester->name;
}
[...]
}


this should work :-)

okay, so there is solution:

class Gruppe extends BaseGruppe
{
[..]
public static function getGruppenChoices()
{
$choices = array();
foreach (Doctrine::getTable('Gruppen')->findAll() as $gruppe) {
if (!isset($choices[$gruppe->Semester->name])) {
$choices[$gruppe->Semester->name] = array();
}
$choices[$gruppe->Semester->name][$gruppe->id] = $gruppe->name;
}
return $choices
}
[..]
}

class StudentenForm extends BaseStudentenForm
{
public function configure()
{
[..]
$this->widgetSchema['gruppe'] = new sfWidgetFormChoice(array('choices' => Gruppe::getGruppenChoices()));
[..]
}
}


sorry, there should be Doctrine::getTable('Gruppen')->findAll() instead of ->find()


Martin Baudrechsel comments:

Database schema

Semester:
columns:
name: {type: string(50), notnull: true}

Gruppen:
columns:
name: { type: string(4), notnull: true }
semester_id: { type: integer, notnull: true }
relations:
Semester: { onDelete: RESTRICT, local: semester_id, foreign: id, foreignAlias: fk_gruppen_Semester}


I use the generated forms of Symfony and adapt them for my use just by unsetting some columns or changing inputTextboxes to radiobuttons etc.


Martin Baudrechsel comments:

forget the student table:
Studenten:
actAs:
Timestampable: ~
columns:
matnr: { type: string(15), notnull: true }
anrede: { type: string(1), notnull: false}
nachname: { type: string(50), notnull: true }
vorname: { type: string(50), notnull: true }
email: { type: string(50), notnull: false }
gruppe: { type: integer, notnull: true }
created_by: { type: integer, notnull: true }
updated_by: { type: integer, notnull: true }
relations:
Gruppe: { onDelete: RESTRICT, local: gruppe, foreign: id, foreignAlias: fk_studenten_Gruppe}



as you can see it is like this: student has a group and a group belongs to a semester


Martin Baudrechsel comments:

no i get a dropdown, that doesn't look like the attached image and the preselected values are wrong if i want to edit a student.

look at the image attached:

Instead of Europe i want to have the name of a semester
and instead of the countrys i want to display the name of the group.

If I click on "edit student" i want to have the choice preselected by the foreign-key of the group. so the workflow should be something like this

$sem= getSemesterByGivenGroupId();
$sem->getName();

$groups= getTable();
$choices = array(
$sem->getName() => array( $groups->getNameBySemesterId($sem->getId()) => '$groups->getNameBySemesterId($sem->getId()))
);

$w = new sfWidgetFormChoice(array('choices' => $choices));


i hope this pseudocode isn't to weirred ;)