protected function beforeDelete()
{
if ($this->auto_update_relations) {
$deleted_classes = array();
$record_relations = $this->getMetaData()->relations;
foreach ($record_relations as $rel_name => $rel) {
$rel_type = get_class($rel);
if ($rel_type == self::MANY_MANY) {
$tbl_name = $rel->getJunctionTableName();
$tbl_keys = $rel->getJunctionForeignKeys();
if (count($tbl_keys) == 2) {
// if the relationship is more complex, this needs to be handled in the record class itself.
$builder = $this->getCommandBuilder();
$criteria = new CDbCriteria();
$criteria->addColumnCondition(array($tbl_keys[0] => $this->getPrimaryKey()));
$cmd = $builder->createDeleteCommand($tbl_name, $criteria);
$cmd->execute();
}
} elseif ($rel_type == self::HAS_MANY) {
if (!$rel->through) {
// if the relationship is 'through', then the delete is handled by that relationship so we ignore it
$rel_cls = $rel->className;
if (!in_array($rel_cls, $deleted_classes)) {
// only need to delete once for any given class as we are ignoring the conditions added to the relation
// beyond the fk relation to this owning object (can't envision a relation based on a different fk relation
// to the same model)
$rel_cls::model()->deleteAllByAttributes(array($rel->foreignKey => $this->getPrimaryKey()));
$deleted_classes[] = $rel_cls;
}
}
}
}
}
return parent::beforeDelete();
}