简体   繁体   中英

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 updateAll() 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'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM