cakephp中save和saveAll函数有什么区别? [英] What is the difference between save and saveAll function in cakephp?
问题描述
可以使用任何一个例子请
save
只需保存模型:
数组
/ pre>
(
[ModelName] =& b(
[fieldname1] =>'value'
[fieldname2] =>'value'
)
)
假设上面的信息存储在一个名为$ data的数组中,可以调用
$ this-> ModelName-> save($ data);
,以便在模型表中插入记录(如果
id
字段未指定)或更新模型表的记录(如果指定
id
字段)。
saveAll
用于:
保存模型的多条记录
数组
(
[Article] => Array
[0] => Array
(
[title] => title 1
)
[1] =& title] => title 2
)
)
)
因此,您可以同时保存多个模型,而不是循环并使用
save()
。
保存模型的相关记录
数组
] => Array
(
[username] => billy
)
[Profile] =& >男性
[occupation] =>程序员
)
)
这将保存
用户
和配置文件
模型。否则,您必须先为User
调用save()
,获取id
,然后保存配置文件
,并将user_id
设置为获取的id
直接从图书。
can any one give example please
解决方案
save
is used to simply save a model:Array ( [ModelName] => Array ( [fieldname1] => 'value' [fieldname2] => 'value' ) )
Assuming the above information was stored in an array called $data, one would call
$this->ModelName->save($data);
in order to INSERT a record into the model's table (if
id
field is not specified) or UPDATE a record of the model's table (ifid
field is specified).
saveAll
is used to:Save multiple records of a model
Array ( [Article] => Array ( [0] => Array ( [title] => title 1 ) [1] => Array ( [title] => title 2 ) ) )
So, you may save many models at the same time instead of looping and using
save()
each time.Save related records of a model
Array ( [User] => Array ( [username] => billy ) [Profile] => Array ( [sex] => Male [occupation] => Programmer ) )
This would save both
User
andProfile
models at the same time. Otherwise, you would have to callsave()
forUser
first, obtain theid
of the newly saved user and then saveProfile
withuser_id
set to the obtainedid
.Examples taken straight from the book.
这篇关于cakephp中save和saveAll函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!