CrimpJiggler Posted January 21, 2014 Share Posted January 21, 2014 (edited) Just thought I'd share a few useful bits of code I wrote for CakePHP. Heres code to select rows by ID of a nested hierarchical (tree) database: $conditions = array( 'alias' => 'items' ); if (isset($this->request->query['category'])) { $cat_id = $this->request->query['category']; $category = ClassRegistry::init('Categories')->findById($cat_id)['Categories']; $joins = array( array( 'table' => 'categories', 'alias' => 'cats', 'type' => 'INNER', 'conditions' => array( 'cats.lft BETWEEN ? and ?' => array($category['lft'],$category['rght']) ) ), array( 'table' => 'items_cat_rels', 'alias' => 'rels', 'type' => 'INNER', 'conditions' => array( 'Item.id = rels.item_id', 'rels.category_id = cats.id' ) ) ); $conditions['joins'] = $joins; } $this->set('items', $this->Item->find('all',$conditions)); and heres a function for cleaning empty and null arrays out of your multivariable arrays: public function arrayCleaner($results) { $arrayUnsetter = function (&$data) { foreach ($data as $resType => $params) { if (empty($data[$resType])) { unset($data[$resType]); } $keys = array_keys($params); if (isset($keys[0]) && $keys[0] !== 0 && $keys[0] == "id") { if (@$params['id'] === null) { unset($data[$resType]); } } } return $data; }; if (isset($results[0])){ foreach($results as $key => $value){ $output[$key] = $arrayUnsetter($value,$key); } } else { $output = $arrayUnsetter($results,"NULL"); } return $output; } Edited January 21, 2014 by CrimpJiggler Quote Link to comment https://forums.phpfreaks.com/topic/285566-cakephp-snippets/ Share on other sites More sharing options...
trq Posted January 22, 2014 Share Posted January 22, 2014 Jeeze, Cake isn't pretty is it? Quote Link to comment https://forums.phpfreaks.com/topic/285566-cakephp-snippets/#findComment-1466144 Share on other sites More sharing options...
CrimpJiggler Posted January 23, 2014 Author Share Posted January 23, 2014 (edited) Its probably just my use of it that isn't pretty lol. I'm a beginner to it and the more I learn, the more I'm able to use it to save me time. For example, I just converted a massive adjacency model hierarchical database to a nested one with cake, because all you need to do is prepare an array/object of the right format and feed it into cakes saveAll function and it does all the rearranging for you. Its the model part I find a pain in the ass, cake lets you define all your table relationships in the model file but in my case, some of my relationships are too complicated for cakes built in functionality and cake doesn't make it easy to do things your own way, when theirs doesn't work. Working with AJAX seems to be a real pain in the ass in cake though. And working with multiple databases, I haven't even figured out how to do that yet. I wanna try another framework to see if I like it better, but will I have to start from scratch in learning it? Cake is the first framework I tried so theres loads of new stuff I'm learning before I can use it. I suppose at least I know what MVC is all about now, so that'll gimme a headstart. My brother recommended Slim but I'm gonna try laravel since its so popular: Edited January 23, 2014 by CrimpJiggler Quote Link to comment https://forums.phpfreaks.com/topic/285566-cakephp-snippets/#findComment-1466252 Share on other sites More sharing options...
trq Posted January 23, 2014 Share Posted January 23, 2014 I wouldn't read too much into that graph either. Quote Link to comment https://forums.phpfreaks.com/topic/285566-cakephp-snippets/#findComment-1466271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.