您好,欢迎访问一九零五行业门户网

CakePHP 2.x CookBook 中文版 第七章 模型 之 保存数据_PHP教程

保存数据
cakephp 会为保存模型数据制作快照。准备保存的数据使用如下基本格式传递给模型的 save() 方法:
1 array
2 (
3     [modelname] => array
4     (
5         [fieldname1] => 'value'
6         [fieldname2] => 'value'
7     )
8 )
多数时候你无需担心这种格式: cakephp 的 formhelper 和模型的 find 方法都用这种格式打包所有数据。如果使用其它的助手,数据也能方便地以 $this->request->data 形式使用。
下面是使用 cakephp 模型向数据库表存入数据的控制器动作的示例:
1 public function edit($id) {
 2     // 有表单数据被 post?
 3     if ($this->request->is('post')) {
 4         // 如果表单数据能够通过校验并保存...
 5         if ($this->recipe->save($this->request->data)) {
 6             // 设置 session 跳转信息并跳转
 7             $this->session->setflash('recipe saved!');
 8             $this->redirect('/recipes');
 9         }
10     }
11 
12     // 如果没有表单数据,查找被编辑的 recipe 并将其赋值给视图。
13     $this->set('recipe', $this->recipe->findbyid($id));
14 }
在 save 方法被调用时,在第一个参数中传递给它的数据,被 cakephp 校验机制校验(更多信息请参见 数据校验 一节)。 如果因为某些原因,数据没有被保存,检查一下是不是没有符合某些校验规则。 可以通过输出model::$validationerrors 来 debug 这种情况。
1 if ($this->recipe->save($this->request->data)) {
2     // 保存 成功后的处理逻辑
3 }
4 debug($this->recipe->validationerrors);
其它一些与保存相关的有用的模型方法:
model::set($one, $two = null)
model::set() 能够用于将数据的一个或多个列放入模型的 data 数组。当使用带有由 model 提供的 activerecord 特性的模型时很有用:
1 $this->post->read(null, 1);
2 $this->post->set('title', 'new title for the article');
3 $this->post->save();
此例展示了如何使用 activerecord 的 set() 方法更新和保存单个列。还可以使用 set() 给多个列赋新值:
1 $this->post->read(null, 1);
2 $this->post->set(array(
3     'title' => 'new title',
4     'published' => false
5 ));
6 $this->post->save();
上例将更新 thitle 和 published 列并保存到数据库中。
model::save(array $data = null, boolean $validate =true, array $fieldlist = array())
这个方法保存数组格式的数据。第二个参数允许跳过校验,第三个参数允许提供要保存的模型的列的列表。为了提高安全性,可以使用 $fieldlist 限制要保存的列。
注解
如果不提供 $fieldlist,恶意用户能够向表单数据中添加附加的列(在你没有使用 securitycomponent 的情况下),并通过这种方法来改变原本不可以被改变的列。
save 方法还有一个替代语法:
1 save(array $data = null, array $params = array())
$params 数组可以用如下选项作为其键:
validate 设置为 true/false 能够 允许/禁止 校验。
fieldlist 允许保存的列构成的数组。
callbacks 设置为 false 将禁止回调。使用 ‘before’ 或 ‘after’ 将仅允许指定的回调。
关于模型回调的更多信息请参见 这里
小技巧
如果你不想更新列在保存某些数据时被更新,在 $data 数组中添加 'updated' => false。
一旦保存完成,可以使用模型对象的 $id 属性获得对象的 id - 在创建新对象时可能会非常有用。
1 $this->ingredient->save($newdata);
2 $newingredientid = $this->ingredient->id;
创建或更新是通过模型的 id 列来控制的。如果设置了 $model->id,带有这个主键的记录将被更新。 其它情况下,一条新记录被创建:
1 // 创建新记录: id 没有设置或设置为 null
2 $this->recipe->create();
3 $this->recipe->save($this->request->data);

5 // 更新记录: id 被设置为一个数字值
6 $this->recipe->id = 2;
7 $this->recipe->save($this->request->data);
小技巧
在循环中调用 save 时,不要忘记调用 create() 。
如果想更新一个值,而不是创建一条新记录,必须确保向数据数组传递了主键列:
1 $data = array('id' => 10, 'title' => 'my new title');
2 // 将更新 id 为 10 的 recipe 记录
3 $this->recipe->save($data);
model::create(array $data = array())
这个方法为保存新信息重置模型的状态。 实际上它并不在数据库中创建新记录,而是清除预先设置的 model::$id,并在 model::$data 中设置基于数据库列默认的默认值。
如果传递了 $data 参数(使用上面描述的数组格式),模型实例将准备保存这些数据(使用 $this->data)。
如果用 false 代替一个数组传递给此方法,模型实际将不根据之前没有设置的模型结构来初始化列,而仅仅重置已经设置的列, 并且保留未设置的列。 这么做是为了避免更新数据库中已经设置的列的值。
小技巧
如果想要用插入一个新行来代替更新已经存在的一行,必须先调用 create()。这样能够避免与回调或者其它位置中曾调用过的 save 发生冲突。
model::savefield(string $fieldname, string$fieldvalue, $validate = false)
用于保存单个列的值。在使用 savefield() 之前要先设置模型的 id ($this->modelname->id = $id)。在使用这个方法时,$fieldname 仅需要包含列名,不需要模型名和列。
例如,更新一条博客的标题,可以用如下方式在控制器中调用 savefield:
1 $this->post->savefield('title', 'a new title for a new day');
警告
在使用这个方法更新时不能停止更新列,你需要使用 save() 方法。
savefield 方法也有一个替代语法:
1 savefield(string $fieldname, string $fieldvalue, array $params = array())
$params 数组可以用如下选项作为其键:
validate 设置为 true/false 能够 允许/禁止 校验。
callbacks 设置为 false 将禁止回调。使用 ‘before’ 或 ‘after’ 将仅允许指定的回调。
model::updateall(array $fields, array $conditions)
一次调用更新一条或多条记录。被更新的记录通过 $conditions 数组标识,$fields 参数指定的列和值被更新。
例如,批准所有成为会员超过一年的面包师,调用如下的更新语句:
1 $this_year = date('y-m-d h:i:s', strtotime('-1 year'));

3 $this->baker->updateall(
4     array('baker.approved' => true),
5     array('baker.created $this_year)
6 );
小技巧
$fields 数组接受 sql 表达式。字面值使用 sanitize::escape() 手动引用。
注解
即使列中存在的编辑列被更新,它也不会通过 orm 自动更新。必须手动将其加入到你想更新的数组中。
例如,关闭所有属于指定客户的所有门票:
1 $this->ticket->updateall(
2     array('ticket.status' => 'closed'),
3     array('ticket.customer_id' => 453)
4 );
默认情况下,updateall() 将自动连接支持 join 的数据库的 belongsto 关联。通过临时绑定关联能够防止这种连接。
model::savemany(array $data = null, array $options= array())
此方法用于同时保存同一模型的多行。可以带有如下选项:
validate: 设置为 false 将禁止校验,设置为 true 将在保存前校验每条记录,设置为 ‘first’(此为默认值) 将在任意一条被保存前检查 全部 记录。
atomic: 如果为 true(默认),将在单个指令中保存所有记录,如果 数据库/表 不支持单指令需要设置为 false。
fieldlist: 与 model::save() 方法的 $fieldlist 参数相同。
deep: (自 2.1 版开始)如果设置为 true,关联数据也被保存,参见 saveassociated。
为单个模型保存多条记录,$data 需要是数字索引的记录数组:
1 $data = array(
2     array('title' => 'title 1'),
3     array('title' => 'title 2'),
4 );
注解
我们传递了数字索引代替了通常情况下 $data 包含的 article 键。在保存同一模型的多条记录时,记录数组需要使用数字索引,而不是模型的键。
它也可以接受如下格式的数据:
1 $data = array(
2     array('article' => array('title' => 'title 1')),
3     array('article' => array('title' => 'title 2')),
4 );
如果还要保存带有 $options['deep'] = true 的关联数据,上面的两个例子将类似于下面的代码:
1 $data = array(
2     array('title' => 'title 1', 'assoc' => array('field' => 'value')),
3     array('title' => 'title 2'),
4 );
5 $data = array(
6     array('article' => array('title' => 'title 1'), 'assoc' => array('field' => 'value')),
7     array('article' => array('title' => 'title 2')),
8 );
9 $model->savemany($data, array('deep' => true));
切记,如果想用更新记录代替创建新记录,需要向数据行添加主键索引:
1 $data = array(
2     array('article' => array('title' => 'new article')), // 创建新记录
3     array('article' => array('id' => 2, 'title' => 'title 2')), // 更新存在的记录
4 );
model::saveassociated(array $data = null, array$options = array())
此方法用于一次保存多个模型关联。可以带有如下选项:
validate: 设置为 false 将禁止校验,设置为 true 将在保存前校验每条记录,设置为 ‘first’(此为默认值) 将在任意一条被保存前检查 全部 记录。
atomic: 如果为 true(默认),将在单个指令中保存所有记录,如果 数据库/表 不支持单指令需要设置为 false。
fieldlist: 与 model::save() 方法的 $fieldlist 参数相同。
deep:(自 2.1 版开始)如果设置为 true,关联数据也被保存,参见 saveassociated。
为了保存记录的同时保存与其有着 hasone 或者 belongsto 关联的记录,data 数组看起来就像下面这样:
1 $data = array(
2     'user' => array('username' => 'billy'),
3     'profile' => array('sex' => 'male', 'occupation' => 'programmer'),
4 );
为了保存记录的同时,保存与其有着 hasmany 关联的记录,data 数组看起来就像下面这样:
1 $data = array(
2     'article' => array('title' => 'my first article'),
3     'comment' => array(
4         array('body' => 'comment 1', 'user_id' => 1),
5         array('body' => 'comment 2', 'user_id' => 12),
6         array('body' => 'comment 3', 'user_id' => 40),
7     ),
8 );
为了保存记录的同时保存与其有着超过两层深度的 hasmany 关联的记录,data 数组看起来就像下面这样:
1 $data = array(
 2     'user' => array('email' => 'john-doe@cakephp.org'),
 3     'cart' => array(
 4         array(
 5             'payment_status_id' => 2,
 6             'total_cost' => 250,
 7             'cartitem' => array(
 8                 array(
 9                     'cart_product_id' => 3,
10                     'quantity' => 1,
11                     'cost' => 100,
12                 ),
13                 array(
14                     'cart_product_id' => 5,
15                     'quantity' => 1,
16                     'cost' => 150,
17                 )
18             )
19         )
20     )
21 );
注解
如果保存成功,主模型的外键将被存储在相关模型的 id 列中,例如 $this->relatedmodel->id。
警告
在调用 atomic 选项设置为 false 的 saveassociated 方法时要小心的进行检查,它返回的是一个数组,而不是逻辑值。
在 2.1 版更改: 现在你可以保存深层关联的数据(用 $options['deep'] = true 设置)。
为了保存记录的同时,保存与其有 hasmany 关联的相关记录及深层关联的 comment belongsto user 数据,data 数组看起来就像下面这样::
1 $data = array(
2     'article' => array('title' => 'my first article'),
3     'comment' => array(
4         array('body' => 'comment 1', 'user_id' => 1),
5         array('body' => 'save a new user as well', 'user' => array('first' => 'mad', 'last' => 'coder')),
6     ),
7 );
并用如下语句进行保存:
1 $article->saveassociated($data, array('deep' => true));
在 2.1 版更改: model::saveall() 和同族方法现在支持为多个模型传递 fieldlist。
为多个模型传递 fieldlist 的例子:
1 $this->somemodel->saveall($data, array(
2     'fieldlist' => array(
3         'somemodel' => array('field_1'),
4         'associatedmodel' => array('field_2', 'field_3')
5     )
6 ));
fieldlist 是一个以模型别名为键,以列构成的数组作为值的数组。 模型名如同在被保存的数据中那样,不能嵌套。
model::saveall(array $data = null, array $options =array())
saveall 函数只是 savamany 和 saveassociated 方法的包装器。它检查数据并且决定执行哪种数据保存类型。它查看数据并决定执行哪种类型的保存。如果数据是数字索引数组,savemany 被调用,否则 saveassociated 被调用。
此函数的选项与前面的两个函数相同,并向后兼容。推荐根据实际情况使用 savemany 或 saveassociated。
保存相关模型的数据(hasone, hasmany, belongsto)
在与关联模型一起工作时,when working with associated models, 一定要意识到模型数据的保存总是由相应有 cakephp 模型来完成。如果保存一条新的 post 和它关联的 comment,就需要在保存操作的过程中同时使用 post 和 comment 模型。
如果系统中还不存在关联模型记录(例如,想要保存新的 user,同时保存相关的 profile 记录),需要先保存主模型或者父模型。
为了了解这是如何工作的,想像一下我们在处理保存新用 user 和相关 profile 的控制器中有一个动作。下面的示例动作假设已经为创建单个 user 和单个 profile,post 了足够的数据(使用 formhelper):
1 public function add() {
 2     if (!empty($this->request->data)) {
 3         // 我们能保存 user 数据:
 4         // 它放在 $this->request->data['user'] 中
 5 
 6         $user = $this->user->save($this->request->data);
 7 
 8         // 如果用户被保存,添加这条信息到数据并保存 profile。
 9 
10         if (!empty($user)) {
11             // 新创建的 user id 已经被赋值给 $this->user->id.
12             $this->request->data['profile']['user_id'] = $this->user->id;
13 
14             // 由于 user hasone profile,因此可以通过 user 模型访问 profile 模型:
15             $this->user->profile->save($this->request->data);
16         }
17     }
18 }
作为一条规则,当带有 hasone、hasmany、belongsto 关联时,全部与键有关。基本思路是从一个模型中获取键,并将其放入另一个模型的外键列中。有时需要涉及使用保存后的模型类的 $id 属性,但是其它情况下只涉及从 post 给控制器动作的表单的隐藏域(hidden input)中得到的 id。
作为上述基本方法的补充,cakephp 还提供了一个非常有用的方法 saveassociated(),它允许你用一个简短的方式校验和保存多个模型。另外,saveassociated() 还提供了事务支持以确保数据库中的数据的完整(例如,一个模型保存失败,另一个模型也就不保存了)。
注解
为使事务工作在 mysql 中正常工作,表必须使用 innodb 引擎。记住,myisam 表不支持事务。
来看看如何使用 saveassociated() 同时保存 company 和 account 模型吧。
首先,需要同时为 company 和 account 创建表单(假设 company hasmany account):
1 echo $this->form->create('company', array('action' => 'add'));
 2 echo $this->form->input('company.name', array('label' => 'company name'));
 3 echo $this->form->input('company.description');
 4 echo $this->form->input('company.location');
 5 
 6 echo $this->form->input('account.0.name', array('label' => 'account name'));
 7 echo $this->form->input('account.0.username');
 8 echo $this->form->input('account.0.email');
 9 
10 echo $this->form->end('add');
看看为 acount 模型命名表单列的方法。如果 company 是主模型,saveassociated() 期望相关模型(account)数据以指定的格式放进数组。并且拥有我们需要的 account.0.fieldname。
注解
上面的列命名对于 hasmany 关联是必须的。如果关联是 hasone,你就得为关联模型使用 modelname.fieldname 了。
现在,可以在 companiescontroller 中创建 add() 动作了:
1 public function add() {
2     if (!empty($this->request->data)) {
3         // 使用如下方式避免校验错误:
4         unset($this->company->account->validate['company_id']);
5         $this->company->saveassociated($this->request->data);
6     }
7 }
这就是全部的步骤了。现在 company 和 account 模型将同时被校验和保存。默认情况下,saveassociated 将检验传递过来的全部值,然后尝试执行每一个保存。
通过数据保存 hasmany
让我们来看看存在在 join 表里的两个模型的数据是如何保存的。就像 hasmany 贯穿 (连接模型) 一节展示的那样,join 表是用 hasmany 类型的关系关联到每个模型的。 我们的例子包括 cake 学校的负责人要求我们写一个程序允许它记录一个学生在某门课上出勤的天数和等级。下面是示例代码:
1 // controller/coursemembershipcontroller.php
 2 class coursemembershipscontroller extends appcontroller {
 3     public $uses = array('coursemembership');
 4 
 5     public function index() {
 6         $this->set('coursemembershipslist', $this->coursemembership->find('all'));
 7     }
 8 
 9     public function add() {
10         if ($this->request->is('post')) {
11             if ($this->coursemembership->saveassociated($this->request->data)) {
12                 $this->redirect(array('action' => 'index'));
13             }
14         }
15     }
16 }
17 
18 // view/coursememberships/add.ctp
19 
20 form->create('coursemembership'); ?>
21 form->input('student.first_name'); ?>
22 form->input('student.last_name'); ?>
23 form->input('course.name'); ?>
24 form->input('coursemembership.days_attended'); ?>
25 form->input('coursemembership.grade'); ?>
26     save
27 form->end(); ?>
提交的数据数组如下:
1 array
 2 (
 3     [student] => array
 4     (
 5         [first_name] => joe
 6         [last_name] => bloggs
 7     )
 8 
 9     [course] => array
10     (
11         [name] => cake
12     )
13 
14     [coursemembership] => array
15     (
16         [days_attended] => 5
17         [grade] => a
18     )
19 
20 )
cake 会很乐意使用一个带有这种数据结构的 saveassociated 调用就能同时保存很多,并将 student 和 course 的外键赋予 cousemembership. 如果我们运行 coursemembershipscontroller 上的 index 动作,从 find(‘all’) 中获取的数据结构如下:
1 array
 2 (
 3     [0] => array
 4     (
 5         [coursemembership] => array
 6         (
 7             [id] => 1
 8             [student_id] => 1
 9             [course_id] => 1
10             [days_attended] => 5
11             [grade] => a
12         )
13 
14         [student] => array
15         (
16             [id] => 1
17             [first_name] => joe
18             [last_name] => bloggs
19         )
20 
21         [course] => array
22         (
23             [id] => 1
24             [name] => cake
25         )
26     )
27 )
当然,还有很多带有连接模型的工作的方法。上面的版本假定你想要立刻保存每样东西。 还有这样的情况:你想独立地创建 student 和 course,稍后再指定两者与 coursemembership 的关联。 因此你可能有一个允许利用列表或id选择存在的学生和课程及两个 coursemembership 元列的表单,例如:
1 // view/coursememberships/add.ctp

3 form->create('coursemembership'); ?>
4 form->input('student.id', array('type' => 'text', 'label' => 'student id', 'default' => 1)); ?>
5 form->input('course.id', array('type' => 'text', 'label' => 'course id', 'default' => 1)); ?>
6 form->input('coursemembership.days_attended'); ?>
7 form->input('coursemembership.grade'); ?>
8     save
9 form->end(); ?>
所得到的 post 数据:
1 array
 2 (
 3     [student] => array
 4     (
 5         [id] => 1
 6     )
 7 
 8     [course] => array
 9     (
10         [id] => 1
11     )
12 
13     [coursemembership] => array
14     (
15         [days_attended] => 10
16         [grade] => 5
17     )
18 )
cake 利用 saveassociated 将 student id 和 course id 推入 coursemembership。
保存相关模型数据 (habtm)
通过 hasone、belongsto、hasmany 保存有关联的模型是非常简单的: 只需要将关联模型的 id 填入外键列。 填完之后,只要调用模型上的 save() 方法,一切就都被正确的串连起来了。 下面是准备传递给 tag 模型的 save() 方法的数据数组格式的示例:
1 array
 2 (
 3     [recipe] => array
 4         (
 5             [id] => 42
 6         )
 7     [tag] => array
 8         (
 9             [name] => italian
10         )
11 )
也可以在 saveall() 中使用这种格式保存多条记录和与它们有 habtm 关联的的模型,格式如下:
1 array
 2 (
 3     [0] => array
 4         (
 5             [recipe] => array
 6                 (
 7                     [id] => 42
 8                 )
 9             [tag] => array
10                 (
11                     [name] => italian
12                 )
13         )
14     [1] => array
15         (
16             [recipe] => array
17                 (
18                     [id] => 42
19                 )
20             [tag] => array
21                 (
22                     [name] => pasta
23                 )
24         )
25     [2] => array
26         (
27             [recipe] => array
28                 (
29                     [id] => 51
30                 )
31             [tag] => array
32                 (
33                     [name] => mexican
34                 )
35         )
36     [3] => array
37         (
38             [recipe] => array
39                 (
40                     [id] => 17
41                 )
42             [tag] => array
43                 (
44                     [name] => american (new)
45                 )
46         )
47 )
将上面的数组传递给 saveall() 将创建所包含的 tag ,每个都与它们各自的 recipe 关联。
作为示例,我们建立了创建新 tag 和运行期间生成与 recipe 关联的正确数据数组的表单。
这个简单的表单如下:(我们假定 $recipe_id 已经设置了):
1 form->create('tag'); ?>
2 form->input(
3         'recipe.id',
4         array('type' => 'hidden', 'value' => $recipe_id)
5     ); ?>
6 form->input('tag.name'); ?>
7 form->end('add tag'); ?>
在这个例子中,你能看到 recipe.id hidden 域,其值被设置为我们的 tag 想要连接的 recipe 的 id。
当在控制器中调用 save() 方法,它将自动将 habtm 数据保存到数据库:
1 public function add() {
2     // 保存关联
3     if ($this->tag->save($this->request->data)) {
4         // 保存成功后要做的事情
5     }
6 }
这段代码将创建一个新的 tag 并与 recipe 相关联,其 id 由 $this->request->data['recipe']['id'] 设置。
某些情况下,我们可能希望呈现的关联数据能够包含下拉 select 列表。数据可能使用 find('list') 从模型中取出并且赋给用模型名命名的视图变量。 同名的 input 将自动把数据放进 :
1 // 控制器中的代码:
2 $this->set('tags', $this->recipe->tag->find('list'));
1 // 视图中的代码:
2 $this->form->input('tags');
更可能的情形是一个 habtm 关系包含一个允许多选的 。例如,一个 recipe 可能被赋了多个 tag。在这种情况下,数据以相同的方式从模型中取出,但是表单 input 定义稍有不同。tag 的命名使用 modelname 约定:
1 // 控制器中的代码:
2 $this->set('tags', $this->recipe->tag->find('list'));
1 // 视图中的代码:
2 $this->form->input('tag');
使用上面这段代码,将建立可多选的下拉列表(select),允许多选自动被保存到已添加或已保存到数据库中的 recipe。
当 habtm 变得复杂时怎么办?
默认情况下,cake 在保存 habtm 关系时,会先删除连接表中的所有行。 例如,有一个拥有10个 children 关联的 club。带着2个 children 更新 club。club 将只有2个 children,而不是12个。
要注意,如果想要向带有 habtm 的连接表添加更多的列(建立时间或者元数据)是可能的,重要的是要明白你有一个简单的选项。
两个模型间的 hasandbelongstomany 关联实际上是同时拥有 hasmany 和 belongsto 关联的三个模型关系的简写。
考虑下面的例子:
child hasandbelongstomany club
另一个方法是添加一个 membership 模型:
child hasmany membership
membership belongsto child, club
club hasmany membership.
这两个例子几乎是相同的。它们在数据库中使用了命名相同的 amount 列,模型中的 amount 也是相同的。最重要的不同是 “join” 表命名不同,并且其行为更具可预知性。
小技巧
当连接表包含外键以外的扩展列时,通过将数组的 'unique' 设置为 “‘keepexisting’”,能够防止丢失扩展列的值。同样,可以认为设置 ‘unique’ => true,在保存操作过程中不会丢失扩展列的数据。参见 habtm association arrays。
不过,更多情况下,为连接表建立一个模型,并像上面的例子那样设置 hasmany、belongsto 关联,代替使用 habtm 关联,会更简单。
数据表
虽然 cakephp 可以有非数据库驱动的数据源,但多数时候,都是有数据库驱动的。 cakephp 被设计成可以与 mysql、mssql、oracle、postgresql 和其它数据库一起工作。 你可以创建你平时所用的数据库系统的表。在创建模型类时,模型将自动映射到已经建立的表上。表名被转换为复数小写,多个单词的表名的单词用下划线间隔。例如,名为 ingredient 的模型对应的表名为 ingredients。名为 eventregistration 的模型对应的表名为 event_registrations。cakephp 将检查表来决定每个列的数据类型,并使用这些信息自动化各种特性,比如视图中输出的表单域。列名被转换为小写并用下划线间隔。
使用 created 和 modified 列
通过在数据库表中定义 created 和 modified 列作为 datetime 列,cakephp 能够识别这些域并自动在其中填入记录在数据库中创建的时间和保存的时间(除非被保存的数据中已经包含了这些域的值)。
在记录最初添加时,created 和 modified 列将被设置为当前日期和时间。当已经存在的记录被保存时,modified 列将被更新至当前日期和时间。
如果在 model::save() 之前 $this->data 中包含了 updated、created、modified 数据(例如 model::read 或者 model::set),那么这些值将从 $this->data 中获取,并且不自动更新。 或者使用 unset($this->data['model']['modified']) 等方法。总是可以覆盖 model::save() 方法来做这件事:
1 class appmodel extends model {
 2 
 3     public function save($data = null, $validate = true, $fieldlist = array()) {
 4         // 在每个保存操作前清除 modified 域值:
 5         $this->set($data);
 6         if (isset($this->data[$this->alias]['modified'])) {
 7             unset($this->data[$this->alias]['modified']);
 8         }
 9         return parent::save($this->data, $validate, $fieldlist);
10     }
11 
12 }
http://www.bkjia.com/phpjc/477776.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/477776.htmltecharticle保存数据 cakephp 会为保存模型数据制作快照。准备保存的数据使用如下基本格式传递给模型的 save() 方法: 1 array 2 ( 3 [modelname] = array 4 ( 5...
其它类似信息

推荐信息