I know its been a bit of a whole since my last update but have a bit of useful info here:
function beforeFind(&$queryData)
{
$conditions = $queryData['conditions'];
if (!is_array($conditions)) {
if (!$conditions) {
$conditions = array();
} else {
$conditions = array($conditions);
}
}
if (!isset($conditions['active']) && !isset($conditions[$this->alias . '.active'])) {
$conditions[$this->alias . '.active'] = 1;
}
$queryData['conditions'] = $conditions;
return true;
}
Put this in the model that you have an ‘active’ field in and it will save you having to add a condition into each find statement to check whether that record is flagged as active / inactive!
Don’t get me wrong – I love CakePHP – im not an expert in it by any means but can quickly knock up a site and it definitely saves me time when coding stuff – no doubt.
One of the main problems I have with it is its pagination classes, true, they work fine if you are just looking to paginate a single model on a page. Whatabout paginating multiple models? For example – say you have a “User Profile” page – on this page you would expect to see things like “Gallery”, “Friends”, “Messages” etc etc – that alone is 3 models.
And say you needed to paginate each of these models to say show only 5 at a time and allow them to be sorted by title, date, etc?
Currently its not very easy with the current set of tools available in cakePHP. Luckily CakePHP has a fantastic community of skilled proggrammers (Certainly more skiilled than I am!).
One such programmer is a guy called Andrew who has released a helper and component out to the community which allows for such functionality.
You can find the code here (http://github.com/angel333/listing/tree/master).
Basically you instantiate the Helper and component in the controller:
class PostsController extends AppController { public $components = array ('Listing.Listing'); public $helpers = array ('Listing.Listing'); ...
Then after setting up some routing you set your data array using something similar to this:
$this->set('data', $this->Listing->create($this->Post, array ( 'default' => array ( 'order' => 'Post.created DESC', 'limit' => 10, ), 'user' => array ( 'order' => array ( 'Post.title', 'Post.name', 'Post.modified', ), 'limit' => array (10, 25, 50, 100), 'search' => array ( 'Post.title', 'Post.content', ), ), )));
then call the ever so handy scaffold function to see what code you need to put in.
Everything is on his readme in detail – and from my experience he is very helpful if you contact him!
Good work Andrew
Ok, so you are preparing your application to start coding, you are probably going to work on the users and the authentication system first of all and you want to limit different users to different parts of the site.
Sounds simple enough – cakePHP provides something called ACL as part of its core. However you find that when you try and implement it – you realise it is clunky, unreleting and basically far too complicated for every day use.
We used a different approach that we unashamedly rinsed from Studio Canaria, Peter Butler has knocked up a fantastic and a lot more logical approach to ACL. Basically you assign permissions based on groups rather than “objects” and “requesters”.
e.g. to grant access for a group to a specific controller / action you can add a row to the permissions table along the lines of controller:action or controller:* or even just * to override (for say a system developer).
Anyway, I digress. Check it out – his blog is a fantastic resource and has helped me out many times in the past:
http://www.studiocanaria.com/articles/cakephp_auth_component_users_groups_permissions_revisited
Just a quick one today, if you find you are having to use nested Set::Extracts or nested foreach loops in your code to extract data then it is possible you have some incorrect associations.
For instance if you array looks like this:
Array ( [0] => Array ( Array ( [0] => Array (
It is possible that you have a hasMany where you should have an hasOne or belongsTo association.
I recently had a bit of a brain block how to do a certain thing in cakePHP – I basically wanted to update a model from a view but not using a GET method (as that would have allowed URL manipulation – and potentially created problems).
The simple answer (again thanks to Darren at zeen.co.uk) is to use a form to submit the data you need.
echo $form->create('SongList'); echo $form->input( 'sttw_track_id', array( 'value' => $track['SttwTrack']['id'], 'type' => 'hidden') ); echo $form->input( 'user_id', array( 'value' => $userID_Logged, 'type' => 'hidden') ); echo $form->submit($this->webroot . 'images/add_to_list.png', array('style' => 'width:31px; height:52px')); echo $form->end();
This basically means that rather than creating a GET type URL (/controller/action/param1/param2) – I can now submit information pre-populated using POST. I know this seems obvious now you look at it, but to many newbies in CakePHP – doing something like this when there is so much automagical stuff going on can sometimes be overlooked.