unbind All associations except some
This is a very handy method ( in your AppModel ).
Many times you have a model that has a lot of associations, and for some stuff, you don’t want the overhead of all the associated data. For that you use $this->recursive = ‘-1′, this turns all associations completly. or you can use calls to unbindModel. but then sometimes, you don’t know what associations the model has ( ie. a piece of code you wrote that is used by other people, a component most likely ).
for that there is unbindAll, this method will shut off all associations except the ones passed in params, usage:
$this->Special->Product->unbindAll(array('belongsTo'=>array('Category','Manufacturer'),'hasMany'=>array('Image')));
Neat eh ? here is the code:
function unbindAll($params = array())
{
foreach($this->__associations as $ass)
{
if(!empty($this->{$ass}))
{
$this->__backAssociation[$ass] = $this->{$ass};
if(isset($params[$ass]))
{
foreach($this->{$ass} as $model => $detail)
{
if(!in_array($model,$params[$ass]))
{
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
unset($this->{$ass}[$model]);
}
}
}else
{
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
$this->{$ass} = array();
}
}
}
return true;
}
.
generateEnumList
as the name suggests, this method ( in your AppModel ) generate a list based on the values of an enum field.
function generateEnumList($fieldName)
{
foreach($this->_tableInfo->value as $field)
{
if($field['name'] == $fieldName) {
$enum = $field['type'];
break;
}
} foreach(split("','", substr($enum, 6, -2)) as $num => $name) { $return[$name] = $name;
} return $return;
}
generateNestedList
UPDATE: 15/03/2007 snippet updated. Before it was only working with tables that has a field named ‘name’ now it supports name, title and the value of Model::displayField
also introduced conditions which makes it a bit incompatible with the old unless you call it without params since a new param has been introduced. If anyone has a tip on how to indent code properly in WP please let me know.
This method does the same thing as generateList, except that it’s used to return a hierachy
its main use is with select tags
NOTE: generateNestedList assumes a parent_id field, and a record with parent_id = 0 is a root field. put the two methods in your AppModel
function generateNestedList($conditions = null,$sort = null,$indent = ‘–’)
{
$showField = ($this->hasField(’name’))?’name’:(($this->hasField(’title’))?’title’:$this->displayField);
$this->recursive = ‘-1′;
$cats = $this->findAllThreaded($conditions,array(
$this->name.’.id’,$this->name.’.’.$showField,$this->name.’.parent_id’),$sort);
$glist = $this->_generateNestedList($cats,$indent,$showField); return $glist;
}
function _generateNestedList($cats,$indent,$showField,$level = 0)
{
static $list = array();
for($i = 0, $c = count($cats); $i < $c; $i++)
{
$list[$cats[$i][$this->name]['id']] = str_repeat($indent,$level).$cats[$i][$this->name][$showField];
if(isset($cats[$i]['children']) && !empty($cats[$i]['children']))
{
$this->_generateNestedList($cats[$i]['children'],$indent,$showField,$level + 1);
}
}
return $list;
}