模型中的 CodeIgniter 引用 return 对象

CodeIgniter reference return object inside model

我在代码点火器中有一个模型,它获取一个照片对象作为一行

return $query->row();

我需要从这个对象中获取 photoid 并在同一模型内的另一个函数中使用它。

请建议我该怎么做。

如果我理解正确的话:

型号:

public function myfunction()
{
    //stuff

    $photo = $this->mysecondfunction();
    $photo_id = $photo->id;

    //stuff

}

private function mysecondfunction()
{
    //stuff

    return $query->row();
}


控制器:

$photo = $this->my_model->getPhoto();
$this->my_model->doStuffWithMyPhotoId($photo->id);

型号:

public function getPhoto()
{
    //stuff

    return $query->row();
}


public function doStuffWithMyPhotoId($photoid)
{
    //stuff
}

将照片 ID 分配给变量;

$photo_id = $query->row('photo_id');

然后你就可以这样使用了;

$this->method($photo_id);

如果您希望 photo_id 被该函数的任何功能所取代,您可以这样做;

$query->row('photo_id') = $this->method($query->row('photo_id'));

希望对您有所帮助。

在构造函数中调用模型函数并放入如下数组:

class Demo extends CI_Controller {
var $data = array();
function __construct()
{
    parent::__construct();
    $this->load->model('photo');
    $this->data = $this->photo->getData();

}
function index()
{
    print_r($this->data);
}
function user()
{
    print_r($this->data);
}
}