codeigniter $ this-> save()无效

时间:2016-07-16 09:50:20

标签: php codeigniter ubuntu-14.04 datamapper

我最近开始使用codeigniter。尝试为新表创建新模型。似乎save()方法无法正常工作。我无法在我的表中看到新记录。我对net和stackoverflow进行了一些研究,但没有用。有人可以检查我的代码和建议。



<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2 
  3 class Listing_Price_History extends DataMapper
  4 {
  5 
  6     var $table = 'listing_price_history';
  7     var $primary_key = 'id';
  8     var $primary_key_suffix = '_uid';
  9 
 10 
 11     // --------------------------------------------------------------------
 12         /** Validations for the requried fields */
 13     var $validation = array(
 14         'listing_id' => array(
 15                 'label' => 'Listing Id',
 16                 'rules' => array('required')
 17         ),
 18         'price_per_sqft' => array(
 19                 'label' => 'Price per square feet',
 20                 'rules' => array('required')
 21         )
 22     );
 23 
 24     // --------------------------------------------------------------------
 25 
 26     public function __construct() {
 27         parent::__construct();
 28         $CI =& get_instance();
 29         $CI->load->library('uuid');
 30         $this->uuid = $CI->uuid;
 31         $CI->load->helper('company');
 32 
 33     }
 34 
 35     // --------------------------------------------------------------------
 36 
 37     // --------------------------------------------------------------------
 38 
 39     /**
 40      * Create listing entry
41      *
 42      * @return bool
 43      **/
 44     public function savePriceHistory($history) {
 45           //  var_dump($history); 
 46           //  var_dump($this);
 47             if(isset($history['listing_id']))
 48               $this->listing_id = $history['listing_id'];
 49             if(isset($history['price_per_sqft']))
 50               $this->price_per_sqft = $history['price_per_sqft'];
 51             $date = date('Y-m-d H:i:s');
 52             $this->created_by = $this->uuid;
 53             $this->created_date = $date;
 54             $save_history = $this->save();
 55           //  var_dump($this->check_last_query());
 56           //  var_dump($this->db->_error_message());
 57           //  var_dump($this->db->_error_number());
 58           //  var_dump($save_history);
 59     }
 60 }
                                                                                                                              60,1          Bot
&#13;
&#13;
&#13;

控制器: 这就是我调用模型的方式

&#13;
&#13;
 432             if($status){
 433               echo "Entered";
 434               $price_history = new Listing_Price_History();
 435               $save_history =  $price_history->savePriceHistory($history);
 436               echo $save_history;
 437               echo "Finished";
 438             }
&#13;
&#13;
&#13;

这是我的表结构

enter image description here

请让我知道我做错了什么。

2 个答案:

答案 0 :(得分:0)

我不时会遇到这个问题,必须提醒自己,在一些codeigniter系统上,datamapper会在缓存目录中创建用于保存数据库表元数据的文件。如果向数据库添加一列,如果存在缓存文件,则无法自动访问该列。

因此,在codeigniter中,转到app / cache目录并删除与您的表同名的文件。您应该发现它是在第一次将它与新表模式一起使用时重建的,并且它将全部工作

答案 1 :(得分:0)

对于当今面临这些问题的人们: 尝试向表模型添加另一个属性 primaryKey,如下所示:

受保护的 $primaryKey = 'id';

它帮助了我。 Codeigniter 4 save() 方法应该知道,你的键是什么列,特别是如果列名与典型的“id”名称不同。 就我而言,表的 id 正是“user_id”,这是一个问题。