2015-05-29 35 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'''会猜出**信号数据**和**请求数据** :)之间的区别 –