public
Description: Lessens HABTM pains
Homepage: http://teknoid.wordpress.com
Clone URL: git://github.com/teknoid/cakephp-habtamable-behavior.git
name age message
file README.md Thu Oct 08 13:36:34 -0700 2009 Updates to README [teknoid]
directory models/ Thu Oct 08 13:18:59 -0700 2009 Refactoring of behavior logic in some places Ab... [teknoid]
README.md

Habtamable behavior (!!! Still alpha, quite a few to-do's !!!)

Save two HABTM models at once

For example you are saving a Location (my sweet penthouse) with all the relevant info and the Address (the actual address of the penthouse).

Example: $this->Location->saveAll($this->data); In the above example Location and Address will be saved with transactional support. (FYI save() works just as well.)

Search accross HABTM models

For example, you can do something like: $this->Location->find('all', array('conditions' => array('Location.is_active' => 1, 'Address.city' => 'Miami')));

(The searching works by "faking" a hasOne bind, and therefore building a join to specify conditions. For the time being only INNER join is supported and the bind is not permament.)

Of couse, Location HABTM Address

Features (so far):

  • If address (HABTM record) exists will use existing ID
  • Will ensure proper relationship saving
  • Model validation
  • Allows you to search across HABTM models

Very simple to use:

Using our example above, in the Location model, all you need to do is to add:
public $actsAs = array('Habtamable');

You can also pass some settings like so:
public $actsAs = array('Habtamable' => array('joinType' => 'LEFT', 'fieldsToSkip' => array('this', 'that'), 'habtmModel' => 'SomeModel'));

'joinType' -> default is "INNER", any valid SQL JOIN type should be supplied. This is used when doing cross-model queries.
'fieldsToSkip' -> do not include these fields, when checking for an exising record. The setting will be merged with defaults set in the behavior.
'habtmModel' -> related HABTM model, if not provided the first model in the association is used.

The above presumes that in the Location model definition you have:
public $hasAndBelongsToMany = array('Address');

The rest is taken care of for you.

Suggestions, improvments, etc. are needed. Contact here