On Tue, Mar 3, 2009 at 9:35 AM, Henrik Gemal <[email protected]> wrote: > > Hi > > I'm having a little problem with converting an old sql request to use > cakephp. > > The sql is: > select name, if(module_id is null,0,1) activated from module left join > active_modules on (id = module_id and res_id = $id) > > I have something like: > ---- > var $useTable = "modules"; > var $belongsTo = array( > "active_modules" => array( > "className" => "ActiveModules", > "foreignKey" => "module_id", > ) > ); > --- > > not sure how to proceed. What do I do with "if(module_id is null, > 0,1)" ?
((CASE WHEN module_id IS NULL THEN 0 ELSE 1 END)) AS activated The double parentheses are for Cake's benefit.[1] Ordinarily, a single pair would do. The trouble with this, though, is that Cake won't return activated under 'Module' but beside it, under '0' (zero) because it has no model name to associate it with. But there's a way around that, too.[2] But, I wonder if it wouldn't be simpler to just handle this in afterFind() instead of your query, given that the [2] solution requires an afterFind() routine, anyway. [1] http://www.nabble.com/How-to-use-MySql%27s-CASE-WHEN-function-with-CakePhp-td19994842.html [2] http://teknoid.wordpress.com/2008/09/29/dealing-with-calculated-fields-in-cakephps-find/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
