PHP Codeignitor中等效于Rails的脏方法

时间:2019-03-28 12:04:47

标签: php mysql codeigniter phpmyadmin

我是PHP和Codeigniter的新手,但是在PHP中需要某种实现。

默认情况下,Rails框架中提供了以下dirty methods,这里person是表示persons表内行的模型对象。

person.name = 'Bob'
person.changed?       # => true
person.name_changed?  # => true
person.name_changed?(from: nil, to: "Bob") # => true
person.name_was       # => nil
person.name_change    # => [nil, "Bob"]
person.name = 'Bill'
person.name_change    # => [nil, "Bill"]

我对tofrom特别感兴趣,请提出是否可以通过任何方式提出建议。

1 个答案:

答案 0 :(得分:0)

如果您考虑使用Laravel的后续框架,那么您已经拥有了大量的功能。

Laravel Eloquent update just if changes have been made

它在模型中包含一个“原始”值的数组,如果其中任何一个已更改,它将把它们提交到数据库中。

它们还带有许多您可以插入的事件(beforeSave,afterSave,beforeCreate,afterCreate,验证规则等),并且可以轻松扩展它们。我想像中,这可能是最接近的兼容产品。

但是,这不是codeigniter,它依赖于不同的框架。如果您对codeigniter的了解还不多,则可以根据需要考虑切换到Laravel或OctoberCMS之类的框架。

编辑,因为您受制于codeigniter

您可能希望使用像这样的库:https://github.com/yidas/codeigniter-model

然后使用某些自定义缓存机制进行扩展非常容易。

下面的代码可以用作您自己的模型实现的基础。

它具有非常原始的逻辑基础,但是允许您检查脏状态并回滚对模型所做的任何更改。

请注意,这是非常基本的操作,甚至可能包含一些错误,因为我尚未运行此代码。它更是一种概念验证,可帮助您创建适合自己需求的模型。

class User extends CI_Model 
{

     public $table = 'users';

     public $primaryKey = 'id';

     public $attributes;

     public $original;

     public $dirty = [];

     public $exists = false;

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

     }

     public static function find($model_id) 
     {
         $static = new static;
         $query = $static->db->query("SELECT * FROM ' . $static->getTable() . ' WHERE ' . $this->getKeyName() . ' = ?", [$model_id]);
         if($result->num_rows()) {
            $static->fill($query->row_array());
            $static->exists = true;
         }
         else {
             return null;
         }
         return $static;
     }

     public function getKeyName() 
     {
         return $this->primaryKey;   
     }

     public function getKey() 
     {
        return $this->getAttribute($this->getKeyName());  
     }

     public function getTable()
     {
         return $this->table;
     }

     public function fill($attributes) 
     {
         if(is_null($this->original)) {
             $this->original = $attributes;
             $this->dirty = $attributes;
         }
         else {
             foreach($attributes as $key => $value) {
                 if($this->original[$key] != $value) {
                     $this->dirty[$key] = $value;
                 }
             }
         }
         $this->attributes = $attributes;
     }

     public function reset() 
     {
         $this->dirty = [];
         $this->attributes = $this->original;
     }

     public function getAttribute($attribute_name) 
     {
        return array_key_exists($attribute_name, $this->attributes) ? $this->attributes[$attribute_name] : null;    
     }

     public function __get($key) 
     {
        return $this->getAttribute($key);    
     }

     public function __set($key, $value)
     {
         $this->setAttribute($key, $value);
     }

     public function setAttribute($key, $value) 
     {
         if(array_key_exists($key, $this->original)) {
             if($this->original[$key] !== $value) {
                $this->dirty[$key] = $value;
             }
         }
         else {
             $this->original[$key] = $value;
             $this->dirty[$key] = $value;
         }

         $this->attributes[$key] = $value;
     }

     public function getDirty() 
     {
        return $this->dirty;    
     }

     public function isDirty() 
     {
        return (bool)count($this->dirty);    
     }

     public function save() 
     {
         if($this->isDirty()) {
             if($this->exists) 
             {
                 $this->db->where($this->getKeyName(), $this->getKey());
                 $this->db->update($this->getTable(), $this->getDirty());

                 $this->dirty = [];
                 $this->original = $this->attributes;
             }
             else 
             {
                 $this->db->insert($this->getTable(), $this->getDirty());

                 $this->dirty = [];
                 $this->original = $this->attributes;

                 $this->attributes[$this->getKeyName()] = $this->db->insert_id();
                 $this->original[$this->getKeyName()] = $this->getKey();
                 $this->exists = true;
             }

         }
     }
 }

 if($user = User::find(1)) {
      $user->name = "Johnny Bravo";
      $user->save();
 }