Oth\’s Thoughts


Restrict a record from deletion if it has associated records

Posted in CakePHP by othy on May 30, 2006

ex: Category has Many Article

You don't want to delete a Category if it has related Articles how to do that ?

Well the other way around is possible, you just set 'dependent' to true when you define the association:

$hasMany = array('Article'=>array('dependent'=> true));

now a category is deleted along with all its related articles. beforeDelete is called for every article ( to do some app clean up, ie. delete a dir etc..)

You still want to restrict ?
ok here it is:

if(!$this->Category->Article->hasAny(array('Article.category_id'=> $cat_id)))
{
$this->Category->del($cat_id);
}

Gotchas

This is a good work around, Unfortunetly there are cases where it's useless.
Supose we have another Model Called User.
User hasMany Categories
in the definition of User we've set 'dependent' to true,
which means when we delete a User all its categories will be deleted.
OH NO!! you say ? because those categories might have articles and if they are deleted they will leave orphan childs alone :(.
Tragedy, well not really:

function beforeDelete($id)
{ if(!$this->Article->hasAny(array('Article.category_id'=> $id)))
{
return true;
}
return false;
}

Ahhh I see a smile in your face now baby !
we use Category::beforeDelete so if 'dependent' is to true in any Model that hasMany Categories, thet category won't be deleted and we will save orphan childs, how charitable..Eh..just a last thingy, if the other models that hasMany Categories have exclusive set to true, beforeDelete won't be called.

Well what do you want, Black and White.

Advertisements

othAuth 0.2 released

Posted in CakePHP by othy on May 12, 2006

UPDATE: Please download the package from the snippets here
In the menu:

  • Bug Fixes
  • Some Refractoring
  • New Features

The major new feature is CAKE_ADMIN routing support. That doesn't mean that the old version couldn't work with CAKE_ADMIN, but it was not automatic ( e.g redirections redirect to /controller/admin_action instead of /admin/controller/action ), you can also specify CAKE_ADMIN routing in the permission list of a group, previously you'de specify controller/admin_action, now you can specify admin/controller/action, admin/controller/*, or just admin if want to have access to all CAKE_ADMIN actions

Another important feature is a simplified method for accessing user/group/permission stored in the session, given the fact that othAuth uses a hashkey (the one you specify when you init the component). to get othAuth information you need to do something like:

$auth_sess_name = 'othAuth_'.$this->othAuth->hashkey;
$user_data = $_SESSION[$auth_sess_name];
$user_data is now an array, containing all the information related to the user logged in print_r($user_data); to get an overview of how it's stored
to get the user name, it should be something like $user_data['User']['name']
to get the group name $user_data['Group']['name'];

othAuth now includes a methods to simplify all this, a helper to be used in the view scope is available too: $othAuth->user('name') $othAuth->group('name') etc..