hyunsu 2016-01-15 10:06:11 4391次浏览 2条回复 0 0 0

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 确实能删除

  • 回复于 2016-01-15 13:39 举报

    this->find有你这样写的

  • 回复于 2016-01-19 08:15 举报

    帮你把代码修改如下

    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()

您需要登录后才可以回复。登录 | 立即注册