yii2 AR 更新无效,求解 [ 新手入门 ]
yii 用AR插入,删除都可以,就是更新没有效果
public function updateToken()
{
$result = $this->find()
->where(['token' => $this->lastToken])
->one();
print_r($result);
$result->mobile = 'cccccc';
echo "<br><br><br>";
print_r($result);
$result->save();
}
app\appClient\models\token Object ( [lastToken] => [did] => [Ver] => [_attributes:yii\db\BaseActiveRecord:private] => Array ( [user_id] => [mobile] => aaabbbbba [token] => d9d59a7ea590dc66cb930bbbe1759031 [password] => [lat] => [lng] => [time] => 0 [login] => 0 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [user_id] => [mobile] => aaabbbbba [token] => d9d59a7ea590dc66cb930bbbe1759031 [password] => [lat] => [lng] => [time] => 0 [login] => 0 ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) )
app\appClient\models\token Object ( [lastToken] => [did] => [Ver] => [_attributes:yii\db\BaseActiveRecord:private] => Array ( [user_id] => [mobile] => cccccc [token] => d9d59a7ea590dc66cb930bbbe1759031 [password] => [lat] => [lng] => [time] => 0 [login] => 0 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [user_id] => [mobile] => aaabbbbba [token] => d9d59a7ea590dc66cb930bbbe1759031 [password] => [lat] => [lng] => [time] => 0 [login] => 0 ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) )
这是2次打印的输出,也就是找到数据了,但是更新后没有效果, 把update 改成 delete 确实能删除
共 2 条回复
-
帮你把代码修改如下
public function updateToken() { $result = $this->findOne(['token' => $this->lastToken]); print_r($result); $result->mobile = 'cccccc'; echo "<br><br><br>"; print_r($result); $result->save(); }
find()查出来的是对象数组,也就是说类似如下结构
[ [...], [...], ]
虽然你使用了one(),但是你的$result对应的仍是find(),但是find()是如下方式构建的
/** * @inheritdoc * @return ActiveQuery the newly created [[ActiveQuery]] instance. */ public static function find() { return Yii::createObject(ActiveQuery::className(), [get_called_class()]); }
它返回的是一个ActiveQuery对象,而findOne()是如下构建的
/** * @inheritdoc * @return static|null ActiveRecord instance matching the condition, or `null` if nothing matches. */ public static function findOne($condition) { return static::findByCondition($condition)->one(); }
它返回的是一个ActiveRecord对象,只有ActiveRecord对象才能进行save()
hyunsu
注册时间:2016-01-15
最后登录:2016-01-15
在线时长:0小时9分
最后登录:2016-01-15
在线时长:0小时9分
- 粉丝0
- 金钱10
- 威望0
- 积分10