本文整理汇总了PHP中yii\db\ActiveQuery::indexBy方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveQuery::indexBy方法的具体用法?PHP ActiveQuery::indexBy怎么用?PHP ActiveQuery::indexBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveQuery
的用法示例。
在下文中一共展示了ActiveQuery::indexBy方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: indexBy
/**
* Converts the indexBy column name an anonymous function that writes rows to the
* result array indexed an attribute name that may be in dotted notation.
*
* @param callable|string $column name of the column by which the query results should be indexed
* @return $this
*/
public function indexBy($column)
{
if (!$this->asArray) {
return parent::indexBy($column);
}
/** @var DynamicActiveRecord $modelClass */
$modelClass = $this->modelClass;
$this->indexBy = function ($row) use($column, $modelClass) {
if (isset($row[$column])) {
return $row[$column];
}
$dynamicColumn = $modelClass::dynamicColumn();
if (!isset($row[$dynamicColumn])) {
throw new UnknownPropertyException("Dynamic column {$dynamicColumn} does not exist - wasn't set in select");
}
$dynamicAttributes = DynamicActiveRecord::dynColDecode($row[$dynamicColumn]);
$value = $this->getDotNotatedValue($dynamicAttributes, $column);
return $value;
};
return $this;
}