2013-05-06 16 views
0

这适用于我的控制器。在Codeigniter中向函数库发送函数

$this->load->driver('cache'); 

//working 
// $data['advisory']=$advisory = $this->cache->memcached->get('advisory'); 
// if(! $advisory) 
// { 
//  $data['advisory']=$advisory = $this->Mhomework->getbyadvisory($this->teacherid); 
//  $this->cache->memcached->save('advisory' , $advisory); 
// } 

我为此做了一个库,以便我可以将它用于其他人。

function cache($key, $data) 
{ 
    $this->CI->load->driver('cache'); 
    $cache = $this->CI->cache->memcached->get($key); 

    if (!$cache) { 
     // There's been a miss, so run our data function and store it 
     $cache = $data($CI); 
     //$cache = $data; 
     $this->CI->cache->memcached->save($key, $cache); 
    } 

    return $cache; 
} 

而在控制器中,我更改为以下错误。

$data['advisory'] = $this->hwtracker->cache('advisory.'.$this->teacherid, function(&$CI){ 
     return $CI->Mhomework->getbyadvisory($this->teacherid); 
    }); 

// error Call to a member function getbyadvisory() on a non-object in ... controller 
// Message: Trying to get property of non-object 

我的问题是我如何发送函数到我的库在CodeIgniter中?或者我可以吗?

更新:

function getbyadvisory($id){ 
       $Q="SELECT *, studentid, COUNT(studentid),be_user_profiles.first_name,    
       be_user_profiles.last_name 
      FROM be_user_profiles 
      LEFT JOIN hw_homework 
      ON be_user_profiles.user_id= hw_homework.studentid 
      WHERE be_user_profiles.advisor = $id 
      GROUP BY be_user_profiles.user_id 
      ORDER BY COUNT(studentid) DESC"; 
     $query = $this->db->query($Q); 

     if ($query->num_rows() > 0) 
     { 
      foreach ($query->result_array() as $row) 
      { 
       $data[] = $row; 
      } 

     } 
     else 
     { 
      $data = FALSE; 
     } 
     $query->free_result(); 
     return $data; 
    } 
+0

邮编()'函数,我认为你在传递'object'的同时接受数组 – 2013-05-06 08:55:09

+0

确保'Mhomework'已经加载。 – Aurel 2013-05-06 09:01:10

+0

$ this-> CI-> load-> model('welcome/Mhomework');加载到库构造函数中。 – shin 2013-05-06 09:09:35

回答

0

请改变你的库是这样

function cache($key, $data) 
{ 
$this->CI->load->driver('cache'); 

//bind your key here 
$key_details = $data.$key; 

$cache = $this->CI->cache->memcached->get($key_details); 

if (!$cache) { 

    // There's been a miss, so run our data function and store it 
    $cache = $this->$data($key); 

    //$cache = $data; 
    $this->CI->cache->memcached->save($key, $cache); 
} 

    return $cache; 
} 

//new advisory function in library 
function advisory($teacherid){ 

    return $this->CI->Mhomework->getbyadvisory($teacherid); 
} 

并在控制器调用,比如getbyadvisory的`这个

$data['advisory'] = $this->hwtracker->cache($this->teacherid,'advisory'); 
+0

1.我想在$ this-> hwtracker-> cache()中添加一个函数,而不是在库中。 2.它给出一个错误调用未定义的方法Hwtracker :: data()在/Users/teacher/Sites/hw_current2/application/libraries/Hwtracker.php在278行 – shin 2013-05-06 09:17:50