【问题标题】:How to get last inserted id, then assign it to a global variable如何获取最后插入的 id,然后将其分配给全局变量
【发布时间】:2014-04-11 21:45:53
【问题描述】:

我想获取最后插入的 id,然后将其分配给全局变量以供以后使用。

我使用callback_after_insert如下:

$crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));

function _callback_get_lastInsertID($post_array,$primary_key) {
   $this->lastInsertedId = $primary_key;
}

然后,当使用$this->lastInsertedId 获取它的值时,我没有找到任何存储的值。

function featured_ad_offer() { 
    echo $this->lastInsertedId; 
    @$data['content'] .= $this->load->view('featured_ad_offer', '', true); 
    $this->load->view('index', $data); 
}

有一条错误消息说Undefined property: Content::$lastInsertedId

现在,该怎么做?

【问题讨论】:

  • 有关您使用 $this 的更多信息会有所帮助。你在同一个对象中吗?你确定它是一开始就设置好的吗?
  • Are you in the same object? -> 是的。 Are you sure it gets set in the first place ? -> 是的。
  • 如果它被设置,那么它在什么时候变得未设置?函数退出后立即?
  • 对不起,我不明白,但我在同一个对象的另一个方法中使用了这个变量。 public function featured_ad_offer() { echo $this->lastInsertedId ; @$data['content'] .= $this->load->view('featured_ad_offer', '', true); $this->load->view('index', $data); }
  • 您应该更新您的 OP,而不是将这么大的代码块复制到评论中。如果您的变量在某一时刻可用,但后来不可用,则说明正在重新设置该变量,或者您访问了错误的对象。

标签: php codeigniter grocery-crud


【解决方案1】:

请在“声明”类本身之后立即尝试declaring 类中的变量(假设两个函数在同一个类中),即

class Blog extends CI_Controller {
    private $lastInsertedId = NULL;

    public function fcn1() {$this->lastInsertedId = 3.14;}
    public function fcn2() {var_dump($this->lastIndertedId);}

}

如果您真的想将其用作global variable,则需要在CI_Controller 中声明它,或者更好的是在/core/Public_Controller 中创建扩展CI_Controller 的文件,并让您的所有控制器不是由CI_Controller 扩展而是Public_Controller 因此,您可以轻松声明“全局”变量。

有关扩展的更多信息,请使用此tutorial by Phil

如果您遇到问题,请联系user guide link

还有另一种选择(不推荐!)config class

$this->config->set_item('item_name', 'item_value'); //setting a config value
$this->config->item('item name'); //accesing config value

【讨论】:

    【解决方案2】:

    也许您可以使用它来获取最后插入的 id

    $this->db->insert_id();
    

    将其存储在会话中

    $this->load->library('session');
    $this>session->set_userdata('last_inserted_id');
    

    并在其他地方使用此会话变量

    $last_inserted_id = $this->session->userdata('last_inserted_id');
    

    希望得到帮助:D

    【讨论】:

      【解决方案3】:

      您需要的解决方案可以通过 Codeigniter 会话轻松解决。例如,你可以做的是:

      $crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));
      
      function _callback_get_lastInsertID($post_array,$primary_key) {
         $this->session->set_userdata('last_insert_id', $primary_key);
      }
      

      确保首先加载了session class。最好的方法是在自动加载时实际拥有会话库。

      现在要再次调用 last_insert_id,您只需调用:

      echo $this->session->userdata('last_insert_id');
      

      所以在你的例子中你可以简单地做:

      function featured_ad_offer() { 
          echo $this->session->userdata('last_insert_id'); 
          @$data['content'] .= $this->load->view('featured_ad_offer', '', true); 
          $this->load->view('index', $data); 
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-25
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多