yii 2.0 ActiveRecord 更新不起作用
yii 2.0 ActiveRecord update doesn't work
我是 Yii 2 的初学者。我正在尝试使用 activerecord 进行简单的用户更新,但是 update() 方法没有做任何事情。 update() return 返回 "true",总是 "success",但数据库记录没有改变。
控制器:
use app\models\Users;
.....
$userId = 1;
foreach ($array as $value) {
$user = Users::findOne($userId);
//$user->ramount = ($user->ramount + $value->ramunt);
$user->ramount = 22;
if ($user->update() !== false) {
echo "update successful";
} else {
echo "update failed";
}
//$user = Users::findOne($userId);
//$user->updateCounters(['ramount' => 22]);
}
我简化了更新,因为没有用。 updateCounters() 方法非常有效,但我不想使用它。
型号
namespace app\models;
use yii\base\Model;
class Users extends \yii\db\ActiveRecord
{
public $id;
public $email;
public $username;
public $ramount;
public function attributeLabels() {
return [
'id' => 'User ID',
'username' => 'Your name',
'email' => 'Your email address',
'ramount' => 'Resources',
];
}
public static function tableName() {
return 'users';
}
public function rules() {
return [
[['username'], 'string', 'max'=>60],
[['email'], 'string', 'max'=>120],
[['ramount'],'number','max'=>999999],
];
}
}
apache 日志中没有错误,yii 运行时日志中没有错误。感谢所有提示
您已在模型中定义class
class Users extends \yii\db\ActiveRecord
{
public $id;
public $email;
public $username;
public $ramount;
....
}
删除这些变量,因为它们会覆盖 ActiveRecord 所处理的属性。
Yii Guide 说:
Note: The Active Record attributes are named after the associated
table columns in a case-sensitive manner. Yii automatically defines an
attribute in Active Record for every column of the associated table.
You should NOT redeclare any of the attributes.
排队搜索记录并设置新值:
$userId = 1;
$user = Users::findOne($userId);
$user->ramount = 22;
尝试使用save()
方法:
if ($user->save()) {
echo "update successful";
} else {
echo "update failed";
}
PD: return 保存方法的值是布尔值(true 或 false)。
我是 Yii 2 的初学者。我正在尝试使用 activerecord 进行简单的用户更新,但是 update() 方法没有做任何事情。 update() return 返回 "true",总是 "success",但数据库记录没有改变。
控制器:
use app\models\Users;
.....
$userId = 1;
foreach ($array as $value) {
$user = Users::findOne($userId);
//$user->ramount = ($user->ramount + $value->ramunt);
$user->ramount = 22;
if ($user->update() !== false) {
echo "update successful";
} else {
echo "update failed";
}
//$user = Users::findOne($userId);
//$user->updateCounters(['ramount' => 22]);
}
我简化了更新,因为没有用。 updateCounters() 方法非常有效,但我不想使用它。
型号
namespace app\models;
use yii\base\Model;
class Users extends \yii\db\ActiveRecord
{
public $id;
public $email;
public $username;
public $ramount;
public function attributeLabels() {
return [
'id' => 'User ID',
'username' => 'Your name',
'email' => 'Your email address',
'ramount' => 'Resources',
];
}
public static function tableName() {
return 'users';
}
public function rules() {
return [
[['username'], 'string', 'max'=>60],
[['email'], 'string', 'max'=>120],
[['ramount'],'number','max'=>999999],
];
}
}
apache 日志中没有错误,yii 运行时日志中没有错误。感谢所有提示
您已在模型中定义class
class Users extends \yii\db\ActiveRecord
{
public $id;
public $email;
public $username;
public $ramount;
....
}
删除这些变量,因为它们会覆盖 ActiveRecord 所处理的属性。
Yii Guide 说:
Note: The Active Record attributes are named after the associated table columns in a case-sensitive manner. Yii automatically defines an attribute in Active Record for every column of the associated table. You should NOT redeclare any of the attributes.
排队搜索记录并设置新值:
$userId = 1;
$user = Users::findOne($userId);
$user->ramount = 22;
尝试使用save()
方法:
if ($user->save()) {
echo "update successful";
} else {
echo "update failed";
}
PD: return 保存方法的值是布尔值(true 或 false)。