gpt4 book ai didi

php - Codeigniter Undefined Property,或者我的 Controller 如何停止担心并学会喜欢这个模型

转载 作者:可可西里 更新时间:2023-11-01 13:28:33 25 4
gpt4 key购买 nike

我正在学习 Codeigniter website 中的教程关于制作新闻版 block 。现在我在第 13 行收到一条未定义的属性消息。

这是我的模型。 applications/models/articles_model

<?php

class Articles_model extends CI_Model{

public function __construct(){

$this->load->database();
}

public function get_article($slug = False){

if ($slug === FALSE){

$query = $this->db->get('articles');
return $query->result_array();
}

$query = $this->db->get('articles');
return $query->result_array();
}

$query = $this->db->get_where('articles', array('slug' => $slug));
return $query->row_array();
}

这是 Controller 。 applications/controllers/articles.php

<?php

class Articles extends CI_Controller{

public function __contruct(){

parent::__construct();
$this->load->model('Articles_model');
}

public function index(){

//Message: Undefined property Articles::Articles_model
$data['articles'] = $this->Articles_model->get_article();
$data['title'] = "Archive";

$this->load->view('templates/header', $data);
$this->load->view('articles/index', $data);
$this->load->view('templates/footer');
}

public function view($slug){

$data['articles'] = $this->Articles_model->get_article($slug);

if(empty($data['articles'])){

show_404();
}

$data['title'] = $data['articles']['title'];

$this->load->view('templates/header', $data);
$this->load->view('articles/view', $data);
$this->load->view('templates/footer');
}
}

这些是我设置的所有路线。 applications/config/routes.php

$route['articles/(:any)'] = 'articles/view/$1';
$route['articles'] = 'articles';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

我的数据库是这样的

mysql> describe articles
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(128) | NO | | NULL | |
| slug | varchar(128) | NO | MUL | NULL | |
| text | text | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+

我尝试过使用首字母大写的属性,$this->Articles_model,而不是 $this->articles_model

我错过了什么愚蠢的东西吗?如果有,那是什么?
如果不傻我怎么调试?

编辑

根据评论,print_r 的输出不包含“Articles_model”。第一层的事情在我头上,但 CTRL-F 不是。 Apache 日志也提到了这一点......

[Sun Sep 23 13:19:30 2012] [error] [client 127.0.0.1] PHP Fatal error:  Call to a member function get_article() on a non-object in /home/noel/dev/web/
ci/CodeIgniter_2.1.2/application/controllers/articles.php on line 13

编辑1

我以为我修复了它,但没有,请参阅我的回答 中的评论。由于某些原因 $this->article_model 不是一个对象。

为什么?我去过一次世界博览会、一次野餐和一次牛仔竞技表演,那是我戴上耳机听到的最愚蠢的事情。你确定你得到了今天的代码?

最佳答案

仔细观察,答案很简单:尝试更改此行:

public function __contruct(){

为此:

public function __construct(){

;)

关于php - Codeigniter Undefined Property,或者我的 Controller 如何停止担心并学会喜欢这个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12556913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com