【问题标题】:Save error in CodeIgniter and Doctrine tutorial在 CodeIgniter 和 Doctrine 教程中保存错误
【发布时间】:2013-01-17 15:17:14
【问题描述】:

我想在 Doctrine 2.3 中保存数据,不幸的是,如果可能我需要很好的教程,我也会出错,因为我认为文档没有用。

我的最后一个话题:

Controller error in CodeIgniter and Doctrine tutorial

厄洛尔:

致命错误:在第 19 行调用 C:\wamp\www\nauka\application\controllers\hello.php 中未定义的方法 User::save()

带有保存选项的图片:

型号

ripa 我做了你想要的,但我得到了错误:

解析错误:解析错误,在第 8 行的 C:\wamp\www\nauka\application\controllers\hello.php 中需要 `T_FUNCTION'

<?php
    // system/application/controllers/hello.php

    $this->load->model("user");

    class Hello extends CI_Controller {

            $this->user->setTableDefinition();

        function world() {
            echo "Hello CodeIgniter!";
        }

        function user_test() {

            $u = new User;
            $u->username = 'johndoe';
            $u->password = 'secret';
            $u->first_name = 'John';
            $u->last_name = 'Doe';
            $u->save();

            $u2 = new User;
            $u2->username = 'phprocks';
            $u2->password = 'mypass';
            $u2->first_name = 'Codeigniter';
            $u2->last_name = 'Doctrine';
            $u2->save();

            echo "added 2 users";
                    }

}

?>

【问题讨论】:

  • 好像你没有在 User 处扩展 Doctrine,而且 User 没有保存等方法。
  • @qeremy 你能在帖子中展示示例吗?
  • 其实我没用过Doctrine,但是心理上需要在User类扩展一个类(有save方法)或者在User中定义save方法。

标签: php codeigniter doctrine


【解决方案1】:
 <?php



class Hello extends CI_Controller {

        $this->user->setTableDefinition();

    function world() {
        echo "Hello CodeIgniter!";
    }

    function user_test() {

        $this->load->model("user");
        $u["username"] = 'johndoe';
        $u["password"] = 'secret';
        $u["first_name"] = 'John';
        $u["last_name"] = 'Doe';
        $this->user->save($u);



        echo "added 1 users";
                }

    }

?>

可以从http://ellislab.com/codeigniter/user-guide/general/models.html 获得帮助。请参阅此链接的“目录”。您将获得与 codeigniter 相关的所有内容。

【讨论】:

    【解决方案2】:

    在 codeigniter 中,所有模型都将加载到控制器中。 require_once 将不起作用。首先在 $this-&gt;load-&gt;model("your model name") 这样的 costructor 中加载您的模型。然后从控制器调用模型函数,如$this-&gt;model_name-&gt;function_name()

    【讨论】:

      【解决方案3】:

      你可以这样保存:

      $this->load->model('user');
      
      $this->user->save_User('username','password','first_name','last_name');
      

      模型文件:

      class user extends CI_Model {
      
      
          function __construct()
          {
              // Call the Model constructor
              parent::__construct();
          }
      
           function save_User($username,$password,$fName,$lName)  {
      
                //Save to database
           }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-23
        • 1970-01-01
        • 2012-07-05
        相关资源
        最近更新 更多