【问题标题】:creating news item in codeigniter在 codeigniter 中创建新闻项目
【发布时间】:2014-05-16 05:42:06
【问题描述】:

我想使用 slug 在 codeigniter 中创建新闻项目,但错误显示是这样的。

> Call to a member function insert() on a non-object in line 34

我是 Codeigniter 的新手,无法真正弄清楚如何解决这个问题。

我的控制器

    function tambah_profil()
        {
            $this->data['title'] ='create a new items';

            $this->form_validation->set_rules('judul', 'Judul', 'required');
            $this->form_validation->set_rules('content', 'Content', 'required');

            if ($this->form_validation->run() == FALSE) 
            {
                $this->data['contents'] = $this->load->view('admin/profil/tambah_profil', '', true);

            }else{

                $this->mhalaman->insert_profil();
                $this->data['contents'] = $this->load->view('admin/profil/view_profil', '', true);

            }

            $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
            $this->data['contents'] = $this->load->view('admin/profil/tambah_profil', '', true);
            //$this->data['contents'] = 'admin/profil/tambah_profil';
            $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
        }

我的模特

var $db;
    private $tbl_halaman = 'halaman';

    public function __construct()
    {
        parent ::__construct();
        $this->load->database();
    }

    function get_profil($slug = FALSE)
    {
        if ($slug === FALSE)
        {
            $query = $this->db->get($this->tbl_halaman);
            return $query->result_array();
        }

        $query = $this->db->get_where($this->tbl_halaman, array('slug'=>$slug));
        return $query->row_array();
    }
function insert_profil()
        {
            $slug = url_title($this->input->post('judul'),'dash', TRUE);

            $data = array(
                'judul'         => $this->input->post('judul'),
                'slug'          => $slug,
                'content'       => $this->input->post('content')
            );
            return $this->db->insert($this->tbl_halaman ,$data); //line 34
        }

请帮我做什么。谢谢。

【问题讨论】:

  • 你加载数据库了吗?
  • 是的,我有。我在这样的结构中加载数据库 $this->load->model(array('mlogin','mhalaman'));那该怎么办?
  • 不必返回$this->db->insert($this->tbl_halaman ,$data)ellislab.com/codeigniter/user-guide/database/active_record.html 供参考使用插入功能。
  • 我已删除“返回”但同样的错误。怎么样?

标签: php codeigniter slug


【解决方案1】:

请检查你是否在模型构造函数中加载了$this->load->database()。

$this->db->method_name();只有在加载数据库库时才能工作。

如果您打算在整个应用程序中使用数据库,我建议您将其添加到 /application/config/ 中的 autoload.php 中。

$autoload['libraries']=array('database');

【讨论】:

  • 你是否扩展了 CI_Model 也可以确保以下 Your_Model 扩展 CI_Model { function __construct() { parent::__construct(); } }
  • 是的,我有。这个问题与获取模式中的所有数据有什么关系吗?我想编辑和显示代码,你能看到吗?
  • 这个 var $db 是什么。调用 $this->db->insert() 你不需要声明任何 $db 变量。请确保你有这样的“class Profilemodel extends CI_Model {”
猜你喜欢
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 2017-03-02
  • 2010-11-28
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多