Skip to content

Suggestions

frogwarrior edited this page Jan 23, 2014 · 5 revisions

Hi, sorry if this is the wrong way to communicate, I'm new to Github. I've been using the Tree helper part of this Tools plugin, gonna install the full plugin to see what it does. Anyhow, I have a suggestion: Add methods to help with selecting items related to the nested category table. I started a TreeComponent but I don't have any time at the moment so I'll just post it here and see if you wanna add it to the tools:

` App::uses('Component', 'Controller'); class TreeComponent extends Component {

public $controllerName;

function initialize(Controller $controller) {
	
	if (isset($controller->request->query['category'])) {
		$cat_id = $controller->request->query['category'];
	}	
	else { $cat_id = NULL; }
	
	$this->controller = $controller->params["controller"];
	$this->slug = strtolower($this->controller);
	$this->model = Inflector::classify($this->slug);
	$this->cat_id = $cat_id;
}

public function getConditions() {
			
	$cat_id = $this->cat_id;
			
	$conditions = array();
	$conditions['joins'] = array();
	
	if ($cat_id !== NULL) {	

		$catsController = Inflector::camelize($this->slug) . 'Categories';
		$catsTable = Inflector::underscore($catsController);
		$catsRelTable = "{$this->slug}_cat_rels";
			
		$category = ClassRegistry::init($catsController)->findById($cat_id)[$catsController];

		$joins = array(
			array(
				'table' => $catsTable,
				'alias' => 'cats',
				'type' => 'INNER',
				'conditions' => array(
					'cats.lft BETWEEN ? and ?' => array($category['lft'],$category['rght'])
				)
			),
			array(
				'table' => $catsRelTable,
				'alias' => 'scr',
				'type' => 'INNER',
				'conditions' => array(
					"{$this->model}.id = scr.solution_id",
					"scr.{$this->slug}_category_id = cats.id"
				)
			)
		);  
		
		$conditions['joins'] = $joins;  
	}
	return $conditions;
}

} `

Heres how you'd use it with find('all'):

$conditions = $this->Tree->getConditions(); $items = $this->Model->find('all',$conditions); $this->set('items', $items);

and for paginating: `$conditions = $this->Tree->getConditions();

$this->paginate = array( 'maxLimit' => 100, 'limit' => 20, 'order' => array('id' => 'asc'), 'joins'=>$conditions['joins'] );

$items = $this->Paginator->paginate(); `

Clone this wiki locally