0

这适用于我的控制器。

$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;
    }
4

1 回答 1

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);
}

在这样的控制器调用中

$data['advisory'] = $this->hwtracker->cache($this->teacherid,'advisory');
于 2013-05-06T09:13:35.357 回答