Skip to content

Commit

Permalink
Fixed bug introduced by #11
Browse files Browse the repository at this point in the history
This commit fixed default value of queryOptions[batch] from false to true
  • Loading branch information
SilverFire committed Nov 10, 2017
1 parent 5d5f608 commit 028c161
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class Collection extends Component
public $modelOptions = [];

/**
* Perform query options
* @var array
* @var array Options that will be passed to [[ActiveRecord::query()]] method as third argument.
* @see ActiveRecord::query()
*/
public $queryOptions = [];

Expand Down Expand Up @@ -112,6 +112,14 @@ class Collection extends Component
*/
public $dataCollector;


public function init()
{
if (!isset($this->queryOptions['batch'])) {
$this->queryOptions['batch'] = true;
}
}

/**
* Sets the model of the collection.
* @param ActiveRecord|array $model if the model is an instance of [[Model]] - sets it, otherwise - creates the model
Expand Down Expand Up @@ -326,7 +334,7 @@ public function save($runValidation = true, $attributes = null, $options = [])
}
}

public function insert($runValidation = true, $attributes = null, array $options = [])
public function insert($runValidation = true, $attributes = null, array $queryOptions = [])
{
if (!$attributes) {
$attributes = $this->attributes ?: $this->first->activeAttributes();
Expand All @@ -338,8 +346,8 @@ public function insert($runValidation = true, $attributes = null, array $options
return false;
}

$data = $this->collectData($attributes, $options);
$results = $this->first->query('create', $data, $options);
$data = $this->collectData($attributes, $queryOptions);
$results = $this->first->query('create', $data, $queryOptions);
$pk = $this->first->primaryKey()[0];
foreach ($this->models as $key => $model) {
$values = &$data[$key];
Expand All @@ -359,7 +367,7 @@ public function insert($runValidation = true, $attributes = null, array $options
return true;
}

public function update($runValidation = true, $attributes = null, array $options = [])
public function update($runValidation = true, $attributes = null, array $queryOptions = [])
{
if (!$attributes) {
$attributes = $this->attributes ?: $this->first->activeAttributes();
Expand All @@ -371,8 +379,8 @@ public function update($runValidation = true, $attributes = null, array $options
return false;
}

$data = $this->collectData($attributes, $options);
$results = $this->first->query('update', $data, $options);
$data = $this->collectData($attributes, $queryOptions);
$results = $this->first->query('update', $data, $queryOptions);

foreach ($this->models as $key => $model) {
$changedAttributes = [];
Expand Down Expand Up @@ -406,10 +414,10 @@ public function delete()
/**
* Collects data from the stored models.
* @param string|array $attributes list of attributes names
* @param array $options
* @param array $queryOptions options that are going to be
* @return array
*/
public function collectData($attributes = null, $options = [])
public function collectData($attributes = null, $queryOptions = [])
{
$data = [];
foreach ($this->models as $model) {
Expand All @@ -427,7 +435,11 @@ public function collectData($attributes = null, $options = [])
}
}

return $this->isBatch($options) ? $data : reset($data);
if (isset($queryOptions['batch']) && (bool)$queryOptions['batch'] === true) {
return $data;
}

return reset($data);
}

/**
Expand Down Expand Up @@ -594,17 +606,4 @@ 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 028c161

Please sign in to comment.