Skip to content

Commit

Permalink
Added attribute queryOptions and sent it to like query options. Add…
Browse files Browse the repository at this point in the history
…ed Collection::isBatch() method in order to determine whether is neede to do batch query.
  • Loading branch information
tafid committed Nov 6, 2017
1 parent a35aa74 commit 24cdbd6
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class Collection extends Component
*/
public $modelOptions = [];

/**
* Perform query options
* @var array
*/
public $queryOptions = [];

/**
* @var ActiveRecord the first model of the set. Fills automatically by [[set()]]
* @see set()
Expand Down Expand Up @@ -311,6 +317,7 @@ public function save($runValidation = true, $attributes = null, $options = [])
if ($this->isEmpty()) {
throw new InvalidCallException('Collection is empty, nothing to save');
}
$options = array_merge($options, $this->queryOptions);

if ($this->first->getIsNewRecord()) {
return $this->insert($runValidation, $attributes, $options);
Expand All @@ -332,7 +339,7 @@ public function insert($runValidation = true, $attributes = null, array $options
}

$data = $this->collectData($attributes);
$results = $this->first->batchQuery('create', $data, $options);
$results = $this->isBatch($options) ? $this->first->batchQuery('create', $data, $options) : $this->first->query('create', reset($data), $options);
$pk = $this->first->primaryKey()[0];
foreach ($this->models as $key => $model) {
$values = &$data[$key];
Expand Down Expand Up @@ -365,7 +372,7 @@ public function update($runValidation = true, $attributes = null, array $options
}

$data = $this->collectData($attributes);
$results = $this->first->batchQuery('update', $data, $options);
$results = $this->isBatch($options) ? $this->first->batchQuery('update', $data, $options) : $this->first->query('update', reset($data), $options);

foreach ($this->models as $key => $model) {
$changedAttributes = [];
Expand Down Expand Up @@ -586,4 +593,17 @@ public function isEmpty()
{
return empty($this->models);
}

/**
* @param array $options
* @return bool
*/
public function isBatch($options = [])
{
if (isset($options['batch'])) {
return (bool)$options['batch'];
}

return true;
}
}

0 comments on commit 24cdbd6

Please sign in to comment.