Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Author: Carsten Brandt (mail@cebe.cc)
Example #1
0
 /**
  * @param Model|ActiveRecordInterface $model
  * @return array list of variation models in format: behaviorName => Model[]
  */
 protected function findVariationModelBatches($model)
 {
     $variationModels = [];
     foreach ($model->getBehaviors() as $name => $behavior) {
         if (empty($this->variationNames) && $behavior instanceof VariationBehavior || in_array($name, $this->variationNames)) {
             $variationModels[$name] = $behavior->getVariationModels();
         }
     }
     return $variationModels;
 }
Example #2
0
 /**
  * @param Model|ActiveRecordInterface $model
  * @return array list of variation models in format: behaviorName => Model[]
  */
 private function findRoleModels($model)
 {
     $roleModels = [];
     foreach ($model->getBehaviors() as $name => $behavior) {
         if (empty($this->roleNames) && $behavior instanceof \yii2tech\ar\role\RoleBehavior || in_array($name, $this->roleNames)) {
             $roleModels[$name] = $behavior->getRoleRelationModel();
         }
     }
     return $roleModels;
 }
Example #3
0
 /**
  * @param \yii\db\ActiveRecordInterface|\yii2tech\ar\position\PositionBehavior $model
  * @param $position
  * @throws BadRequestHttpException
  */
 protected function positionModel($model, $position)
 {
     switch (strtolower($position)) {
         case 'up':
         case 'prev':
             $model->movePrev();
             break;
         case 'down':
         case 'next':
             $model->moveNext();
             break;
         case 'top':
         case 'first':
             $model->moveFirst();
             break;
         case 'bottom':
         case 'last':
             $model->moveLast();
             break;
         default:
             if (is_numeric($position)) {
                 $model->moveToPosition($position);
             } else {
                 throw new BadRequestHttpException(Yii::t('yii', '{attribute} is invalid.', ['attribute' => $this->positionParam]));
             }
     }
 }
Example #4
0
 /**
  * Ensure what model has scenario before action execution
  */
 protected function ensureScenario()
 {
     if ($this->scenario === false) {
         return;
     }
     if ($this->multiple) {
         foreach ($this->model as $model) {
             $model->setScenario($this->scenario);
         }
         return;
     }
     $this->model->setScenario($this->scenario);
 }
Example #5
0
 /**
  * @param array $link
  * @param ActiveRecordInterface $foreignModel
  * @param ActiveRecordInterface $primaryModel
  * @throws InvalidCallException
  */
 private function bindModels($link, $foreignModel, $primaryModel)
 {
     foreach ($link as $fk => $pk) {
         $value = $primaryModel->{$pk};
         if ($value === null) {
             throw new InvalidCallException('Unable to link models: the primary key of ' . get_class($primaryModel) . ' is null.');
         }
         if (is_array($foreignModel->{$fk})) {
             // relation via array valued attribute
             $foreignModel->{$fk} = array_merge($foreignModel->{$fk}, [$value]);
         } else {
             $foreignModel->{$fk} = $value;
         }
     }
     $foreignModel->save(false);
 }
 /**
  * Find max index stored in database.
  *
  * If no index found, startIndex - 1 will be returned.
  *
  * @param  ActiveRecordInterface $model
  * @param  string                $attribute
  * @param  string                $commonPart
  * @return integer
  */
 protected function findMaxIndex(ActiveRecordInterface $model, $attribute, $commonPart)
 {
     // Find all possible max values.
     $db = $model::getDb();
     $indexExpression = 'SUBSTRING(' . $db->quoteColumnName($attribute) . ', :commonPartOffset)';
     $query = $model->find()->select(['_index' => $indexExpression])->andWhere($this->getExcludeByPkCondition($model))->andWhere(['like', $attribute, $commonPart])->andHaving(['not in', '_index', [0]])->orderBy(['CAST(' . $db->quoteColumnName('_index') . ' AS UNSIGNED)' => SORT_DESC])->addParams(['commonPartOffset' => mb_strlen($commonPart) + 1])->asArray();
     $this->addFilterToQuery($query);
     foreach ($query->each() as $row) {
         $index = $row['_index'];
         if (!preg_match('/^\\d+$/', $index)) {
             continue;
         }
         return $index;
     }
     return $this->startIndex - 1;
 }
Example #7
0
 /**
  * Delete slave record
  *
  * @param ActiveRecordInterface $slave
  */
 protected function deleteSlave(ActiveRecordInterface $slave)
 {
     if ($slave->delete() == false) {
         if (is_callable($this->errorDeleteCallback)) {
             call_user_func($this->errorDeleteCallback, $slave);
         }
     }
 }
Example #8
0
 /**
  * @param array $link
  * @param ActiveRecordInterface $foreignModel
  * @param ActiveRecordInterface $primaryModel
  * @throws InvalidCallException
  */
 private function bindModels($link, $foreignModel, $primaryModel)
 {
     foreach ($link as $fk => $pk) {
         $value = $primaryModel->{$pk};
         if ($value === null) {
             throw new InvalidCallException('Unable to link models: the primary key of ' . get_class($primaryModel) . ' is null.');
         }
         $foreignModel->{$fk} = $value;
     }
     $foreignModel->save(false);
 }
Example #9
0
 /**
  * @param ActiveRecordInterface $record
  */
 protected function deleteOrFail(ActiveRecordInterface $record)
 {
     if (!$record->delete()) {
         throw new InvalidValueException('No records were deleted from database.');
     }
 }