Re: Model1 hasMany Model2 with count(), how to get SQL like this?
When you specify fields you are telling Cake which fields to return... in this case only the count... nothing from the primary model. I think a * is enough as an additional field to get all fields. On Mar 12, 3:35 am, Svel svel.so...@gmail.com wrote: Thank You =) Little fix to your reply: $this-find('all', array('fields' = array('COUNT(BaseImage.id) as cnt', 'Base.id'), 'group' = array('BaseImage.id'), 'joins' = array( array( 'type' = null, // or INNER or LEFT - uppered, couse no automatic 'alias' = 'BaseImage', 'table' = 'base_images', // defaults to 'join_table' =) 'conditions' = array('Base.id = BaseImage.base_id') // but not array('Base.id' = 'BaseImage.base_id') ) ) )); What about 'foreignKey' for joins? all other keys found in cake/libs/model/datasources/dbo_source.php lines 1101-1105 and 1223-1229 and 1491-1494 resulting array looks like: array( [0] = array( [0] = array([cnt] = 1) [Base] = array(...) ) [1] = ... ) ... no [BaseImages] key PS: sorry for my english On 11 мар, 18:04, Martin Westin martin.westin...@gmail.com wrote: You probably want a little, slightly obscure, gem called joins. In use it looks something like: $this-find('all', array( 'conditions' = $conditions, 'joins' = array( array( 'table' = 'model2', 'alias' = 'Model2', 'type' = 'inner', 'foreignKey' = model1_id, 'conditions'= array( 'Model2.another_fk = Model1.id' ) ) ) )); Check out Nate's nice article on the subject: (he talks about habtm but it is just as useful for hasMany)http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-... /Martin On Mar 11, 2:39 pm,Svelsvel.so...@gmail.com wrote: Searching for 2 days and found nothing =( How i can build this SQL code: SELECT `m1`.`id`, COUNT(`m2`.`id`) as cnt FROM `m1` LEFT JOIN `m2` ON (`m1`.`id` = `m2`.`m1_id`) WHERE 1=1 GROUP BY `m2`.`m1_id` ORDER BY `m1`.`id` is it real without custom Model-query() ? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Model1 hasMany Model2 with count(), how to get SQL like this?
You probably want a little, slightly obscure, gem called joins. In use it looks something like: $this-find('all', array( 'conditions' = $conditions, 'joins' = array( array( 'table' = 'model2', 'alias' = 'Model2', 'type' = 'inner', 'foreignKey' = model1_id, 'conditions'= array( 'Model2.another_fk = Model1.id' ) ) ) )); Check out Nate's nice article on the subject: (he talks about habtm but it is just as useful for hasMany) http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find /Martin On Mar 11, 2:39 pm, Svel svel.so...@gmail.com wrote: Searching for 2 days and found nothing =( How i can build this SQL code: SELECT `m1`.`id`, COUNT(`m2`.`id`) as cnt FROM `m1` LEFT JOIN `m2` ON (`m1`.`id` = `m2`.`m1_id`) WHERE 1=1 GROUP BY `m2`.`m1_id` ORDER BY `m1`.`id` is it real without custom Model-query() ? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Model1 hasMany Model2 with count(), how to get SQL like this?
Thank You =) Little fix to your reply: $this-find('all', array('fields' = array('COUNT(BaseImage.id) as cnt', 'Base.id'), 'group' = array('BaseImage.id'), 'joins' = array( array( 'type' = null, // or INNER or LEFT - uppered, couse no automatic 'alias' = 'BaseImage', 'table' = 'base_images', // defaults to 'join_table' =) 'conditions' = array('Base.id = BaseImage.base_id') // but not array('Base.id' = 'BaseImage.base_id') ) ) )); What about 'foreignKey' for joins? all other keys found in cake/libs/model/datasources/dbo_source.php lines 1101-1105 and 1223-1229 and 1491-1494 resulting array looks like: array( [0] = array( [0] = array([cnt] = 1) [Base] = array(...) ) [1] = ... ) ... no [BaseImages] key PS: sorry for my english On 11 мар, 18:04, Martin Westin martin.westin...@gmail.com wrote: You probably want a little, slightly obscure, gem called joins. In use it looks something like: $this-find('all', array( 'conditions' = $conditions, 'joins' = array( array( 'table' = 'model2', 'alias' = 'Model2', 'type' = 'inner', 'foreignKey' = model1_id, 'conditions'= array( 'Model2.another_fk = Model1.id' ) ) ) )); Check out Nate's nice article on the subject: (he talks about habtm but it is just as useful for hasMany)http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-... /Martin On Mar 11, 2:39 pm,Svelsvel.so...@gmail.com wrote: Searching for 2 days and found nothing =( How i can build this SQL code: SELECT `m1`.`id`, COUNT(`m2`.`id`) as cnt FROM `m1` LEFT JOIN `m2` ON (`m1`.`id` = `m2`.`m1_id`) WHERE 1=1 GROUP BY `m2`.`m1_id` ORDER BY `m1`.`id` is it real without custom Model-query() ? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---