php - 设置单个数据库行的字段值

标签 php mysql cakephp model

我正在尝试更新 profiles 表中的一行,以将用户个人资料图片重置为默认值 user.png。我的 Controller 中有以下操作:

public function deleteProfilePicture() {
    $this->layout = 'ajax';

    // First find the profile ID from the user ID
    $profileId = $this->Profile->find('first', array(
        'condition' => array('User.id' => $this->Auth->user('id')),
        'fields' => array('Profile.id'),
        'recursive' => -1
    ));

    $this->Profile->id = $profileId['Profile']['id'];
    $this->Profile->saveField('picture', 'user.png', false);
}

但是,当我请求 URL (/profile/deleteProfilePicture) 时,我没有收到任何错误,但数据库行未更新。我已通过使用 debug($profileId) 确保使用当前的配置文件 ID。

这里可能出了什么问题?

编辑:saveField()的返回值:

array(
    'Profile' => array(
        'id' => '36',
        'modified' => '2013-04-05 14:16:57'
    )
)

最佳答案

尝试

$this->个人资料->id = $profileId['个人资料']['id']; $this->个人资料->设置(数组( '图片' => '用户.png' )); $this->Post->save();

关于php - 设置单个数据库行的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15833588/

相关文章:

php - 查询输出php代码

php - 我是否总是必须在 ->first() 之前调用 ->count() 来防止 "No query results for model [Foo]"?

css - Cakephp 水平复选框

cakephp - CakePHP 表单上多个复选框 (HABTM) 的验证问题

mysql - CakePHP 显示作者不在作者表中的所有书籍

php - MVC 博客 - 如何包含 header\footer\other.php 文件

php - 在 Zend 2 应用程序中通过邮件报告错误

mysql - 从数据库中选择 2 个表并在表中获取合并结果

sql - 如何有效地选择表中新添加的行?

mysql - 如何在POST body的基础上创建动态插入语句