BaseActiveRecord::beforeDelete PHP Method

beforeDelete() protected method

Iterate through relations and remove the records that will break constraints.
protected beforeDelete ( ) : boolean
return boolean
    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();
    }

Usage Example

Esempio n. 1
0
 protected function beforeDelete()
 {
     $idObject = $this->getIdObject();
     //$idInstance = $this->getPrimaryKey();
     // Проверяем есть ли у данного экземпляра зависимые от него экземпляры (например, если данный экземпляр ялвяется родительским для других)
     if (!$this->isAvailableForDelete(false)) {
         return false;
     }
     // обрабатываем файлы экземпляра
     if ($idObject != File::model()->getIdObject()) {
         // TODO
         Yii::app()->db->createCommand('DELETE FROM da_search_data WHERE id_object=:obj AND id_instance=:inst')->execute(array(':obj' => $idObject, ':inst' => $this->getIdInstance()));
         $files = File::model()->byInstance($this)->findAll();
         foreach ($files as $f) {
             $f->delete();
         }
     }
     //$cur->updateObjectInstanceInfo(3);
     return parent::beforeDelete();
 }