将变量从Model传递给控制器

时间:2011-08-12 14:28:48

标签: php codeigniter

我在我的模型上创建并使用了我需要能够在我的控制器上使用的变量如何实现?

编辑:

控制器:http://pastebin.com/jhAwAVa6

型号:http://pastebin.com/9xXRyYAa

3 个答案:

答案 0 :(得分:3)

您的问题不清楚,您究竟想做什么。

如果是访问模型属性,正确的方法是使用访问器方法:

class Model extends CI_Model{
    private $name;

    public function getName() {return $this->name; /*any other logic here*/}
    public function setName($value) {$this->name= $value; /*any other logic here*/}
}

答案 1 :(得分:1)

您无法将变量从模型传递给控制器​​。 您可以通过控制器访问模型的公共变量。

echo $this->model_name->variable_name;

答案 2 :(得分:1)

模型(my_model)

function useful_info()
{
    $data = new stdClass();
    $q = $this->db->get('users');
    $data->users = $this->db->result();
    $data->date = date('Y-m-d');
    $data->info = array('whatever','more','anything');
    return $data;
}

控制器

function index()
{
    $info = $this->my_model->useful_info();
    foreach($info->users as $user)
    {
        echo $user->id;
    }
    echo $info->date;
    if($info->info[0] == 'whatever')
    {
        // do something
    }
}

您不必创建对象(可以是字符串,T / F,数组等),但通常需要从模型和库函数中返回某些。您可以通过将其返回到变量$info = $this->my_model->useful_info();

来访问您返回的内容
相关问题