Browse Source

Sphinx Active Record updated to be compatible with ActiveDataProvider.

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
765d47d00d
  1. 40
      extensions/sphinx/ActiveRecord.php
  2. 2
      extensions/sphinx/QueryBuilder.php
  3. 42
      tests/unit/extensions/sphinx/ActiveDataProviderTest.php

40
extensions/sphinx/ActiveRecord.php

@ -253,11 +253,16 @@ class ActiveRecord extends Model
/**
* 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()
{
return static::getIndexSchema()->primaryKey;
return [static::getIndexSchema()->primaryKey];
}
/**
@ -861,8 +866,9 @@ class ActiveRecord extends Model
}
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
$key = $this->primaryKey();
$values[$key] = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null;
foreach ($this->primaryKey() as $key) {
$values[$key] = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null;
}
}
$db = static::getDb();
$command = $db->createCommand()->insert($this->indexName(), $values);
@ -1231,12 +1237,15 @@ class ActiveRecord extends Model
*/
public function getPrimaryKey($asArray = false)
{
$key = $this->primaryKey();
$value = isset($this->_attributes[$key]) ? $this->_attributes[$key] : null;
if ($asArray) {
return [$key => $value];
$keys = $this->primaryKey();
if (count($keys) === 1 && !$asArray) {
return isset($this->_attributes[$keys[0]]) ? $this->_attributes[$keys[0]] : null;
} 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
*/
public function getOldPrimaryKey($asArray = false)
{
$key = $this->primaryKey();
$value = isset($this->_oldAttributes[$key]) ? $this->_oldAttributes[$key] : null;
if ($asArray) {
return [$key => $value];
$keys = $this->primaryKey();
if (count($keys) === 1 && !$asArray) {
return isset($this->_oldAttributes[$keys[0]]) ? $this->_oldAttributes[$keys[0]] : null;
} else {
return $value;
$values = [];
foreach ($keys as $name) {
$values[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
}
return $values;
}
}

2
extensions/sphinx/QueryBuilder.php

@ -493,7 +493,7 @@ class QueryBuilder extends Object
if (is_object($direction)) {
$orders[] = (string)$direction;
} else {
$orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : '');
$orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : 'ASC');
}
}

42
tests/unit/extensions/sphinx/ActiveDataProviderTest.php

@ -0,0 +1,42 @@
<?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));
}
}
Loading…
Cancel
Save