Skip to content

Commit

Permalink
Merge pull request #110 from kyle-mccarthy/master
Browse files Browse the repository at this point in the history
added accessor for default primary key
  • Loading branch information
beowulfenator authored Oct 18, 2016
2 parents 336aeb5 + 7c274c8 commit b297f57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;
use yii\db\ActiveQueryInterface;

/**
* ActiveDataProvider is an enhanced version of [[\yii\data\ActiveDataProvider]] specific to the ElasticSearch.
Expand Down Expand Up @@ -116,4 +117,44 @@ protected function prepareTotalCount()
$results = $this->getQueryResults();
return (int)$results['hits']['total'];
}

/**
* @inheritdoc
*/
protected function prepareKeys($models)
{
$keys = [];
if ($this->key !== null) {
foreach ($models as $model) {
if (is_string($this->key)) {
$keys[] = $model[$this->key];
} else {
$keys[] = call_user_func($this->key, $model);
}
}

return $keys;
} elseif ($this->query instanceof ActiveQueryInterface) {
/* @var $class \yii\db\ActiveRecord */
$class = $this->query->modelClass;
$pks = $class::primaryKey();
if (count($pks) === 1) {
foreach ($models as $model) {
$keys[] = $model->primaryKey;
}
} else {
foreach ($models as $model) {
$kk = [];
foreach ($pks as $pk) {
$kk[$pk] = $model[$pk];
}
$keys[] = $kk;
}
}

return $keys;
} else {
return array_keys($models);
}
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Yii Framework 2 elasticsearch extension Change Log
- Enh: Bulk API implemented and used in AR (tibee, beowulfenator)
- Enh #82: Support HTTPS protocol (dor-denis, beowulfenator)
- Enh #43: Elasticsearch log target (trntv, beowulfenator)
- Bug: Added accessor method for the default elasticsearch primary key (kyle-mccarthy)


2.0.4 March 17, 2016
Expand Down

0 comments on commit b297f57

Please sign in to comment.