dongqiang8058 2015-04-26 10:42 采纳率: 100%
浏览 164

与Yii和关系的软删除

I have a simple DB with multiple tables and relationships, ie: Article - Category User - Group etc...

I have implemented SoftDelete behavior where there is a Active column and if set to 0, it is considered deleted. My question is simple.

How to i specify in as few places as possible that i only want load Articles that belong to Active categories.

I have specified relationships and default scopes (with Active = 1) condition.

However, when i do findAll(), it returns those Articles that have Active = 1, even if the category it belongs to is Active = 0....

Thank you

Implementation so far: In base class

public function defaultScope()
{
    return array('condition' => 'Active = 1');
}    

in model:

'category' => array(self::BELONGS_TO, 'Category', 'CategoryID'),

'query':

$data =  Article::model()->findAll();

MY SOLUTION

So i decided, that doing it in framework is:

  1. inneficient
  2. too much work
  3. not good as it moves business logic away from database - this is fairly important to save work later on when working on interfaces/webservices and other customizations that should be part of the product.

Overall lesson: Try to keep all business logic as close to database as possible to prevent disrepancies.

First, i was thinking using triggers that would propagate soft delete down the hierarchy. However after thinking a bit more i decided not to do this. The reason is, that this way if I (or an interface or something) decided to reactivate the parent records, there would be no way to say which child record was chain-deleted and which one was deleted before: CASE: Lets say Category and Article. First, one article is deleted. Then the whole category is deleted. Then you realize this was a mistake and you want to undelete the Category. How do you know which article was deleted by deleting category and which one should stay deleted? Yes there are solutions, ie timestamps but ...... too complex, too easy to break

So my solution in the end are: VIEWS. I think i will move away from yii ORM to using views for anything more complex then basic things.

There are two advantages to this for me: 1) as a DBA i can do better SQL faster 2) logic stays in database, in case the application changes/another one is added, there is no need to implement the logic in more then one places

  • 写回答

2条回答 默认 最新

  • duan32342 2015-04-26 11:32
    关注

    You need to specify condition when you are using findAll method. So You should use CDbCriteria for this purpose:

    $criteria=new CDbCriteria;
    $criteria->with = "category";
    $criteria->condition = "category.Active = 1"; //OR $criteria->compare('category.active', 1 true);
    $data =  Article::model()->findAll($criteria);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算