php - yii 模型-> 保存不适用于更新值

标签 php mysql yii

public function actionUpdateprofile(){
    $user = User::model()->findByPk($_POST['User']['user_id']);
    $profile = Profile::model()->findByPk($_POST['User']['user_id']);

    if(isset($_POST['User']) || isset($_POST['Profile'])){
        $user->attributes = $_POST['User'];
        $profile->attributes = $_POST['Profile'];
        $user->save();
        $profile->save();

    }
}

我编写此代码是为了更新配置文件和用户表中的值。但它不起作用。 如果我发送 $_POST['Profile']['email'] = 'abc@gmail.com'; 没有错误,但数据库仍然显示旧值。为什么?

我在那里做错了什么?

这是 $profile->attributes 的结果。电子邮件仍然具有旧值。

Array
(
    [user_id] => 35
    [lastname] => asd
    [firstname] => asrtyr
    [email] => xyz.gmail.com
    [phoneno] => 123456
    [prof_img] => 
    [phoneno2] => 0
    [agentKey] => 
    [fb_id] => 
)

最佳答案

我建议你像下面这样添加错误报告

if(!$user->save()){
    echo 'Error to save user model<br />';
    var_dump($user->getErrors());
}
if(!$profile->save()){
    echo 'Error to save profile model<br />';
    var_dump($profile->getErrors());
}

关于php - yii 模型-> 保存不适用于更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26612018/

相关文章:

php - 另一个小错误(可能很明显)

PHP 在 JSON 数组中传回带有特殊字符的对象

Python:登录无错误

php - MySQL FOUND_ROWS 的 Yii 框架分页问题

php - ajaxSubmitButton 在 Linux 服务器上不起作用

php - 使用 PHP Selenium Webdriver 单击下拉菜单中的选项?

PHP - 扩展类

php - 来自 Paypal 的 Laravel 5.1 csrftoken curl

mysql - 通过 SQLCMD 将 sql 文件从 MySQL 导入到 MS SQL 时语法不正确

php - Yii、yiic 和控制台