src/commands/TagController.php
public function actionCleanup()
    {
        $tagIds = Tag::find()->select(['id'])->column();

        $batch = TagRelation::find()->batch();
        $i = 0;
        $delete = 0;
        foreach ($batch as $rows) {
            foreach ($rows as $relation) {
                $i++;
                // check if tag id exists in table
                if (!in_array($relation->tag_id, $tagIds)) {
                    $relation->delete();
                    $delete++;
                    continue;
                }

                $prefixedTableName = '{{%'.$relation->table_name.'}}';
                $pk = $this->tableSchema($prefixedTableName)->primaryKey;
                $query = (new Query())
                    ->from($prefixedTableName)
                    ->where([current($pk) => $relation->pk_id]) // provide model mapping or read pk name from schema
                    ->exists();

                if (!$query) {
                    $relation->delete();
                    $delete++;
                }

                unset($relation, $prefixedTableName, $pk, $query);
            }
        }

        return $this->outputSuccess("{$i} items checked and {$delete} items deleted.");
    }
public function actionCleanup()
    {
        $tagIds = Tag::find()->select(['id'])->column();

        $batch = TagRelation::find()->batch();
        $i = 0;
        $delete = 0;
        foreach ($batch as $rows) {
            foreach ($rows as $relation) {
                $i++;
                // check if tag id exists in table
                if (!in_array($relation->tag_id, $tagIds)) {
                    $relation->delete();
                    $delete++;
                    continue;
                }

                $prefixedTableName = '{{%'.$relation->table_name.'}}';
                $pk = $this->tableSchema($prefixedTableName)->primaryKey;
                $query = (new Query())
                    ->from($prefixedTableName)
                    ->where([current($pk) => $relation->pk_id]) // provide model mapping or read pk name from schema
                    ->exists();

                if (!$query) {
                    $relation->delete();
                    $delete++;
                }

                unset($relation, $prefixedTableName, $pk, $query);
            }
        }

        return $this->outputSuccess("{$i} items checked and {$delete} items deleted.");
    }
public function actionCleanup ( ) { $ tagIds = Tag :: find ( ) -> select ( [ 'id' ] ) -> column ( ) ; $ batch = TagRelation :: find ( ) -> batch ( ) ; $ i = 0 ; $ delete = 0 ; foreach ( $ batch as $ rows ) { foreach ( $ rows as $ relation ) { $ i ++ ; // check if tag id exists in table if ( ! in_array ( $ relation -> tag_id , $ tagIds ) ) { $ relation -> delete ( ) ; $ delete ++ ; continue ; } $ prefixedTableName = '{{%' . $ relation -> table_name . '}}' ; $ pk = $ this -> tableSchema ( $ prefixedTableName ) -> primaryKey ; $ query = ( new Query ( ) ) -> from ( $ prefixedTableName ) -> where ( [ current ( $ pk ) => $ relation -> pk_id ] ) // provide model mapping or read pk name from schema -> exists ( ) ; if ( ! $ query ) { $ relation -> delete ( ) ; $ delete ++ ; } unset ( $ relation , $ prefixedTableName , $ pk , $ query ) ; } } return $ this -> outputSuccess ( "{$i} items checked and {$delete} items deleted." ) ; }
Handle not existing relations, tags which does not exists or relation entries which does not exists in the table.

@return integer
Handle not existing relations tags which does not exists or relation entries which does not exists in the table .