Odd many-to-many behavior in symfony 1.4 forms with Doctrine
I am encountering unexpected behavior with form processing, symfony 1.4 with Doctrine. I have a table which has a many-to-many relationship to itself:
Person:
[...]
relations:
Teachers:
class: Person
refClass: PersonTeacher
local: student_id
foreign: teacher_id
foreignAlias: Students
PersonTeacher:
columns:
teacher_id: { type: integer, primary: true }
student_id: { type: integer, primary: true }
In Person, I have a method which gets called in PersonForm for use in a custom widget:
public function getTeachersOrderByName()
{
$q = Doctrine::getTable('Person')
->createQuery('t')
->leftJoin('t.Students s')
->where('s.id = ?', $this->getId())
->orderBy('t.last_name, t.first_name ASC');
return $q->execute();
}
Without that method call, Doctrine correctly INSERTs and DELETEs records from PersonTeacher. But when I call that method from the PersonForm, the following UPDATE is run upon saving the form [taken from the logs]:
Doctrine_Connection->update(object('PersonTeacherTable'), array('student_id' => '9'), array('teacher_id' => '9', 's开发者_开发问答tudent_id' => '6'))
which throws
SQLSTATE[23000]: Integrity constraint violation: 19 columns teacher_id, student_id are not unique
Backtrace:
at Doctrine_Connection_Statement->execute(array('9', '9', '6'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php line 1042 ...
at Doctrine_Connection->exec('UPDATE person_teacher SET student_id = ? WHERE teacher_id = ? AND student_id = ?', array('9', '9', '6'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php line 653 ...
at Doctrine_Connection->update(object('PersonTeacherTable'), array('student_id' => '9'), array('teacher_id' => '9', 'student_id' => '6'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/UnitOfWork.php line 534 ...
at Doctrine_Connection_UnitOfWork->update(object('PersonTeacher'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/UnitOfWork.php line 89 ...
at Doctrine_Connection_UnitOfWork->saveGraph(object('PersonTeacher'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php line 1718 ...
at Doctrine_Record->save(object('Doctrine_Connection_Sqlite'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Collection.php line 910 ...
at Doctrine_Collection->save(object('Doctrine_Connection_Sqlite'), 1)
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/UnitOfWork.php line 136 ...
at Doctrine_Connection_UnitOfWork->saveGraph(object('Person'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php line 1718 ...
at Doctrine_Record->save(object('Doctrine_Connection_Sqlite'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Collection.php line 910 ...
at Doctrine_Collection->save(object('Doctrine_Connection_Sqlite'), )
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/UnitOfWork.php line 445 ...
at Doctrine_Connection_UnitOfWork->saveAssociations(object('Person'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/UnitOfWork.php line 142 ...145.
at Doctrine_Connection_UnitOfWork->saveGraph(object('Person'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php line 1718 ...
at Doctrine_Record->save(object('Doctrine_Connection_Sqlite'))
in SF_SYMFONY_LIB_DIR/form/addon/sfFormObject.class.php line 161 ...
at sfFormObject->doSave(object('Doctrine_Connection_Sqlite'))
in SF_ROOT_DIR/lib/form/doctrine/base/BasePersonForm.class.php line 116 ...
at BasePersonForm->doSave(object('Doctrine_Connection_Sqlite'))
in SF_SYMFONY_LIB_DIR/form/addon/sfFormObject.class.php line 130 ...
This only occurs when the many-to-many refers to the same table. I also have a one-to-many referring to its own table, and it works correctly.
This ring any bells?
This is a known bug in Doctrine and/or symfony, over a year old:
- http://trac.symfony-project.org/ticket/5849
- http://trac.symfony-project.org/ticket/9398
- http://www.doctrine-project.org/jira/browse/DC-329
精彩评论