2015-05-29 36 views
0

對不起我的英語不好:(更新僅當變化來檢測(自動)

我使用CakePHP的3框架,我想更新行在表格中的行(實體) (實體)在表中僅當的變化來檢測(自動地)。

例如:

$id = 1; 
// debug($this->request->data) 
$this->request->data = ['id' => 1, 'name' => "My Name", 'content' => "My Content"]; 
// Inside the ArticlesController 
$article = $this->Articles->get($id); 
// debug($article) 
object(Cake\ORM\Entity) { 
    'id' => (int) 1, 
    'name' => "My Name", 
    'content' => "My Content", 
    '[new]' => false, 
    '[accessible]' => [ 
     '*' => true 
    ], 
    '[dirty]' => [], 
    '[original]' => [], 
    '[virtual]' => [], 
    '[errors]' => [], 
    '[repository]' => 'Articles' 
} 

沒有d 。差分請求數據和之間原始數據所以我必須拒絕更新查詢返回警告檢測到任何更改用戶「! ?

我有什麼做的

感謝

回答

0

每個實體都有一個字段‘髒’這說明,如果實體已經更新

您可以使用類似:

if($article->dirty()) { 
    // send a flash message 
} 

檢查書在這裏:http://book.cakephp.org/3.0/en/orm/entities.html#checking-if-an-entity-has-been-modified

+0

沒有髒方法不檢測特定鍵的值是否已經改變:(如果你在'''$ this->'之後加了'''article'> name =「我的名字」'''''' , >文章 - > get($ id);'''你會得到''''[dirty]'=> ['name'=> true]'' –

+0

是不是你想要的?您將能夠檢查'ArticlesEntity'是否更新'ArticlesController'中的實體。 – Jun

+0

yes'''patchEntity'''會猜出**信號數據**和**請求數據** :)之間的區別 –