php - Softdelete with Yii and relations -


i have simple db multiple tables , relationships, ie: article - category user - group etc...

i have implemented softdelete behavior there active column , if set 0, considered deleted. question simple.

how specify in few places possible want load articles belong active categories.

i have specified relationships , default scopes (with active = 1) condition.

however, when findall(), returns articles have active = 1, if category belongs active = 0....

thank you

implementation 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 decided, doing in framework is:

  1. inneficient
  2. too work
  3. not moves business logic away database - important save work later on when working on interfaces/webservices , other customizations should part of product.

overall lesson: try keep business logic close database possible prevent disrepancies.

first, thinking using triggers propagate soft delete down hierarchy. after thinking bit more decided not this. reason is, way if (or interface or something) decided reactivate parent records, there no way child record chain-deleted , 1 deleted before: case: lets category , article. first, 1 article deleted. whole category deleted. realize mistake , want undelete category. how know article deleted deleting category , 1 should stay deleted? yes there solutions, ie timestamps ...... complex, easy break

so solution in end are: views. think move away yii orm using views more complex basic things.

there 2 advantages me: 1) dba can better sql faster 2) logic stays in database, in case application changes/another 1 added, there no need implement logic in more 1 places

you need specify condition when using findall method. should use cdbcriteria purpose:

$criteria=new cdbcriteria; $criteria->with = "category"; $criteria->condition = "category.active = 1"; //or $criteria->compare('category.active', 1 true); $data =  article::model()->findall($criteria); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -