web
CakePHP 1.2 GROUP BY with HAVING COUNT(*) HABTM associations (limit the query based on associated data)
by volve on Mar.26, 2009, under articles, web
The problem: Your models look like this “Images HABTM Albums” so when you want to show 4 Albums, they come along with their Images too – great! BUT, what happens if some Albums don’t have any Images? You don’t want to show those to your users, but how do you filter them out? Well you could do it after the find() has run, in which case you may end-up with less than 4 useable Albums – bad
To workaround that you could always ask find() to give you 8 Albums in the hope that at least 50% of them will be useable, but there’s no guarantee – still bad.
The solution below takes advantage of CakePHP 1.2’s bindModel() method to manually create an INNER JOIN in place of the pre-defined HABTM association. This allows us to execute the find() with a HAVING COUNT(*) in the group option. In the example below, this causes any Albums with 0 Images to be omitted from the results as they don’t meet the HAVING criteria. What’s more, the limit of 4 correlates exactly so 8-just-incase kludge needed.
This was driving me crazy so here’s the solution for anyone else that might be stuck in the same situation. (This method is in the Albums model.)
function getAlbums($num=4) {
$contain = array( // use Containable Behavior for efficiency 'AlbumsImage',
'FilterImage',// although part of the magic and required, this seems to confuse CakePHP a little - I'll try and resolve it and update the post
'Image',
'Image'=>array('ImageSize',
'fields'=>array('Image.id','Image.title','Image.host','Image.path')),
'User'=>array('fields'=>'User.id,User.username'));
$this->bindModel(array('hasOne'=>array('AlbumsImage',
'FilterImage'=>array( // FilterImage so as to distinguish between regular Image
'className'=>'Image',
'foreignKey'=>false,
'type'=>'INNER',
'conditions'=>array(
'FilterImage.status = 0',
'FilterImage.id = AlbumsImage.image_id')
)
)
));
$albums = $this->find('all', array('contain' => $contain,
'conditions' => array('Album.status'=>'0'),
'fields' => array('Album.id','Album.user_id','Album.title'),
'order' => 'Album.created DESC',
'limit' => $num,
'group' => array('Album.id','Album.title HAVING COUNT(*) >= 1'))); // here's the clincher
return $albums;
}
New CNN.com Layout
by volve on Jun.30, 2007, under ui, web
I just noticed today that CNN.com have rolled out a new layout. I quite like the simplified look. It definitely feels as if it brings the news to the forfront whilst maintaining compartmentalization.
Looking at the brief stats it seems a lot lighter too. Oh and it has some smooth carousel action going on with the video thumbnails too.
Kind of a shame that Glasgow Airport terrorist crashing nonsense had to take me there but oh well.
scrybe organiser
by volve on Oct.22, 2006, under software, ui, web
This looks fairly impressive. At times it appears as though they’re using Flash or Flex, but at others it’s hard to tell. In any case, it should be fun to see. I’m particularly impressed with the couple of different printing options mentioned, and the smooth integration of timezones. I’m not so enamored with the Offline support as I think it’ll have many limitations, unless they’re definitely using Flex… hmmm
Teasing details and a beta email signup available at: Scrybe site
Some Snappy Applications
by volve on Jul.04, 2006, under ui, web
Here a couple of recent UI concepts I’ve been impressed with. Imagery and Timeline. Go play with them!
Database Schemata Design Quickie
by volve on Jan.05, 2006, under ui, web
In an awesome twist of “bobby just sent me a link when I asked about schema design“-fate, WWW SQL Designer has to be the coolest “random web app from some random guy” in quite a while! Go forth and create!
Firefox session extension
by volve on Nov.17, 2005, under software, web
As it caused me some effort to actually locate, I wish to share with you this awesome Firefox Extension entitled SessionSaver. The below description does not do the usefulness justice. The version at the link below works with Firefox version 1.0.x and 1.5. Recently the Mozilla team have implemented automatic session restoring upon a Firefox crash (maybe even simply a quit) for the 2.0 version, which is excellent. I’ve always loved Opera’s ability to remeber everything a user was doing when the browser was closed – definitely a step in the right direction.
Download SessionSaver from: adblock.ethereal.net
[Description from the Addons.Mozilla.org page]:
“SessionSaver restores your browser -exactly- as you left it, every startup, every time. Not even a crash will phase it. Windows, tabs, even things you were typing — they’re all saved. Use the menu to add + remove sessions; right, shift, or middle-clicking will delete. “Simple mode” for peace of mind, or “Expert mode” for advanced flexibility. Just Click. Install. Rad.
Added TextSaver: everything you type, saved (even in frames). Added RemoteSync: session-syncing between browsers (syncs Adblock, too). Added intelligent postData-restore. Added various patches + bugfixes, per discussion in the official support thread.”
