CodeIgniter引用模型内部的返回对象


CodeIgniter reference return object inside model

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

return$query->row();

我需要从这个物体中获取照片,并在同一模型内的另一个函数中使用它。

请建议我该怎么做。

将照片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);
}
}

如果我正确理解:

型号:

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
}