I'm using Symfony's 1.4 admin generator, and I have a question about a one to many relationship. If A has many B and B one A, I will see a listbox when editing B, that's ok, but I have to add a list (with multiple choices) of B when I edit an A. I have a solution but I don't no if it is the right way : I have modified the A form this way :
class AForm extends BaseAForm
{
public function configure()
{
$this->widgetSchema['b_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'B'));
$this->validatorSchema['b_list'] = new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'B', 'required' => false));
}
public function updateDefaultsFromObject()
{
parent::updateDefaultsFromObject();
if (isset($this->widgetSchema['b_list']))
{
$this->setDefault('b_list', $this->object->B->getPrimaryKeys());
}
}
protected function doSave($con = null)
{
$this->saveBsList($con);
parent::doSave($con);
}
public function saveBsList($con = null)
{
if (!$this->isValid())
{
throw $this->getErrorSchema();
}
if (!isset($this->widgetSchema['b_list']))
{
// somebody has unset this widget
return;
}
if (null === $con)
{
$con = $this->getConnection();
}
$existing = $this->object-Bs->getPrimaryKeys();
$values = $this->getValue('b_list');
if (!is_array($values))
{
$values = array();
}
$unlink = array_diff($existing, $values);
if (count($unlink))
{
$this->object->unlink('Bs', array_values($unlink));
}
$link = array_diff($values, $existing);
if (count($link))
{
$this->object->link('Bs', array_values($link));
}
}
}
I use the same code which is generated for a many-to-namy relation. Is there a better way ?
Teşekkürler
Edit:
Şema
Agency:
columns:
id:
type: integer(4)
primary: true
autoincrement: true
name: { type: string(255), notnull: true }
relations:
Consultants:
refClass: Consultant
Consultant:
columns:
if:
type: integer(4)
primary: true
autoincrement: true
name: { type: string(255), notnull: true }
ref_agency: integer(4)
relations:
Agence:
foreignAlias: Agencies
local: ref_agency
foreign: id
class: Agency