Skip to content

Commit

Permalink
Merge pull request #12 from hiqsol/master
Browse files Browse the repository at this point in the history
Still fixing running non batch queries from Collection
  • Loading branch information
hiqsol authored Nov 16, 2017
2 parents 028c161 + d5c996d commit c9b9571
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
32 changes: 30 additions & 2 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,41 @@ protected function updateInternal($attributes = null, $options = [])
return $result === false ? false : true;
}

/**
* Perform batch query.
* Attention: takes bulk data and returns bulk result.
* @param string $defaultScenario
* @param array $data bulk data
* @param array $options
* @return array bulk results
*/
public function batchQuery($defaultScenario, $data = [], array $options = [])
{
$options['batch'] = true;
$batch = isset($options['batch']) ? (bool)$options['batch'] : true;
$options['batch'] = $batch;

if (!$batch) {
$val = reset($data);
$key = key($data);
$data = $val;
}

return $this->query($defaultScenario, $data, $options);
$result = $this->query($defaultScenario, $data, $options);

if (!$batch) {
$result = [$key = $result];
}

return $result;
}

/**
* Perform query.
* @param string $defaultScenario
* @param array $data data
* @param array $options
* @return array result
*/
public function query($defaultScenario, $data = [], array $options = [])
{
$action = $this->getScenarioAction($defaultScenario);
Expand Down
17 changes: 6 additions & 11 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ public function insert($runValidation = true, $attributes = null, array $queryOp
return false;
}

$data = $this->collectData($attributes, $queryOptions);
$results = $this->first->query('create', $data, $queryOptions);
$data = $this->collectData($attributes);
$results = $this->first->batchQuery('create', $data, $queryOptions);
$pk = $this->first->primaryKey()[0];
foreach ($this->models as $key => $model) {
$values = &$data[$key];
Expand Down Expand Up @@ -379,8 +379,8 @@ public function update($runValidation = true, $attributes = null, array $queryOp
return false;
}

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

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

if (isset($queryOptions['batch']) && (bool)$queryOptions['batch'] === true) {
return $data;
}

return reset($data);
return $data;
}

/**
Expand Down

0 comments on commit c9b9571

Please sign in to comment.