Commit e19c9ceb by Paul Klimov

Sphinx has many relation test prepared.

parent 6a5b8d19
...@@ -23,6 +23,11 @@ class ArticleIndex extends ActiveRecord ...@@ -23,6 +23,11 @@ class ArticleIndex extends ActiveRecord
return $this->hasOne('db', ArticleDb::className(), ['id' => 'id']); return $this->hasOne('db', ArticleDb::className(), ['id' => 'id']);
} }
public function getTags()
{
return $this->hasMany('db', TagDb::className(), ['id' => 'tag']);
}
public function getSnippetSource() public function getSnippetSource()
{ {
return $this->source->content; return $this->source->content;
......
<?php
namespace yiiunit\data\sphinx\ar;
use yiiunit\data\ar\ActiveRecord as ActiveRecordDb;
class TagDb extends ActiveRecordDb
{
public static function tableName()
{
return 'yii2_test_tag';
}
}
\ No newline at end of file
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
DROP TABLE IF EXISTS yii2_test_article; DROP TABLE IF EXISTS yii2_test_article;
DROP TABLE IF EXISTS yii2_test_item; DROP TABLE IF EXISTS yii2_test_item;
DROP TABLE IF EXISTS yii2_test_tag;
DROP TABLE IF EXISTS yii2_test_article_tag; DROP TABLE IF EXISTS yii2_test_article_tag;
CREATE TABLE IF NOT EXISTS `yii2_test_article` ( CREATE TABLE IF NOT EXISTS `yii2_test_article` (
...@@ -22,7 +23,13 @@ CREATE TABLE IF NOT EXISTS `yii2_test_item` ( ...@@ -22,7 +23,13 @@ CREATE TABLE IF NOT EXISTS `yii2_test_item` (
`category_id` int(11) NOT NULL, `category_id` int(11) NOT NULL,
`price` float NOT NULL, `price` float NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3;
CREATE TABLE IF NOT EXISTS `yii2_test_tag` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5;
CREATE TABLE IF NOT EXISTS `yii2_test_article_tag` ( CREATE TABLE IF NOT EXISTS `yii2_test_article_tag` (
`article_id` int(11) NOT NULL, `article_id` int(11) NOT NULL,
...@@ -38,6 +45,12 @@ INSERT INTO `yii2_test_item` (`id`, `name`, `description`, `category_id`, `price ...@@ -38,6 +45,12 @@ INSERT INTO `yii2_test_item` (`id`, `name`, `description`, `category_id`, `price
(1, 'pencil', 'Simple pencil', 1, 2.5), (1, 'pencil', 'Simple pencil', 1, 2.5),
(2, 'table', 'Wooden table', 2, 100); (2, 'table', 'Wooden table', 2, 100);
INSERT INTO `yii2_test_tag` (`id`, `name`) VALUES
(1, 'tag1'),
(2, 'tag2'),
(3, 'tag3'),
(4, 'tag4');
INSERT INTO `yii2_test_article_tag` (`article_id`, `tag_id`) VALUES INSERT INTO `yii2_test_article_tag` (`article_id`, `tag_id`) VALUES
(1, 1), (1, 1),
(1, 2), (1, 2),
......
...@@ -6,6 +6,7 @@ use yiiunit\data\sphinx\ar\ActiveRecord; ...@@ -6,6 +6,7 @@ use yiiunit\data\sphinx\ar\ActiveRecord;
use yiiunit\data\ar\ActiveRecord as ActiveRecordDb; use yiiunit\data\ar\ActiveRecord as ActiveRecordDb;
use yiiunit\data\sphinx\ar\ArticleIndex; use yiiunit\data\sphinx\ar\ArticleIndex;
use yiiunit\data\sphinx\ar\ArticleDb; use yiiunit\data\sphinx\ar\ArticleDb;
use yiiunit\data\sphinx\ar\TagDb;
/** /**
* @group sphinx * @group sphinx
...@@ -25,21 +26,37 @@ class ExternalActiveRelationTest extends SphinxTestCase ...@@ -25,21 +26,37 @@ class ExternalActiveRelationTest extends SphinxTestCase
{ {
/** @var ArticleIndex $article */ /** @var ArticleIndex $article */
$article = ArticleIndex::find(['id' => 2]); $article = ArticleIndex::find(['id' => 2]);
// has one :
$this->assertFalse($article->isRelationPopulated('source')); $this->assertFalse($article->isRelationPopulated('source'));
$source = $article->source; $source = $article->source;
$this->assertTrue($article->isRelationPopulated('source')); $this->assertTrue($article->isRelationPopulated('source'));
$this->assertTrue($source instanceof ArticleDb); $this->assertTrue($source instanceof ArticleDb);
$this->assertEquals(1, count($article->populatedRelations)); $this->assertEquals(1, count($article->populatedRelations));
// has many :
/*$this->assertFalse($article->isRelationPopulated('tags'));
$tags = $article->tags;
$this->assertTrue($article->isRelationPopulated('tags'));
$this->assertEquals(3, count($tags));
$this->assertTrue($tags[0] instanceof TagDb);*/
} }
public function testFindEager() public function testFindEager()
{ {
// has one :
$articles = ArticleIndex::find()->with('source')->all(); $articles = ArticleIndex::find()->with('source')->all();
$this->assertEquals(2, count($articles)); $this->assertEquals(2, count($articles));
$this->assertTrue($articles[0]->isRelationPopulated('source')); $this->assertTrue($articles[0]->isRelationPopulated('source'));
$this->assertTrue($articles[1]->isRelationPopulated('source')); $this->assertTrue($articles[1]->isRelationPopulated('source'));
$this->assertTrue($articles[0]->source instanceof ArticleDb); $this->assertTrue($articles[0]->source instanceof ArticleDb);
$this->assertTrue($articles[1]->source instanceof ArticleDb); $this->assertTrue($articles[1]->source instanceof ArticleDb);
// has many :
/*$articles = ArticleIndex::find()->with('tags')->all();
$this->assertEquals(2, count($articles));
$this->assertTrue($articles[0]->isRelationPopulated('tags'));
$this->assertTrue($articles[1]->isRelationPopulated('tags'));*/
} }
/** /**
......
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