Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling deleteAll() after modifying a PrimaryKey definition cannot delete old data #219

Open
haohetao opened this issue Jan 8, 2021 · 2 comments
Labels
help wanted status:to be verified Needs to be reproduced and validated.

Comments

@haohetao
Copy link

haohetao commented Jan 8, 2021

What steps will reproduce the problem?

add Data
change primaryKey
deleteAll()

What's expected?

Expected the find()->all() is empty
but,calling find()->all() the data is still

What do you get instead?

Refactoring deleteAll()
\yii\redis\LuaScriptBuilder

    /**
     * @param string $modelClass className
     * @param mixed $condition
     * @return int
     */
    public function delete($modelClass, $condition)
    {
        $columns = [];
        if ($condition !== null) {
            $condition = $this->buildCondition($condition, $columns);
        } else {
            $condition = 'true';
        }
        
        $key = $this->quoteValue($modelClass::keyPrefix());
        $recordKey = $this->quoteValue($modelClass::keyPrefix() . ':a:');
        return <<<EOF
local allpks=redis.call('LRANGE',$key,0,-1)
local count=0
for k,pk in ipairs(allpks) do
    if $condition then
        redis.call('LREM', $key, 0, pk)
        count=count+redis.call('DEL', $recordKey .. pk)
    end
end
return count
EOF;
    }

\yii\redis\ActiveRecord

    /**
     * Deletes rows in the table using the provided conditions.
     * WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
     *
     * For example, to delete all customers whose status is 3:
     *
     * ~~~
     * Customer::deleteAll(['status' => 3]);
     * ~~~
     *
     * @param array $condition the conditions that will be put in the WHERE part of the DELETE SQL.
     * Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
     * @return int the number of rows deleted
     */
    public static function deleteAll($condition = null)
    {
        $db = static::getDb();
        $script = $db->getLuaScriptBuilder()->delete(static::class, $condition);
        return $db->executeCommand('EVAL', [$script, 0]);
    }

Additional info

Q A
Yii vesion 2.0.40
PHP version 8.0.1
Operating system docker
@bizley bizley added help wanted status:to be verified Needs to be reproduced and validated. labels Jan 8, 2021
@samdark
Copy link
Member

samdark commented Jan 11, 2021

@haohetao do you have time for a pull request with some tests (and possibly a fix)?

@haohetao
Copy link
Author

@samdark
我现在比较忙,需要等一或二个月

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted status:to be verified Needs to be reproduced and validated.
Projects
None yet
Development

No branches or pull requests

3 participants