protected function executeScript($db, $type, $columnName = null)
{
if ($this->primaryModel !== null) {
// lazy loading
if ($this->via instanceof self) {
// via junction table
$viaModels = $this->via->findJunctionRows([$this->primaryModel]);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/* @var $viaQuery ActiveQuery */
list($viaName, $viaQuery) = $this->via;
if ($viaQuery->multiple) {
$viaModels = $viaQuery->all();
$this->primaryModel->populateRelation($viaName, $viaModels);
} else {
$model = $viaQuery->one();
$this->primaryModel->populateRelation($viaName, $model);
$viaModels = $model === null ? [] : [$model];
}
$this->filterByModels($viaModels);
} else {
$this->filterByModels([$this->primaryModel]);
}
}
if (!empty($this->orderBy)) {
throw new NotSupportedException('orderBy is currently not supported by redis ActiveRecord.');
}
/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
if ($db === null) {
$db = $modelClass::getDb();
}
// find by primary key if possible. This is much faster than scanning all records
if (is_array($this->where) && (!isset($this->where[0]) && $modelClass::isPrimaryKey(array_keys($this->where)) || isset($this->where[0]) && $this->where[0] === 'in' && $modelClass::isPrimaryKey($this->where[1]))) {
return $this->findByPk($db, $type, $columnName);
}
$method = 'build' . $type;
$script = $db->getLuaScriptBuilder()->{$method}($this, $columnName);
return $db->executeCommand('EVAL', [$script, 0]);
}