开发者

CakePHP savefield() with out knowing the record's id

How can I update a single field without knowing the record's ID? I have the user's username and I don't want to waste resources by doing a query just to get the id.

Note: I know I can achieve this with updateAll() but that seams like over kill, but is there a better way?

Can I some how limit up开发者_开发问答dateAll() to only search for one record and then finish, because I don't want it to continue to search when there is only one possible match.


updateAll() is the way to go in this case.

All the other save methods require the ID to be known.

You could also use the query() method : $this->Model->query($sql)

However, remember that values are not automatically escaped in this method.


If you're using Cake (or pretty much any other framework), you're sacrificing some efficiency with unnecessary queries. However, you can still run custom queries. For example, if you know the username and are trying to update the first_name field, you can do the following:

$this->User->query("UPDATE users SET first_name = '$new_firstname' WHERE username = '$username'");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜