Commit a7ecbfee by Qiang Xue

Fixing AR issues.

parent 30c52fad
...@@ -214,6 +214,11 @@ class ActiveQuery extends BaseQuery ...@@ -214,6 +214,11 @@ class ActiveQuery extends BaseQuery
// todo: normalize $relations // todo: normalize $relations
$primaryModel = new $this->modelClass; $primaryModel = new $this->modelClass;
foreach ($relations as $name => $properties) { foreach ($relations as $name => $properties) {
if (is_integer($name)) {
$name = $properties;
$properties = array();
}
if (!method_exists($primaryModel, $name)) { if (!method_exists($primaryModel, $name)) {
throw new Exception("Unknown relation: $name"); throw new Exception("Unknown relation: $name");
} }
...@@ -227,7 +232,13 @@ class ActiveQuery extends BaseQuery ...@@ -227,7 +232,13 @@ class ActiveQuery extends BaseQuery
// inherit asArray from primary query // inherit asArray from primary query
$relation->asArray = $this->asArray; $relation->asArray = $this->asArray;
} }
if ($relation->via !== null) {
$viaName = $relation->via;
$viaQuery = $primaryModel->$viaName();
$relation->findWith($name, $models, $viaQuery);
} else {
$relation->findWith($name, $models); $relation->findWith($name, $models);
} }
} }
}
} }
...@@ -172,7 +172,8 @@ abstract class ActiveRecord extends Model ...@@ -172,7 +172,8 @@ abstract class ActiveRecord extends Model
} }
} elseif ($q !== null) { } elseif ($q !== null) {
$query->select = array($q); $query->select = array($q);
} elseif ($query->select === null) { }
if ($query->select === null) {
$query->select = array('COUNT(*)'); $query->select = array('COUNT(*)');
} }
return $query; return $query;
...@@ -300,7 +301,7 @@ abstract class ActiveRecord extends Model ...@@ -300,7 +301,7 @@ abstract class ActiveRecord extends Model
return null; return null;
} elseif (method_exists($this, $name)) { } elseif (method_exists($this, $name)) {
// related records // related records
if (isset($this->_related[$name]) || array_key_exists($name, $this->_related)) { if (isset($this->_related[$name]) || isset($this->_related) && array_key_exists($name, $this->_related)) {
return $this->_related[$name]; return $this->_related[$name];
} else { } else {
// lazy loading related records // lazy loading related records
...@@ -379,8 +380,15 @@ abstract class ActiveRecord extends Model ...@@ -379,8 +380,15 @@ abstract class ActiveRecord extends Model
*/ */
public function hasOne($class, $link, $properties = array()) public function hasOne($class, $link, $properties = array())
{ {
$properties['primaryModel'] = $this; if (strpos($class, '\\') === false) {
$primaryClass = get_class($this);
if (strpos($primaryClass, '\\') !== false) {
$class = dirname($primaryClass) . '\\' . $class;
}
}
$properties['modelClass'] = $class; $properties['modelClass'] = $class;
$properties['primaryModel'] = $this;
$properties['link'] = $link; $properties['link'] = $link;
$properties['multiple'] = false; $properties['multiple'] = false;
return new ActiveRelation($properties); return new ActiveRelation($properties);
...@@ -400,11 +408,9 @@ abstract class ActiveRecord extends Model ...@@ -400,11 +408,9 @@ abstract class ActiveRecord extends Model
*/ */
public function hasMany($class, $link, $properties = array()) public function hasMany($class, $link, $properties = array())
{ {
$properties['primaryModel'] = $this; $relation = $this->hasOne($class, $link, $properties);
$properties['modelClass'] = $class; $relation->multiple = true;
$properties['link'] = $link; return $relation;
$properties['multiple'] = true;
return new ActiveRelation($properties);
} }
/** /**
......
...@@ -49,7 +49,7 @@ class ActiveRelation extends ActiveQuery ...@@ -49,7 +49,7 @@ class ActiveRelation extends ActiveQuery
public function via($modelClass, $properties = array()) public function via($modelClass, $properties = array())
{ {
$this->via = array($modelClass, $properties); $this->via = $modelClass;
return $this; return $this;
} }
...@@ -65,9 +65,12 @@ class ActiveRelation extends ActiveQuery ...@@ -65,9 +65,12 @@ class ActiveRelation extends ActiveQuery
if ($this->via !== null) { if ($this->via !== null) {
/** @var $viaQuery ActiveRelation */ /** @var $viaQuery ActiveRelation */
$viaName = $this->via; $viaName = $this->via;
$viaQuery = $this->$viaName(); $viaModels = $this->primaryModel->$viaName;
$primaryModels = array($this->primaryModel); if ($viaModels === null) {
$viaModels = $viaQuery->findWith($viaName, $primaryModels); $viaModels = array();
} elseif (!is_array($viaModels)) {
$viaModels = array($viaModels);
}
$this->filterByModels($viaModels); $this->filterByModels($viaModels);
} else { } else {
$this->filterByModels(array($this->primaryModel)); $this->filterByModels(array($this->primaryModel));
...@@ -76,17 +79,14 @@ class ActiveRelation extends ActiveQuery ...@@ -76,17 +79,14 @@ class ActiveRelation extends ActiveQuery
return parent::createCommand(); return parent::createCommand();
} }
public function findWith($name, &$primaryModels) public function findWith($name, &$primaryModels, $viaQuery = null)
{ {
if (!is_array($this->link)) { if (!is_array($this->link)) {
throw new \yii\base\Exception('invalid link'); throw new \yii\base\Exception('invalid link');
} }
if ($this->via !== null) { if ($viaQuery !== null) {
/** @var $viaQuery ActiveRelation */ $viaModels = $viaQuery->findWith($this->via, $primaryModels);
$viaName = $this->via;
$viaQuery = $this->$viaName();
$viaModels = $viaQuery->findWith($viaName, $primaryModels);
$this->filterByModels($viaModels); $this->filterByModels($viaModels);
} else { } else {
$this->filterByModels($primaryModels); $this->filterByModels($primaryModels);
...@@ -178,21 +178,21 @@ class ActiveRelation extends ActiveQuery ...@@ -178,21 +178,21 @@ class ActiveRelation extends ActiveQuery
{ {
$attributes = array_keys($this->link); $attributes = array_keys($this->link);
$values = array(); $values = array();
if (isset($links[1])) { if (count($attributes) ===1) {
// single key
$attribute = reset($this->link);
foreach ($models as $model) {
$values[] = $model[$attribute];
}
} else {
// composite keys // composite keys
foreach ($models as $model) { foreach ($models as $model) {
$v = array(); $v = array();
foreach ($this->link as $attribute => $link) { foreach ($this->link as $attribute => $link) {
$v[$attribute] = is_array($model) ? $model[$link] : $model->$link; $v[$attribute] = $model[$link];
} }
$values[] = $v; $values[] = $v;
} }
} else {
// single key
$attribute = $this->link[$links[0]];
foreach ($models as $model) {
$values[] = is_array($model) ? $model[$attribute] : $model->$attribute;
}
} }
$this->andWhere(array('in', $attributes, $values)); $this->andWhere(array('in', $attributes, $values));
} }
......
...@@ -365,7 +365,7 @@ class Command extends \yii\base\Component ...@@ -365,7 +365,7 @@ class Command extends \yii\base\Component
} }
\Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__); \Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__);
echo $sql."\n\n";
if ($db->queryCachingCount > 0 && $db->queryCachingDuration >= 0 && $method !== '') { if ($db->queryCachingCount > 0 && $db->queryCachingDuration >= 0 && $method !== '') {
$cache = \Yii::$application->getComponent($db->queryCacheID); $cache = \Yii::$application->getComponent($db->queryCacheID);
} }
......
...@@ -568,7 +568,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -568,7 +568,7 @@ class QueryBuilder extends \yii\base\Object
$values = (array)$values; $values = (array)$values;
if ($values === array() || $column === array()) { if ($values === array() || $column === array()) {
return $operator === 'in' ? '0=1' : ''; return $operator === 'IN' ? '0=1' : '';
} }
if (is_array($column)) { if (is_array($column)) {
...@@ -593,7 +593,12 @@ class QueryBuilder extends \yii\base\Object ...@@ -593,7 +593,12 @@ class QueryBuilder extends \yii\base\Object
$column = $this->quoteColumnName($column); $column = $this->quoteColumnName($column);
} }
if (count($values) > 1) {
return "$column $operator (" . implode(', ', $values) . ')'; return "$column $operator (" . implode(', ', $values) . ')';
} else {
$operator = $operator === 'IN' ? '=' : '<>';
return "$column$operator{$values[0]}";
}
} }
protected function buildCompositeInCondition($operator, $columns, $values) protected function buildCompositeInCondition($operator, $columns, $values)
......
...@@ -52,11 +52,11 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase ...@@ -52,11 +52,11 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals('user2', $customer->name); $this->assertEquals('user2', $customer->name);
// find count // find count
// $this->assertEquals(3, Customer::count()); $this->assertEquals(3, Customer::count()->value());
// $this->assertEquals(2, Customer::count(array( $this->assertEquals(2, Customer::count(array(
// 'where' => 'id=1 OR id=2', 'where' => 'id=1 OR id=2',
// ))); ))->value());
// $this->assertEquals(2, Customer::find()->select('COUNT(*)')->where('id=1 OR id=2')->value()); $this->assertEquals(2, Customer::find()->select('COUNT(*)')->where('id=1 OR id=2')->value());
} }
public function testFindBySql() public function testFindBySql()
...@@ -86,25 +86,52 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase ...@@ -86,25 +86,52 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$customers = Customer::find()->active()->all(); $customers = Customer::find()->active()->all();
$this->assertEquals(2, count($customers)); $this->assertEquals(2, count($customers));
} }
//
// public function testFindLazy() public function testFindLazy()
// { {
// $customer = Customer::find(2); /** @var $customer Customer */
// $orders = $customer->orders; $customer = Customer::find(2);
// $this->assertEquals(2, count($orders)); $orders = $customer->orders;
// $this->assertEquals(2, count($orders));
// $orders = $customer->orders()->where('id=3')->all();
// $this->assertEquals(1, count($orders)); $orders = $customer->orders()->where('id=3')->all();
// $this->assertEquals(3, $orders[0]->id); $this->assertEquals(1, count($orders));
// } $this->assertEquals(3, $orders[0]->id);
// }
// public function testFindEager()
// { public function testFindEager()
// $customers = Customer::find()->with('orders')->all(); {
// $this->assertEquals(3, count($customers)); $customers = Customer::find()->with('orders')->all();
// $this->assertEquals(1, count($customers[0]->orders)); $this->assertEquals(3, count($customers));
// $this->assertEquals(2, count($customers[1]->orders)); $this->assertEquals(1, count($customers[0]->orders));
// } $this->assertEquals(2, count($customers[1]->orders));
}
public function testFindLazyVia()
{
/** @var $order Order */
$order = Order::find(1);
$this->assertEquals(1, $order->id);
$this->assertEquals(2, count($order->items));
$this->assertEquals(1, $order->items[0]->id);
$this->assertEquals(2, $order->items[1]->id);
$order = Order::find(1);
$order->id = 100;
$this->assertEquals(array(), $order->items);
}
public function testFindEagerVia()
{
$orders = Order::find()->with('items')->orderBy('id')->all();
$this->assertEquals(3, count($orders));
$order = $orders[0];
$this->assertEquals(1, $order->id);
$this->assertEquals(2, count($order->items));
$this->assertEquals(1, $order->items[0]->id);
$this->assertEquals(2, $order->items[1]->id);
}
// public function testInsert() // public function testInsert()
// { // {
......
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