Saturday, July 28, 2012

cakephp help 1.3 to 2.+ migration can you help me classRegistry::getObject(model is not working)

Unexpected Error : helpers array is empty!!
Notice (8): Undefined variable: _SESSION [ROOT\plugins\jq_plugins_bridge\Controller\Component\JqGridComponent.php, line 66]
Notice (8): Undefined variable: _SESSION [ROOT\plugins\jq_plugins_bridge\Controller\Component\JqGridComponent.php, line 67]
jqplugins_jqgrid_AddressJqGrid ERROR : Requested Model cannot be loaded. [Address] 


<?php




require_once '/../../Bases/jq_grid_base.php';
require_once '/../../util.php'; 

App::uses('ClassRegistry','Utility');
App::uses('Address','Model');

class JqGridComponent extends JqGridBase { 
    const EVENT_NEXT     = "next";
    const EVENT_PREVIOUS = "previous";
    
    private $lastEvent;
private $ajaxAction = "gridCallback";
private $controller;
     
    public function initialize(&$controller, $settings=array()){
       Util::injectHelper($controller, $this->name);
  $controller->set("pluginCom." . $this->name,$this);  //////It will be retrieved by injected Helper inside the view
  $this->controller =& $controller;
  //$this->clear();
     }

     
     public function startup(&$controller){
      if(!strpos("_".$controller->action,JQGRID_PREFIX)) /////if we should not intercept the action
       return; 

       $modelName  = substr($controller->action,strrpos($controller->action, "_")+1);
        
       $controller->action = $this->ajaxAction;
       $controller->autoRender = false;
       $controller->autoLayout = false;
       $controller->output = $this->handleRequest($controller,$modelName);
     }
     public function setAjaxAction($actionName){
      $this->ajaxAction = $actionName;
     }

     public function getAjaxAction(){
      return $this->ajaxAction;
     }
     
public function grid($data , $filter = null , $labels = null){  
 
 if(gettype($data) == "string"){
  $this->gridName = $data;      
    
  unset($_SESSION[JQGRID_PREFIX.$this->gridName."_labels"]); 
  unset($_SESSION[JQGRID_PREFIX.$this->gridName]);
   
   $headers = array(); 
   App::uses($data , 'Model');
   $model = ClassRegistry::getObject($data);
  // $model = $this->_View->element($data);
   echo print_r(ClassRegistry::init($data));
  if($model == null) {
 
            $this->controller->loadModel($data); 
            $model = $this->controller->{$data};
            
          } 
          if($model == null || $model->_schema == null){
          die("JqGrid ERROR : Requested Model cannot be loaded. [".$data."]");
          }      
        $metadata = $model->_schema;
        foreach ($metadata as $key => $field)
          $headers[] = $key;
       
   if($filter != null && array_key_exists("fields",$filter)){
          $headers = array();
          foreach ($filter['fields'] as $field)  //////////They are defined like Model.fieldname
            $headers[] = substr($field, strrpos($field, ".")+1);  //////////// we extract fieldname from rest of the string       
        }
           
        if($labels != null){
          $labelMap = array();
          $size = sizeof($labels);
          for($i=0;$i<sizeof($headers);$i++){
               $labelMap[$headers[$i]] = ($i >= $size) ? $headers[$i] : $labels[$i];
          }     
          $_SESSION[JQGRID_PREFIX.$this->gridName."_labels"] = $labelMap;  
        }  
        
       parent::grid($labels==null ? $headers : $labels,$headers);    
 }  
 else{
 
  //////////////$data is an array of prefetched Model objects....
 }
 
 if($this->gridName == null)
   die("Error: Passed data seems to be invalid to make a grid. [".$data."]");
  
 $pagination = $this->controller->paginate;  
 $pagination = array_key_exists($data,$pagination) ? $pagination[$data] : $pagination;
 
  //////////////storing filters
 if($filter != null)
   $_SESSION[JQGRID_PREFIX.$this->gridName] = $filter;  
   
 if($filter != null)  
   $pagination = array_merge($pagination,$filter);  
   
 //////////////////////////////// synchronizing shared options  
 if(isset($pagination['limit']))
   $this->generalOptions(array(GridBuilder::ATTR_ROWS_PER_PAGE => $pagination['limit']));   
   
      if(isset($pagination['page']))
   $this->generalOptions(array(GridBuilder::ATTR_PAGE => $pagination['page']));  
       
 /////////////////some internal options for handling ajax calls  
 $this->generalOptions(array(GridBuilder::ATTR_URL=> JQGRID_PREFIX . $this->gridName,
                             GridBuilder::ATTR_DATA_TYPE => "json")); ///////it just supports json at the moment
 
 $this->generalOptions(array(GridBuilder::ATTR_ENHANCED_INSERTION=>true));                            
                             
      $this->parserOptions(array(GridBuilder::ATTR_PARSER_REPEAT => false,
                                 GridBuilder::ATTR_PARSER_ID => "0")); 
                                 
      $this->searchOptions(array(GridBuilder::ATTR_SEARCH_SOPT => array ("eq" , "ne" , "lt" , "le" , "gt" , "ge" , "bw" , "ew" , "cn")));                           

      
return $this; 
}
 
/*
* Ajax requests are handled here
*/
protected function handleRequest(&$controller,$gridModel){
      $temp = $controller->paginate; 
 if(array_key_exists(JQGRID_PREFIX.$gridModel, $_SESSION)){
  $filter = $_SESSION[JQGRID_PREFIX.$gridModel];
  $paginate = array_key_exists($gridModel, $controller->paginate) ? $controller->paginate[$gridModel] : $controller->paginate;
   foreach ($filter as $key => $options){
             if(is_array($options)){
              $paginate[$key] = (array_key_exists($key,$paginate)) ? array_merge($options,$paginate[$key]) : $options;
             } 
    else 
      $paginate[$key] = $options; 
   }
  $controller->paginate = $paginate;
 }
 
  $map = null;
  if(array_key_exists(JQGRID_PREFIX.$gridModel."_labels", $_SESSION))
    $map  = $_SESSION[JQGRID_PREFIX.$gridModel."_labels"];
 
       if(ClassRegistry::getObject($gridModel) == null)
          $this->controller->loadModel($gridModel);

        $result = null;  
          
        if(array_key_exists("_search", $_REQUEST) && $this->strip($_REQUEST['_search']) == 'true'){
          $this->prepareForSearch($controller->paginate, $gridModel, $map);   
        }
        if(array_key_exists("sidx", $_REQUEST) && strlen($_REQUEST['sidx']) > 1){
          $this->prepareForSort($controller->paginate, $gridModel);
        } 
           
   $result = $controller->paginate($gridModel);
    
  $paging = $controller->params['paging'][$gridModel];
  $responce->page = $paging['page'];
       $responce->total = $paging['pageCount'];
       $responce->records = $paging['count'];
       
       $i = 0;  ///////////////building the json object
  if($map != null){
        foreach($result as $item){
          $responce->rows[$i]=$item[$gridModel];
            foreach ($responce->rows[$i] as $key => $element){
               if(array_key_exists($key,$map)){
                $responce->rows[$i][$map[$key]] = $responce->rows[$i][$key];
                unset($responce->rows[$i][$key]);  
               }  
            }
           $i++; 
         }   
       }
      else { 
        foreach($result as $item){
         $responce->rows[$i++]=$item[$gridModel];
        }   
      }
       $controller->paginate = $temp;  ////////////go back to what it was
       
     return json_encode($responce);
    }
    
    
    private function prepareForSearch(&$paginate , $model , $labels){

     $conditions = array();
$fld = $this->strip($_REQUEST['searchField']);
 if($labels != null){
  foreach ($labels as $key=>$value){
   if($value == $fld){
    $fld = $key;
    break;
   }
 
 }
$fldata = $this->strip($_REQUEST['searchString']);
$foper  = $this->strip($_REQUEST['searchOper']);
switch ($foper) {
case "eq":
$conditions["$model.$fld"] = is_numeric($fldata) ? $fldata : "'$fldata'"; 
break;
case "ne":
$conditions["$model.$fld <>"] = is_numeric($fldata) ? $fldata : "'$fldata'"; 
break;
case "lt":
$conditions["$model.$fld <"] = is_numeric($fldata) ? $fldata : "'$fldata'"; 
break;
case "le":
$conditions["$model.$fld <="] = is_numeric($fldata) ? $fldata : "'$fldata'"; 
break;
case "gt":
$conditions["$model.$fld >"] = is_numeric($fldata) ? $fldata : "'$fldata'"; 
break;
case "ge":
$conditions["$model.$fld >="] = is_numeric($fldata) ? $fldata : "'$fldata'"; 
break;
case "bw":
$conditions["$model.$fld LIKE"] = $fldata."%"; 
break;
case "ew":
$conditions["$model.$fld LIKE"] = "%$fldata"; 
break;
case "cn":
$conditions["$model.$fld LIKE"] = "%$fldata%"; 
break;
default :
$conditions = array();
 }
 
if(array_key_exists('conditions', $paginate))
 $conditions = array_merge($paginate['conditions'],$conditions);

 $paginate['conditions'] = $conditions;  
    } 
    
    
    private function prepareForSort(&$paginate , $model){
     
       $sidx = $_REQUEST['sidx'];
       $sord = $_REQUEST['sord'];
       $order = array("$model.$sidx ".strtoupper($sord));
       if(array_key_exists('order', $paginate))
        $order = array_merge($order,$paginate['order']);
   
       $paginate['order'] = $order; 
    }
    
    
   private function strip($value){
if(get_magic_quotes_gpc() != 0)
  {
    if(is_array($value))  
if(array_is_associative($value)){
foreach( $value as $k=>$v)
$tmp_val[$k] = stripslashes($v);
$value = $tmp_val; 
}
else  
for($j=0;$j<sizeof($value);$j++)
        $value[$j] = stripslashes($value[$j]);
else
$value = stripslashes($value);
}
return $value;
  }
  
   private function array_is_associative ($array){
    if ( is_array($array) && ! empty($array) )
    {
      for ($iterator=count($array)-1;$iterator; $iterator-- )
        if (!array_key_exists($iterator, $array)) { return true; }
     
      return !array_key_exists(0, $array);
    }
    return false;
  }

  
}

     
     
?>

--
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: