cakephp中save和saveAll函数有什么区别? [英] What is the difference between save and saveAll function in cakephp?

查看:836
本文介绍了cakephp中save和saveAll函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用任何一个例子请

解决方案

save 只需保存模型:

 数组

[ModelName] =& b(
[fieldname1] =>'value'
[fieldname2] =>'value'


/ pre>

假设上面的信息存储在一个名为$ 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 (if id 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 and Profile models at the same time. Otherwise, you would have to call save() for User first, obtain the id of the newly saved user and then save Profile with user_id set to the obtained id.

Examples taken straight from the book.

这篇关于cakephp中save和saveAll函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆