当前位置: 首页>>代码示例>>PHP>>正文


PHP ActiveQuery::each方法代码示例

本文整理汇总了PHP中yii\db\ActiveQuery::each方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveQuery::each方法的具体用法?PHP ActiveQuery::each怎么用?PHP ActiveQuery::each使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在yii\db\ActiveQuery的用法示例。


在下文中一共展示了ActiveQuery::each方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: optimizeAttribute

 /**
  * @param \yii\db\ActiveQuery $query
  * @param string $attribute
  * @param int $from
  * @param int $size
  * @param int $offset
  * @param int|null $freeFrom
  * @param int $freeSize
  * @param int|null $targetDepth
  * @param array $additional
  * @return int|null
  * @throws Exception
  * @throws \yii\db\Exception
  */
 protected function optimizeAttribute($query, $attribute, $from, $size, $offset = 0, $freeFrom = null, $freeSize = 0, $targetDepth = null, $additional = [])
 {
     $primaryKey = $this->getPrimaryKey();
     $result = null;
     $isForward = $attribute === $this->leftAttribute;
     // @todo: pgsql and mssql optimization
     if (in_array($this->owner->getDb()->driverName, ['mysql', 'mysqli'])) {
         // mysql optimization
         $tableName = $this->owner->tableName();
         $additionalString = null;
         $additionalParams = [];
         foreach ($additional as $name => $value) {
             $additionalString .= ", [[{$name}]] = ";
             if ($value instanceof Expression) {
                 $additionalString .= $value->expression;
                 foreach ($value->params as $n => $v) {
                     $additionalParams[$n] = $v;
                 }
             } else {
                 $paramName = ':nestedIntervals' . count($additionalParams);
                 $additionalString .= $paramName;
                 $additionalParams[$paramName] = $value;
             }
         }
         $command = $query->select([$primaryKey, $attribute, $this->depthAttribute])->orderBy([$attribute => $isForward ? SORT_ASC : SORT_DESC])->createCommand();
         $this->owner->getDb()->createCommand("\n                UPDATE\n                    {$tableName} u,\n                    (SELECT\n                        [[{$primaryKey}]],\n                        IF (@i := @i + 1, 0, 0)\n                        + IF ([[{$attribute}]] " . ($isForward ? '>' : '<') . " @freeFrom,\n                            IF (\n                                (@result := @i)\n                                + IF (@depth - :targetDepth > 0, @result := @result + @depth - :targetDepth, 0)\n                                + (@i := @i + :freeSize * 2)\n                                + (@freeFrom := NULL), 0, 0),\n                            0)\n                        + IF (@depth - [[{$this->depthAttribute}]] >= 0,\n                            IF (@i := @i + @depth - [[{$this->depthAttribute}]] + 1, 0, 0),\n                            0)\n                        + (:from " . ($isForward ? '+' : '-') . " (CAST(@i AS UNSIGNED INTEGER) + :offset) * :size)\n                        + IF ([[{$attribute}]] = @freeFrom,\n                            IF ((@result := @i) + (@i := @i + :freeSize * 2) + (@freeFrom := NULL), 0, 0),\n                            0)\n                        + IF (@depth := [[{$this->depthAttribute}]], 0, 0)\n                        as 'new'\n                    FROM\n                        (SELECT @i := 0, @depth := -1, @freeFrom := :freeFrom, @result := NULL) v,\n                        (" . $command->sql . ") t\n                    ) tmp\n                SET u.[[{$attribute}]]=tmp.[[new]] {$additionalString}\n                WHERE tmp.[[{$primaryKey}]]=u.[[{$primaryKey}]]")->bindValues($additionalParams)->bindValues($command->params)->bindValues([':from' => $from, ':size' => $size, ':offset' => $offset, ':freeFrom' => $freeFrom, ':freeSize' => $freeSize, ':targetDepth' => $targetDepth])->execute();
         if ($freeFrom !== null) {
             $result = $this->owner->getDb()->createCommand("SELECT IFNULL(@result, @i + 1 + IF (@depth - :targetDepth > 0, @depth - :targetDepth, 0))")->bindValue(':targetDepth', $targetDepth)->queryScalar();
             $result = $result === null ? null : (int) $result;
         }
         return $result;
     } else {
         // generic algorithm (very slow!)
         $query->select([$primaryKey, $attribute, $this->depthAttribute])->asArray()->orderBy([$attribute => $isForward ? SORT_ASC : SORT_DESC]);
         $prevDepth = -1;
         $i = 0;
         foreach ($query->each() as $data) {
             $i++;
             if ($freeFrom !== null && $freeFrom !== (int) $data[$attribute] && ($freeFrom > (int) $data[$attribute] xor $isForward)) {
                 $result = $i;
                 $depthDiff = $prevDepth - $targetDepth;
                 if ($depthDiff > 0) {
                     $result += $depthDiff;
                 }
                 $i += $freeSize * 2;
                 $freeFrom = null;
             }
             $depthDiff = $prevDepth - $data[$this->depthAttribute];
             if ($depthDiff >= 0) {
                 $i += $depthDiff + 1;
             }
             $this->owner->updateAll(array_merge($additional, [$attribute => $isForward ? $from + ($i + $offset) * $size : $from - ($i + $offset) * $size]), [$primaryKey => $data[$primaryKey]]);
             if ($freeFrom !== null && $freeFrom === (int) $data[$attribute]) {
                 $result = $i;
                 $i += $freeSize * 2;
                 $freeFrom = null;
             }
             $prevDepth = $data[$this->depthAttribute];
         }
         if ($freeFrom !== null) {
             $result = $i + 1;
             $depthDiff = $prevDepth - $targetDepth;
             if ($depthDiff > 0) {
                 $result += $depthDiff;
             }
         }
         return $result;
     }
 }
开发者ID:paulzi,项目名称:yii2-nested-intervals,代码行数:83,代码来源:NestedIntervalsBehavior.php


注:本文中的yii\db\ActiveQuery::each方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。