[英]Codeigniter model shows Undefined property error on helper
Here is my helper function in a helper file misc_helper.php, 这是帮助文件misc_helper.php中的帮助函数,
function echoEditorsPickItems($projects = array('02134444', '0314020000')) {
if (!is_null($projects)) {
$CI = &get_instance();
$CI->load->database();
$CI->load->model('Project');
$projects = $CI->Project->getAllProjects(); // this is the 28th line
// getAllProjects() returns
// an arrays of item
for ($i=0; $i<EDITORS_PICK_ITEM_LIMIT; $i++) {
// $CI->Project->getItem('0214010001');
$out = '';
$out .= '<li>';
$out .= '<span>';
$out .= '<a href="'. base_url() . 'portfolio/detail/' . $projects[$i]['pId'] .'" class="post-img">';
$out .= '<img src="'. base_url() . 'assets/img/blog/' . $projects[$i]['brandImageUrl'] . '" alt="' . $projects[$i]['title'] . '">';
$out .= '<span class="overlay"></span>';
$out .= '</a>';
$out .= '</span>';
$out .= '<p class="wrap">';
$out .= '<a href="'. base_url() . 'portfolio/detail/' . $projects[$i]['pId'] .'" class="rp_title">' . $projects[$i]['title'] . '</a>';
$out .= '<small class="rp_date">' . $CI->Project->title . '</small>';
$out .= '</p>';
$out .= '</li>';
echo $out;
}
}
}
When I call it, I get the following error, 调用它时,出现以下错误,
Severity: Notice
Message: Undefined property: Project::$Project
Filename: helpers/misc_helper.php
Line Number: 28
Edit: Here is my model, 编辑:这是我的模型,
class Project extends CI_Controller {
private $tableName = DB_TABLE_NAME_PROJECT;
public $title;
public $description;
public $client;
public $skills;
public $preparedBy;
public $catagory;
public $date;
public $url;
public $images;
public $templateType;
function __construct() {
parent::__construct();
$this->load->database();
}
public function getItem($projectId = NULL) {
if (!is_null($projectId)) {
$res = $this->db->get_where($this->tableName, array('pId' => $projectId));
if ($res->num_rows) {
$this->title = $res->row(1)->title;
$this->description = $res->row(1)->description;
$this->client = $res->row(1)->client;
$this->preparedBy = $res->row(1)->preparedBy;
$this->skills = explode(TOKEN_DELIMINATOR, $res->row(1)->skills);
$this->catagory = $res->row(1)->catagory;
$this->date = $res->row(1)->date;
$this->url = $res->row(1)->url;
$this->images = explode(TOKEN_DELIMINATOR, $res->row(1)->images);
$this->templateType = ($res->row(1)->templateType=='1'? 1:0);
return TRUE;
}
return FALSE;
}
return FALSE;
}
public function getAllProjects() {
$res = $this->db->get($this->tableName);
$projects = array();
for ($i=0; $i<$res->num_rows(); $i++){
$projects[$i] = array();
$projects[$i]['title'] = $res->row($i)->title;
$projects[$i]['catagory'] = explode(TOKEN_DELIMINATOR, $res->row($i)->catagory);
$projects[$i]['brandImageUrl'] = $res->row($i)->brandImageUrl;
$projects[$i]['pId'] = $res->row($i)->pId;
}
return $projects;
}
}
The CI Instance should created with reference. CI实例应参考创建。
$CI = &get_instance();
Edit: 编辑:
You did mistake in your model. 您在模型中犯了错误。
class Project extends CI_Model { // you should extend model not controller
What are you trying to acheive? 您想达到什么目的? By the looks of it you're trying to loop through all projects (or perhaps a certain number of them) and display them?
从外观上看,您正在尝试遍历所有项目(或可能是一定数量的项目)并显示它们?
If that's the case then firstly (as kumar_v has said) you need to pass the get_instance();
如果是这样,那么首先(如kumar_v所说),您需要传递
get_instance();
function by reference as this will allow you to use the original CodeIgniter object rather than creating a copy of it. 引用功能,因为这将允许您使用原始CodeIgniter对象,而不是创建它的副本。 http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
Secondly, it looks like you're trying to access a property of each project (pId, brandImageUrl, title). 其次,您似乎正在尝试访问每个项目的属性(pId,brandImageUrl,标题)。 If this is the case, you need to assign
$CI->Project->getAllProjects();
在这种情况下,您需要分配
$CI->Project->getAllProjects();
to a variable and loop through these results. 到变量并遍历这些结果。 eg
例如
function echoEditorsPickItems($projects = array('02134444', '0314020000')) {
if (!is_null($projects)) {
$CI =& get_instance();
$CI->load->model('Project');
$projects = $CI->Project->getAllProjects();
foreach ($projects as $project) {
$out = '';
$out .= '<li>';
$out .= '<span>';
$out .= '<a href="' . base_url() . 'portfolio/detail/' . $project->pId . '" class="post-img">';
$out .= '<img src="' . base_url() . 'assets/img/blog/' . $project->brandImageUrl . '" alt="' . $project->title . '">';
$out .= '<span class="overlay"></span>';
$out .= '</a>';
$out .= '</span>';
$out .= '<p class="wrap">';
$out .= '<a href="' . base_url() . 'portfolio/detail/' . $project->pId . '" class="rp_title">' . $project->title . '</a>';
$out .= '<small class="rp_date">' . $project->title . '</small>';
$out .= '</p>';
$out .= '</li>';
echo $out;
}
}
}
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.