Thursday, September 8, 2011

ClassRegistry::init and Model function availability problem

I am calling a Model inside of a Component to use as a faceted search Component. However, in two different contexts, I'm getting widely different results.

The function seems to work fine when I'm loading a page, however, when using the AJAX call, I'm getting the "You have an error in your SQL syntax" error, because instead of calling the Model function, CakePHP seems to be just passing the name of the function on as a query string. I understand that this is generally because the function is not being recognized, I'm just not sure why its not being recognized in this instance.

Here is the function where the Model is being called with ClassRegistry::init():

/*
// __facetConditions: Assembles subqueries for each facet/value pair
// @var arr $facets: Array of facet/value pair arrays
// @data arr $conditions: Model-usable array of subqueries.
*/
private function __facetConditions($facets) {
$fif = ClassRegistry::init('FacetedItemsFacet');
$conditions = array();
$aCount = 2;
// We need to create a subquery for every facet/value pair:
foreach($facets as $key=>$value) :
$subq = $fif->subquery(array(
'fields' => array('FacetedItemsFacet' . $aCount . '.faceted_item_id'), 
'conditions' => array('Facet' . $aCount . '.name' => $value[0], 'FacetedItemsFacet' . $aCount . '.value' => $value[1]),
'count' => $aCount),
'FacetedItemsFacet' . $aCount);
$conditions[] = '`FacetedItemsFacet`.`faceted_item_id` IN (`' . $subq . '`)';
$aCount++;
endforeach;
return $conditions;
}

This function is being called inside another function in the same Component. And that same function is being called in both contexts, which confuses me further. For illustration purposes, here is that calling function:

/*
// searchItems: Common function that returns all items matching the current set of sku's and facets/values
// @var mixed $skus: Either an array of sku's or else null
// @var mixed $facets: Either an associative array of facet/value pairs or null.
// @var bool $or: Specifies we would like to find items matching any (OR) values set.
*/
function search($skus=null, $facets=null, $or=false) {
$fif = ClassRegistry::init('FacetedItemsFacet');
$fi = ClassRegistry::init('FacetedItem');
$wpv = ClassRegistry::init('WebPriceView');
$i = ClassRegistry::init('Item');
// No input, no output:
if(empty($skus) && empty($facets)) :
$items = array();
// Otherwise, roll with it:
else :
$conditions = array();
if(!empty($facets)) :
$conditions = $this->__facetConditions($facets);
endif;
// If we have SKUs as well, add them to the conditions array:
if(!empty($skus)) :
$conditions[] = $this->__skuConditions($skus);
endif;
// Toggle AND or OR statements:
$or ? $conditions = array('OR' => $conditions) : $conditions;
// Finally, get our list of matching items:
$items = $fif->find('list', array('conditions' => $conditions,
  'fields' => array('FacetedItemsFacet.faceted_item_id'),
  'group' => array('FacetedItemsFacet.faceted_item_id')));
endif;
// Get the full details of the Items, including Web Price View and Siteline Item data
$fItems = $fi->getItems($items);
foreach($fItems as $key=>$item) :
$prices = $wpv->getPrices($item['FacetedItem']['item']);
$detail = $i->getItem($item['FacetedItem']['item']);
$fItems[$key]['WebPriceView'] = $prices['WebPriceView'];
$fItems[$key]['Detail'] = $detail['Item'];
// Re-order the Facets by their ID so they're easier to call out in the final Charts:
$fItems[$key]['Facet'] = Set::combine($fItems[$key]['Facet'], '{n}.id', '{n}');
endforeach;
return $fItems;
}

As I'm looking at this, I wonder if perhaps its a scope problem? Would it be more efficient to declare the ClassRegistry-initialized Objects as Object properties? Or is this not the problem?

Thank you for your help with this issue!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: