在Codeigniter中向库中发送函数

时间:2013-05-06 08:48:08

标签: php 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;
    }

1 个答案:

答案 0 :(得分: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');