Jump to content

CakePHP Snippets


CrimpJiggler

Recommended Posts

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 by CrimpJiggler
Link to comment
Share on other sites

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:

chart1-1024x853.png

Edited by CrimpJiggler
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.