Skip to content

vlad-ko/cakephp-habtamable-behavior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 

Repository files navigation

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

About

Lessens HABTM pains

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages