Php 为单个数据库行设置字段值
我正在尝试更新我的Php 为单个数据库行设置字段值,php,mysql,cakephp,model,Php,Mysql,Cakephp,Model,我正在尝试更新我的profiles表中的一行,以将用户配置文件图片重置为默认值user.png。我的控制器中有以下操作: public function deleteProfilePicture() { $this->layout = 'ajax'; // First find the profile ID from the user ID $profileId = $this->Profile->find('first', array(
profiles
表中的一行,以将用户配置文件图片重置为默认值user.png
。我的控制器中有以下操作:
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()
的返回值:
如果将debug设置为2,则可以查看正在执行的SQL,查看更新是否实际启动。试试看
$this->Profile->id=$profileId['Profile']['id'];
$this->Profile->set(数组)(
'picture'=>'user.png'
));
$this->Post->save();
试试这个
$this->Profile->read(null, $profileId['Profile']['id']);
$this->Profile->saveField('picture', 'user.png', false);
为什么要将验证设置为false?如果您忽略了它,您会得到错误吗?我看不出您的代码中有错误。尝试使用此命令查看正在执行的查询
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
$data['Profile']['id']=$profileId['Profile']['id'];
$data['Profile'['picture']='user.png';
$this->Profile->save($data);
如果发现问题,请根据结果对查询进行更改
或者试试这个
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
$data['Profile']['id']=$profileId['Profile']['id'];
$data['Profile'['picture']='user.png';
$this->Profile->save($data);
saveField()调用的返回值是多少?您可能有一个beforeSave()回调之前的模型或行为,该回调返回false,从而阻止了更新。请检查生成的sql并尝试直接在DB上执行它。。可能是出了什么问题。我已将
saveField()
的返回值编辑到原始问题中@请不要告诉我如何找到它正在执行的SQL?可能是题外话,但它应该在find()中读取'conditions'而不是'condition'。确保字段名大小写完全相同。清除模型缓存并进行检查。由于某种原因,正在执行的查询只更新modified
字段,它根本不涉及picture
,尽管我明确告诉它要更改它。