public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); if ($insert && $this->pid == $this->id) { $this->pid = 0; $this->save(false); } if (isset($changedAttributes['pid']) && $this->pid > 0 && empty($changedAttributes['pid'])) { //由顶级菜单变成二级菜单 self::updateAll(['pid' => $this->pid], ['pid' => $this->id]); } }
/** * 保存后检测,分类的父类是否是自己。 * @param bool $insert * @param array $changedAttributes */ public function afterSave($insert, $changedAttributes) { //防止预测ID插入脏数据 if ($insert && $this->pid == $this->id) { $this->pid = 0; $this->save(false); } parent::afterSave($insert, $changedAttributes); }