2012-06-19 65 views
0

我有控制器和模型。我正在修改模型中的一个变量值,但它不反映在控制器中,我在OOP中并不那么专业。修改codeigniter模型中的变量值

// controller class structure 
class Leads_import extends CI_Controller { 

public $total = 0; 

    public function import(){ 
    $this->xml_model->imsert(); 
    echo $this->total; 
    } 
} 

// model class structure 
class xml_model extends CI_Model { 

    public function insert(){ 
     this->total = 10; 
    } 
} 

回答

0

试试这个:

// controller class structure 
class Leads_import extends CI_Controller { 

public $total = 0; 

    public function import(){ 
    $this->total = $this->xml_model->imsert(); 
    } 
} 

型号:

// model class structure 
class xml_model extends CI_Model { 

    public function insert(){ 
     return 10; 
    } 
} 
+0

感谢您的帮助,但我在“插入”功能,从功能可按返回别的东西,所以我不能执行其他的事情写这一行“$ this-> total = $ this-> xml_model-> insert()”,因为插入函数做了很多事情,如果你不明白请让我知道,所以我可以显示你完整的代码。 –

0

您必须检查xml_model$total还是有它更新Leads_import$total。你正在读取控制器中的错误变量,它永远不会被更新。

这里是我的建议不知道你真正想要做的事:

class Leads_import extends CI_Controller { 
    public $total = 0; 
    public function import(){ 
    $this->xml_model->insert(); 
    // Read xml_model total and assign to Leads_import total 
    $this->total = $this->xml_model->total; 
    echo $this->total; 
    } 
} 

class xml_model extends CI_Model { 
    public $total = 0; 
    public function insert(){ 
     $this->total = 10; // update xml_model total 
    } 
}