#CakePHP-Listing Model behaviour to append an optgroup into a list for a select box. The idea for this behaviour is that if you have a list of items which belong to another model, it will include that models name. So if you are looking for a specific item by it's relation, this will make that easier. I have created a website for the plugin, with more detailed information. http://jedistirfry.co.uk/CakePHP-Listing/
So we change,
array( 1 => 'First', 2 => 'Second' );
array( 'Cats' => array( 1 => 'First', ), 'Dogs' => array( 2 => 'Second' ) );
This is a standard CakePHP plugin, so it will need to extracted or submoduled into your
app/Plugin folder. I call it
Listing, so it should live in
You will need to activate the plugin in your
CakePlugin::load('Listing'), unless you are already using
- Cake 2
The models you are using with this behaviour must have Containable enabled.
public $actsAs = array('Containable', 'Listing.Listable');
I tend to add Containable to my
AppModel as it's handy to have everywhere!
You can attach to the model using the
$actsAs array. As you would normally.
You must include the name of the related model that you want to join to when you configure the behaviour.
This is usually the parent model in the relationship, as the behaviour will attach to the child. So if you want to list
Provider you would attach the behaviour to the
Broadband model, and configure the
You can also specify the fields that you want to use using,
public $actsAs = array( 'Listing.Listable' => array( 'relatedModelName' => 'Provider', // Example - this should be the parent model, the one you want to group by 'relatedModelPrimaryKey' => 'id', // optional - default shown 'relatedModelDisplayField' => 'name', // optional - default shown ) );
###Getting a listing
Then in order to attach the extra model, I have implemented a custom find called
listing which will return the formatted list.
$broadbands = $this->Broadband->find('listing');
###Customising your listing
The easiest way to customise the display of your listing is to use the models
$virtualFields property to create new fields which you can then pass into the find. Let's look at an example.
We want a listing of all the Users grouped by role. However our database has both
last_name, but we want to display a listing of the users full name. We can create a virtual field and select that.
<?php // app/Model/User.php public $virtualFields = [ 'full_name' => 'CONCAT(User.first_name, ' ', User.last_name)' ]; // app/Controller/UsersController.php $users = $this->User->find('listing', ['fields' => ['id', 'full_name']]);
Write some tests Tidy up the source a little Look to refactor Rewrite this README file!
- Remove the dependancy on Containble
- Thrown an exception if the models are not related
##License The MIT License (MIT)
Copyright (c) 2013 David Yell
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.