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
forked from vlad-ko/cakephp-habtamable-behavior
-
Notifications
You must be signed in to change notification settings - Fork 1
Lessens HABTM pains
zollinger/cakephp-habtamable-behavior
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
Lessens HABTM pains
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published
Languages
- PHP 100.0%