Skip Navigation

Archive for June, 2006

unbind All associations except some

Saturday, June 3rd, 2006

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:

  1.  
  2. $this->Special->Product->unbindAll(array(‘belongsTo’=>array(‘Category’,‘Manufacturer’),‘hasMany’=>array(‘Image’)));

Neat eh ? here is the code:

  1. function unbindAll($params = array())
  2. {
  3. foreach($this->__associations as $ass)
  4. {
  5. if(!empty($this->{$ass}))
  6. {
  7. $this->__backAssociation[$ass] = $this->{$ass};
  8. if(isset($params[$ass]))
  9. {
  10. foreach($this->{$ass} as $model => $detail)
  11. {
  12. if(!in_array($model,$params[$ass]))
  13. {
  14. $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
  15. unset($this->{$ass}[$model]);
  16. }
  17. }
  18. }else
  19. {
  20. $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
  21. $this->{$ass} = array();
  22. }
  23.  
  24. }
  25. }
  26. return true;
  27. }
  28.  

generateEnumList

Saturday, June 3rd, 2006

as the name suggests, this method ( in your AppModel ) generate a list based on the values of an enum field.

  1. function generateEnumList($fieldName)
  2. {
  3. foreach($this->_tableInfo->value as $field)
  4. {
  5. if($field[‘name’] == $fieldName) {
  6. $enum = $field[‘type’];
  7. break;
  8. }
  9. } foreach(split("’,'", substr($enum, 6, -2)) as $num => $name) { $return[$name] = $name;
  10. } return $return;
  11. }
  12.  

generateNestedList

Saturday, June 3rd, 2006

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

  1.  
  2. function generateNestedList($conditions = null,$sort = null,$indent = ‘–’)
  3. {
  4. $showField = ($this->hasField(‘name’))?‘name’:(($this->hasField(‘title’))?‘title’:$this->displayField);
  5. $this->recursive = ‘-1′;
  6. $cats = $this->findAllThreaded($conditions,array(
  7. $this->name.‘.id’,$this->name.‘.’.$showField,$this->name.‘.parent_id’),$sort);
  8. $glist = $this->_generateNestedList($cats,$indent,$showField); return $glist;
  9. }
  10. function _generateNestedList($cats,$indent,$showField,$level = 0)
  11. {
  12. static $list = array();
  13. for($i = 0, $c = count($cats); $i < $c; $i++)
  14. {
  15. $list[$cats[$i][$this->name][‘id’]] = str_repeat($indent,$level).$cats[$i][$this->name][$showField];
  16. if(isset($cats[$i][‘children’]) &amp;&amp; !empty($cats[$i][‘children’]))
  17. {
  18. $this->_generateNestedList($cats[$i][‘children’],$indent,$showField,$level + 1);
  19. }
  20. }
  21. return $list;
  22. }
  23.