【问题标题】:datamapper orm on codeigniter doesn't workcodeigniter 上的 datamapper orm 不起作用
【发布时间】:2012-09-15 22:11:56
【问题描述】:

祝大家有美好的一天。我正在 Codeigniter 上尝试 DATAMAPPER ORM(wan 向导)。示例应用程序工作正常。但是当我尝试制作自己的模型和控制器时,它不起作用。我按照说明做了每一步。这是一个代码:

class Blog extends DataMapper {

var $has_one = array();
var $has_many = array();
var $validation = array(
    'content' => array(
        // example is required, and cannot be more than 120 characters long.
        'rules' => array('required', 'max_length' => 255),
        'label' => 'Content'
    )
);
function __construct($id = NULL)
{
    parent::__construct($id);
}

}

我在 db 中创建了表(博客有一行称为内容)。

这是一个控制器:

class Blog extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $blog = new Blog;
        $blog->content = "shaa";
        $blog->save();
        echo "done";
    }

}

但它总是给我一个错误:Fatal error: Call to undefined method Blog::save() in C:\xampp\htdocs\wanwizarddatamapper\application\controllers\blog.php on line 29

呃,这让我很沮丧!你能帮助我吗?谢谢

【问题讨论】:

  • 您是否有理由需要使用数据映射器?如果没有,我会使用 CodeIgniter 活动记录类的常规模型。

标签: codeigniter codeigniter-datamapper


【解决方案1】:

我相信您的模型和控制器之间存在命名冲突。尝试将您的模型重命名为BlogEntry

class BlogEntry extends DataMapper {

  var $has_one = array();
  var $has_many = array();
  var $validation = array(
    'content' => array(
      // example is required, and cannot be more than 120 characters long.
      'rules' => array('required', 'max_length' => 255),
      'label' => 'Content'
    )
  );
  function __construct($id = NULL)
  {
    parent::__construct($id);
  }

}

class Blog extends CI_Controller {

  function __construct()
  {
    parent::__construct();
  }

  public function index()
  {
    $blogentry = new BlogEntry;
    $blogentry->content = "shaa";
    $blogentry->save();
    echo "done";
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多