1. 程式人生 > >laravel save\update 方法不更新updated_at 解析

laravel save\update 方法不更新updated_at 解析

場景:

      小型論壇,在帖子的表中維持一個最後一個更新的使用者  

      如果上次評論的使用者的和現在這個評論的使用者是同一個人 則不會更新updated_at 欄位

解決:

       先說怎麼解決 

$last_user_id = $event->comment->user_id;
$discussion_id = $event->comment->discussion_id;
$updated_at = date('Y-m-d H:i:s');
Discuss::where(['id' => $discussion_id])->update(compact('last_user_id'
,'updated_at'));

解析:

Illuminate\Database\Eloquent\Model #527行

     update函式內部呼叫了save   save 呼叫了performUpdate  

/**
 * Perform a model update operation.
 *
 * @param  \Illuminate\Database\Eloquent\Builder  $query
 * @return bool
 */
protected function performUpdate(Builder $query)
{
    // If the updating event returns false, we will cancel the update operation so
// developers can hook Validation systems into their models and cancel this // operation if the model does not pass validation. Otherwise, we update. if ($this->fireModelEvent('updating') === false) { return false; } // First we need to create a fresh query instance and touch the creation and
// update timestamp on the model which are maintained by us for developer // convenience. Then we will just continue saving the model instances. if ($this->usesTimestamps()) { $this->updateTimestamps(); } // Once we have run the update operation, we will fire the "updated" event for // this model instance. This will allow developers to hook into these after // models are updated, giving them a chance to do any special processing. $dirty = $this->getDirty(); if (count($dirty) > 0) { $this->setKeysForSaveQuery($query)->update($dirty); $this->fireModelEvent('updated', false); $this->syncChanges(); } return true; }
/**
 * Update the creation and update timestamps.
 *
 * @return void
 */
protected function updateTimestamps()
{
    $time = $this->freshTimestamp();

    if (! is_null(static::UPDATED_AT) && ! $this->isDirty(static::UPDATED_AT)) {
        $this->setUpdatedAt($time);
    }

    if (! $this->exists && ! $this->isDirty(static::CREATED_AT)) {
        $this->setCreatedAt($time);
    }
}
/**
 * Determine if the model or given attribute(s) have been modified.
 *
 * @param  array|string|null  $attributes
 * @return bool
 */
public function isDirty($attributes = null)
{
    return $this->hasChanges(
        $this->getDirty(), is_array($attributes) ? $attributes : func_get_args()
    );
}

相關推薦

laravel save\update 方法更新updated_at 解析

場景:      小型論壇,在帖子的表中維持一個最後一個更新的使用者        如果上次評論的使用者的和現在這個評論的使用者是同一個人 則不會更新updated_at 欄位解決:       先說怎麼解決 $last_user_id = $event->commen

解決Hibernate4執行update操作,更新數據的問題

current spa username tor color code soft etime 解決方法 後臺封裝java對象,使用hibernate4再帶的update,執行不更新數據,不報錯。 下面貼出解決方法: 失敗的方法 hibernate自帶update代碼:(失效

hibernate中的update方法更新部分欄位的方法

Hibernate 中如果直接使用 Session.update(Object o); 會把這個表中的所有欄位更新一遍。 比如: ? 1 2 3 4 5 6 7 8 9 10 11 12 public class Teacher

Hibernate 註解序列生成主鍵執行完select seq_t_user.nextval後執行insert等語句導致 執行save()或update()方法無效

hiberna 不能 nal 自動提交 ext 無效 pen mave ransac 題主解決方法: 1)在DAO中獲取session的時候采用sessionFactory.getCurrentSession();不用

Hibernate的Session介面中save/delete/update方法2個引數(entityName)的深入解析

Hibernate的Session介面中delete/update方法2個引數(entityName)的深入解析 2010-09-14 18:07 session.update(Object arg0); session.update(String arg0, Obje

Hibernate的save方法能進行數據庫插入

tom 插入 con gda 自動 ref fig 分享 save 問題描述 在 MyEcplise 上運行 tomcat,利用 po 模板自動生成 po 文件,調用 po 的 save 方法,不報錯,但是無法把數據插入數據庫 applicationContext.xml

【Vue】解決路由切換,頁面更新的實用方法

前言:vue-router的切換不同於傳統的頁面的切換。路由之間的切換,其實就是元件之間的切換,不是真正的頁面切換。這也會導致一個問題,就是引用相同元件的時候,會導致該元件無法更新,也就是我們口中的頁面

CENTOS伺服器yum update時怎樣更新安全補丁,過濾掉核心升級核心版本呢?

您在購買阿里雲伺服器後,RedHat/CentOS/Fedora使用  yum update  更新時,預設會都會升級核心,比如從CENTOS6.8升級到CENTOS6.9,結果可能導致php5.2等無法使用;有些伺服器硬體(特別是組裝的機器)在升級核心後,新的核心可能也會認

MongoDB學習筆記~Update方法更新集合屬性後的怪問題

回到目錄 在對MongoDB進行封裝後,對於Update更新物件裡的集合屬性時出現了一個現象,讓人感到很噁心,人家更新前是個美麗的Array,但是更新之後集合物件變成了鍵值對,鍵是集合的型別名稱,值是真實的陣列值,哈哈,這個問題起初困擾了我很久,今天終於豁然開朗了,原來是Update方法的問題,呵呵! 看

EF更新資料庫結構seed方法執行

開始學習EF,文章http://www.cnblogs.com/farb/p/FirstCodeFirstApp.html,在dbContext新增表的時候重寫 DropCreateDatabaseAlways<Context>的Seed方法不起總用,沒有新建表

DedeCMS模板檔案存在,無法解析文件! 問題定位方法

(2)如果是生成文件頁面出現這個錯誤提示,開啟arc.archives.class.php這個檔案,找到第484行。修改一下報錯的程式碼:echo "模板檔案不存在,無法解析文件!";   Dedecms5.6使用教程 -js200.CN  修改成:echo "模板檔案不存在,無法解析文件!" .$this-

Mybatis能正確解析typeAliases問題的處理方法

Mybatis配置檔案中使用typeAliases竟然報錯,明明用的是typeAliases,並沒有拼錯。 原來是typeAliases與environments相對順序的問題 更改前配置檔案示

Docker push似乎更新image =>層已經存在 [英]Docker push seems not to update image => Layer already exists

I'm building an elasticsearch image with Dockerfile: 我正在用Dockerfile構建一個彈框搜尋影象: FROM alpine:latest RUN apk update \     && apk

傲遊瀏覽器 頁面廣告過濾 自定義 過濾規則 及使用方法 定時更新

使用方法: 1.開啟傲遊瀏覽器,開啟以下列表中需要過濾廣告的域名 2.單擊傲遊瀏覽器右下角廣告獵手,點選編輯過濾規則 3.將以下列表的規則貼上到右側輸入框中 4.點選完成,頁面會自動重新整理,完成後頁面廣告消失 baidu.com(包含百度音樂盒、百度知道、百度貼吧

JAVA線程池原理源碼解析—為什麽啟動一個線程池,提交一個任務後,Main方法會退出?

ces 內部 pools let 實例 工作 cte hello rep 起因 public static void main(String[] args) { ExecutorService service = Executors.newFixedThr

Hibernate save()和insert()方法能寫入資料庫的問題

session.save() getHibernateTemplate.save()    應用MyEclipse整合的Hibernate工具自動生成的程式碼,經常會遇到這種不寫資料的問題.也就是當你執行一條增加或修改語句時,程式執行正常沒有異常,檢視sql語句確實執行了in

多執行緒委託之跨執行緒問題分析--在建立視窗控制代碼之前,能在控制元件上呼叫 Invoke 或 BeginInvoke(解決方法更新)

檢視巢狀檢視+groupby+sum+if超慢?檢視巢狀檢視+groupby+sum+if超慢? 炯蕉蔚郝iar貉k湯秤TP2Fx扯訃詬壤撞蝸 《 http://babyknow.baidu.com/article/1376a5480527629546e457877078

也來說說關於Hibernate使用update更新資料時,更新的問題

現在使用hibernate框架的雖然沒有前2年那麼多了,但是還是有很多專案在使用hibernate框架的,畢竟作為一個優秀的持久層框架,hibernate自然有存在的價值。 好了,廢話不多說,結合最近的使用,和大家一起探討一個hibernate經常遇到的問題: 問題描述:

織夢 dedecms “模板檔案存在,無法解析文件!” 解決方法分享

dedecms後臺生成html,出現這樣的提示:“模板檔案不存在,無法解析文件!”這個問題很多人都遇到過 我們先清空下快取。 第一。首先開啟:/incluede/arc.archives.class.php 查詢 “模板檔案不存在,無法解析文件” 這行字,然後把整行修改為 echo "模板檔案不存在,無法解

關於更新部分屬性,Hibernate更新某些欄位的幾種update方法

 1.在hbm.xml中設定property 標籤 update = “false” 例如     <property name=”age”update=”false”></property>     我們在執行 Update方法會發現,age 屬