コード例 #1
0
 public function testFind()
 {
     // find one
     $result = ArticleIndex::find();
     $this->assertTrue($result instanceof ActiveQuery);
     $article = $result->one();
     $this->assertTrue($article instanceof ArticleIndex);
     // find all
     $articles = ArticleIndex::find()->all();
     $this->assertEquals(2, count($articles));
     $this->assertTrue($articles[0] instanceof ArticleIndex);
     $this->assertTrue($articles[1] instanceof ArticleIndex);
     // find fulltext
     $article = ArticleIndex::findOne(2);
     $this->assertTrue($article instanceof ArticleIndex);
     $this->assertEquals(2, $article->id);
     // find by column values
     $article = ArticleIndex::findOne(['id' => 2, 'author_id' => 2]);
     $this->assertTrue($article instanceof ArticleIndex);
     $this->assertEquals(2, $article->id);
     $this->assertEquals(2, $article->author_id);
     $article = ArticleIndex::findOne(['id' => 2, 'author_id' => 1]);
     $this->assertNull($article);
     // find by attributes
     $article = ArticleIndex::find()->where(['author_id' => 2])->one();
     $this->assertTrue($article instanceof ArticleIndex);
     $this->assertEquals(2, $article->id);
     // find custom column
     $article = ArticleIndex::find()->select(['*', '(5*2) AS custom_column'])->where(['author_id' => 1])->one();
     $this->assertEquals(1, $article->id);
     $this->assertEquals(10, $article->custom_column);
     // find count, sum, average, min, max, scalar
     $this->assertEquals(2, ArticleIndex::find()->count());
     $this->assertEquals(1, ArticleIndex::find()->where('id=1')->count());
     $this->assertEquals(3, ArticleIndex::find()->sum('id'));
     $this->assertEquals(1.5, ArticleIndex::find()->average('id'));
     $this->assertEquals(1, ArticleIndex::find()->min('id'));
     $this->assertEquals(2, ArticleIndex::find()->max('id'));
     $this->assertEquals(2, ArticleIndex::find()->select('COUNT(*)')->scalar());
     // scope
     $this->assertEquals(1, ArticleIndex::find()->favoriteAuthor()->count());
     // asArray
     $article = ArticleIndex::find()->where('id=2')->asArray()->one();
     unset($article['add_date']);
     $this->assertEquals(['id' => '2', 'author_id' => '2', 'tag' => '3,4'], $article);
     // indexBy
     $articles = ArticleIndex::find()->indexBy('author_id')->orderBy('id DESC')->all();
     $this->assertEquals(2, count($articles));
     $this->assertTrue($articles['1'] instanceof ArticleIndex);
     $this->assertTrue($articles['2'] instanceof ArticleIndex);
     // indexBy callable
     $articles = ArticleIndex::find()->indexBy(function ($article) {
         return $article->id . '-' . $article->author_id;
     })->orderBy('id DESC')->all();
     $this->assertEquals(2, count($articles));
     $this->assertTrue($articles['1-1'] instanceof ArticleIndex);
     $this->assertTrue($articles['2-2'] instanceof ArticleIndex);
 }
コード例 #2
0
 /**
  * @see https://github.com/yiisoft/yii2/issues/4018
  */
 public function testFindCompositeLink()
 {
     $articles = ArticleIndex::find()->with('sourceCompositeLink')->all();
     $this->assertEquals(2, count($articles));
     $this->assertTrue($articles[0]->isRelationPopulated('sourceCompositeLink'));
     $this->assertNotEmpty($articles[0]->sourceCompositeLink);
     $this->assertTrue($articles[1]->isRelationPopulated('sourceCompositeLink'));
     $this->assertNotEmpty($articles[1]->sourceCompositeLink);
 }
コード例 #3
0
 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));
 }
コード例 #4
0
ファイル: ActiveRecordTest.php プロジェクト: aivavic/yii2
 /**
  * @see https://github.com/yiisoft/yii2/issues/4830
  *
  * @depends testFind
  */
 public function testFindQueryReuse()
 {
     $result = ArticleIndex::find()->andWhere(['author_id' => 1]);
     $this->assertTrue($result->one() instanceof ArticleIndex);
     $this->assertTrue($result->one() instanceof ArticleIndex);
     $result = ArticleIndex::find()->match('dogs');
     $this->assertTrue($result->one() instanceof ArticleIndex);
     $this->assertTrue($result->one() instanceof ArticleIndex);
 }
コード例 #5
0
 /**
  * @depends testFindEager
  */
 public function testFindWithSnippets()
 {
     $articles = ArticleIndex::find()->match('about')->with('source')->snippetByModel()->all();
     $this->assertEquals(2, count($articles));
 }