Reynier
Reynier

Reputation: 2478

sfWidgetFormDoctrineChoice: bug, error or what?

I'm using sfWidgetFormDoctrineChoice to build select elements in a form. This is how I'm using:

// Fill maquinaemisorid
$this->widgetSchema['maquinaemisorid'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingMaquina', 'add_empty' => 'Seleccione una Máquina', 'table_method' => 'fillChoice'));

// Fill idoperador
$this->widgetSchema['idoperador'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingOperador', 'add_empty' => 'Seleccione un Operador', 'table_method' => 'fillChoice'));

// Fill idturno
$this->widgetSchema['idturno'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingTurno', 'add_empty' => 'Seleccione un Turno', 'table_method' => 'fillChoice')); 

For the first widget all is fine and values are taken from DB as should be but for the second one and the third is not working. As you may notice I'm calling a method fillChoice in all widgets table_method. This is the code for the three:

SdrivingMaquinaTable.class.php

public function fillChoice() {
    $id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
    return Doctrine_Core::getTable('SdrivingMaquina')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}

SdrivingOperadorTable.class.php

public function fillChoice() {
    $id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
    return Doctrine_Core::getTable('SdrivingOperador')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}

SdrivingTurno.class.php

public function fillChoice() {
    $id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
    return Doctrine_Core::getTable('SdrivingTurno')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}

The method is the same always just changes the table used for each class. I check the generated queries and all are fine and if I run each on phpMyAdmin for example more than one value is fetched altough in idoperador and idturno just the latest is rendered. This is a very rare behavior and I can't find the error or mistake so I need some help or advice here.

As an adition I tried this in SdrivingRegistrosEmisoresForm.class.php (this is where the widgets are being generated):

$id_empresa = $this->current_user->getSfGuardUserProfile()->getIdempresa();
$choices_operador = Doctrine_Core::getTable('SdrivingOperador')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();

$this->widgetSchema['idoperador'] = new sfWidgetFormSelect(array('choices' => $choices_operador));

And this way works fine but the id for each item fetched isn't right meaning:

id      item     real_id
0       Item1    2
1       Item2    16
2       Item4    3

I talked about this in this topic but didn't get any answer, any help?

Upvotes: 0

Views: 108

Answers (1)

sinhix
sinhix

Reputation: 863

As stated in sfWidgetFormDoctrineChoice, if option *table_method* returns a collection like your methods, it renders choices with options *key_method* and method. By default , they are respectively getPrimaryKey() and *__toString()*.

Can we check you schema.yml and the __toString() ?

As for your last example the option choices of sfWidgetFormSelect should be an array an not a *Doctrine_Collection*

Upvotes: 1

Related Questions