Last active
December 20, 2015 14:09
-
-
Save francisrod01/6144174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array | |
( | |
[0] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 40 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => Buffet Castelo | |
) | |
[Category] => Array | |
( | |
[0] => Array | |
( | |
[id] => 14 | |
[parent_id] => 2 | |
[nome] => Buffet | |
) | |
) | |
) | |
[1] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 41 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => Buffet Cesar Monteiro Fine | |
) | |
[Category] => Array | |
( | |
[0] => Array | |
( | |
[id] => 14 | |
[parent_id] => 2 | |
[nome] => Buffet | |
[tipo] => local | |
[url_rewrite] => | |
[status] => 1 | |
[created] => 2013-08-01 12:31:16 | |
[updated] => | |
) | |
) | |
) | |
[2] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 42 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => Buffet Deucher | |
) | |
[Category] => Array | |
( | |
[0] => Array | |
( | |
[id] => 14 | |
[parent_id] => 2 | |
[nome] => Buffet | |
) | |
) | |
) | |
[3] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 43 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => Buffet Ilha do Mehl | |
) | |
[Category] => Array | |
( | |
[0] => Array | |
( | |
[id] => 14 | |
[parent_id] => 2 | |
[nome] => Buffet | |
) | |
) | |
) | |
[205] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 254 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => Totopos | |
[url_rewrite] => totopos | |
[short_url] => | |
[imagem] => sem-imagem.jpg | |
[endereco] => Al. Princesa Izabel | |
[numero] => 2020 | |
[complemento] => | |
[cep] => 81 | |
[status] => 1 | |
[created] => 2013-05-09 00:26:52 | |
[updated] => | |
) | |
[Neighborhood] => Array | |
( | |
[id] => | |
[state_id] => | |
[city_id] => | |
[nome] => | |
) | |
[City] => Array | |
( | |
[id] => 6015 | |
[state_id] => 18 | |
[country_id] => 30 | |
[nome] => Curitiba | |
[status] => 1 | |
[created] => 2013-08-01 11:36:18 | |
[updated] => | |
) | |
[212] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 261 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => Villa Sabor - Shopping Mueller | |
) | |
[Category] => Array | |
( | |
[0] => Array | |
( | |
[id] => 11 | |
[parent_id] => 2 | |
[nome] => Restaurante | |
[tipo] => local | |
[url_rewrite] => | |
[status] => 1 | |
[created] => 2013-08-01 12:31:16 | |
[updated] => | |
) | |
) | |
) | |
[213] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 262 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => Wikimaki Batel | |
) | |
[Category] => Array | |
( | |
[0] => Array | |
( | |
[id] => 11 | |
[parent_id] => 2 | |
[nome] => Restaurante | |
) | |
) | |
) | |
[214] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 263 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => Yü Cozinha | |
) | |
[Category] => Array | |
( | |
[0] => Array | |
( | |
[id] => 11 | |
[parent_id] => 2 | |
[nome] => Restaurante | |
) | |
) | |
) | |
[215] => Array | |
( | |
[Local] => Array | |
( | |
[id] => 264 | |
[user_id] => 1 | |
[neighborhood_id] => | |
[city_id] => 6015 | |
[nome] => XXXX | |
) | |
[Category] => Array | |
( | |
[0] => Array | |
( | |
[id] => 11 | |
[parent_id] => 2 | |
[nome] => Restaurante | |
) | |
) | |
) | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Print da consulta no banco em PNG | |
http://i38.servimg.com/u/f38/11/80/81/44/print_11.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT l_c.local_id, l_c.category_id, | |
local.nome AS local_nome, categ.nome AS categ_nome | |
FROM locals_categories AS l_c | |
INNER JOIN (locals local) ON (local.id = l_c.local_id AND local.city_id = 6015) | |
INNER JOIN (categories categ) ON (categ.id = l_c.category_id) | |
WHERE (categ.id = 2 AND categ.parent_id IS NULL) OR categ.parent_id = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* HABTM Find with CakePHP 2.x | |
* | |
* @author user787301 | |
* @link http://stackoverflow.com/questions/12138150/habtm-find-with-cakephp-2-0 | |
*/ | |
class HabtmBehavior extends ModelBehavior { | |
public function beforeFind(Model $model, $options) { | |
if (!isset($options['joins'])) { | |
$options['joins'] = array(); | |
} | |
if (!isset($options['habtm'])) { | |
return $options; | |
} | |
$habtm = $options['habtm']; | |
unset($options['habtm']); | |
foreach ($habtm as $m => $scope) { | |
$assoc = $model->hasAndBelongsToMany[$m]; | |
$bind = "{$assoc['with']}.{$assoc['foreignKey']} = {$model->alias}.{$model->primaryKey}"; | |
$options['joins'][] = array( | |
'table' => $assoc['joinTable'], | |
'alias' => $assoc['with'], | |
'type' => 'inner', | |
'foreignKey' => false, | |
'conditions' => array($bind) | |
); | |
$bind = $m . '.' . $model->{$m}->primaryKey . ' = '; | |
$bind .= "{$assoc['with']}.{$assoc['associationForeignKey']}"; | |
$options['joins'][] = array( | |
'table' => $model->{$m}->table, | |
'alias' => $m, | |
'type' => 'inner', | |
'foreignKey' => false, | |
'conditions' => array($bind) + (array) $scope, | |
); | |
} | |
return $options; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// MOdel/Local.php | |
Class LocalModel extends AppModel { | |
public $actAs = array('Habtm'); | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Class LocalsController extends AppController { | |
public function getLocals() { | |
$this->Local->unbindModel(array('belongsTo' => array('User'))); | |
$rests = $this->Local->find('all', array( | |
'habtm' => array( | |
'Category' => array( | |
'OR' => array( | |
'AND' => array( | |
'Category.id' => 2, | |
'Category.parent_id' => NULL | |
), | |
'AND' => array( | |
'Category.parent_id' => 2, | |
) | |
), | |
) | |
), | |
'conditions' => array( | |
'Local.city_id' => 6015 // Curitiba | |
) | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
References: | |
Me baseei por este tópico: | |
http://stackoverflow.com/questions/12138150/habtm-find-with-cakephp-2-0/15316824#15316824 | |
Melhorei muito a partir desses tópicos aqui: | |
http://stackoverflow.com/questions/2929666/cakephp-and-group-by | |
http://stackoverflow.com/questions/5649185/cakephp-find-all-condition-and-or |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment