Commit 765d47d0 by Paul Klimov

Sphinx Active Record updated to be compatible with ActiveDataProvider.

parent d1c87c7e
...@@ -253,11 +253,16 @@ class ActiveRecord extends Model ...@@ -253,11 +253,16 @@ class ActiveRecord extends Model
/** /**
* Returns the primary key name for this AR class. * Returns the primary key name for this AR class.
* @return string the primary key of the associated Sphinx index. * The default implementation will return the primary key as declared
* in the Sphinx index, which is associated with this AR class.
*
* Note that an array should be returned even for a table with single primary key.
*
* @return string[] the primary keys of the associated Sphinx index.
*/ */
public static function primaryKey() public static function primaryKey()
{ {
return static::getIndexSchema()->primaryKey; return [static::getIndexSchema()->primaryKey];
} }
/** /**
...@@ -861,8 +866,9 @@ class ActiveRecord extends Model ...@@ -861,8 +866,9 @@ class ActiveRecord extends Model
} }
$values = $this->getDirtyAttributes($attributes); $values = $this->getDirtyAttributes($attributes);
if (empty($values)) { if (empty($values)) {
$key = $this->primaryKey(); foreach ($this->primaryKey() as $key) {
$values[$key] = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null; $values[$key] = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null;
}
} }
$db = static::getDb(); $db = static::getDb();
$command = $db->createCommand()->insert($this->indexName(), $values); $command = $db->createCommand()->insert($this->indexName(), $values);
...@@ -1231,12 +1237,15 @@ class ActiveRecord extends Model ...@@ -1231,12 +1237,15 @@ class ActiveRecord extends Model
*/ */
public function getPrimaryKey($asArray = false) public function getPrimaryKey($asArray = false)
{ {
$key = $this->primaryKey(); $keys = $this->primaryKey();
$value = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null; if (count($keys) === 1 && !$asArray) {
if ($asArray) { return isset($this->_attributes[$keys[0]]) ? $this->_attributes[$keys[0]] : null;
return [$key => $value];
} else { } else {
return $value; $values = [];
foreach ($keys as $name) {
$values[$name] = isset($this->_attributes[$name]) ? $this->_attributes[$name] : null;
}
return $values;
} }
} }
...@@ -1254,12 +1263,15 @@ class ActiveRecord extends Model ...@@ -1254,12 +1263,15 @@ class ActiveRecord extends Model
*/ */
public function getOldPrimaryKey($asArray = false) public function getOldPrimaryKey($asArray = false)
{ {
$key = $this->primaryKey(); $keys = $this->primaryKey();
$value = isset($this->_oldAttributes[$key]) ? $this->_oldAttributes[$key] : null; if (count($keys) === 1 && !$asArray) {
if ($asArray) { return isset($this->_oldAttributes[$keys[0]]) ? $this->_oldAttributes[$keys[0]] : null;
return [$key => $value];
} else { } else {
return $value; $values = [];
foreach ($keys as $name) {
$values[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
}
return $values;
} }
} }
......
...@@ -493,7 +493,7 @@ class QueryBuilder extends Object ...@@ -493,7 +493,7 @@ class QueryBuilder extends Object
if (is_object($direction)) { if (is_object($direction)) {
$orders[] = (string)$direction; $orders[] = (string)$direction;
} else { } else {
$orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : ''); $orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : 'ASC');
} }
} }
......
<?php
namespace yiiunit\extensions\sphinx;
use yii\data\ActiveDataProvider;
use yiiunit\data\sphinx\ar\ActiveRecord;
use yiiunit\data\sphinx\ar\ArticleIndex;
/**
* @group sphinx
*/
class ActiveDataProviderTest extends SphinxTestCase
{
protected function setUp()
{
parent::setUp();
ActiveRecord::$db = $this->getConnection();
}
// Tests :
public function testActiveQuery()
{
$provider = new ActiveDataProvider([
'query' => ArticleIndex::find()->orderBy('id ASC'),
]);
$models = $provider->getModels();
$this->assertEquals(2, count($models));
$this->assertTrue($models[0] instanceof ArticleIndex);
$this->assertTrue($models[1] instanceof ArticleIndex);
$this->assertEquals([1, 2], $provider->getKeys());
$provider = new ActiveDataProvider([
'query' => ArticleIndex::find(),
'pagination' => [
'pageSize' => 1,
]
]);
$models = $provider->getModels();
$this->assertEquals(1, count($models));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment