public function testBuildUsingModel()
 {
     $builder = new InsertBuilder();
     $table = Article::table();
     $builder->setRootModel('Article');
     $options = array('fields' => array('blogId', 'title'), 'values' => array(12, 'Awesome!'));
     $components = $builder->build($options);
     $expectedFields = $table->columnRealName($options['fields']);
     $this->assertEquals($expectedFields, $components['insertColumns']);
     $this->assertEquals($options['values'], $components['values']);
 }
Example #2
0
 public function create()
 {
     if (!$this->has_post()) {
         return redirect_message(array('admin', $this->get_class(), 'add'), array('_flash_message' => '非 POST 方法,錯誤的頁面請求。'));
     }
     $posts = OAInput::post();
     $posts['content'] = OAInput::post('content', false);
     $cover = OAInput::file('cover');
     if (!$cover) {
         return redirect_message(array('admin', $this->get_class(), 'add'), array('_flash_message' => '請選擇照片(gif、jpg、png)檔案,或提供照片網址!', 'posts' => $posts));
     }
     if ($msg = $this->_validation_posts($posts)) {
         return redirect_message(array('admin', $this->get_class(), 'add'), array('_flash_message' => $msg, 'posts' => $posts));
     }
     $article = null;
     $create = Article::transaction(function () use($posts, $cover, &$article) {
         if (!verifyCreateOrm($article = Article::create(array_intersect_key($posts, Article::table()->columns)))) {
             return false;
         }
         if (!$article->cover->put($cover)) {
             return false;
         }
         return true;
     });
     if (!($create && $article)) {
         return redirect_message(array('admin', $this->get_class(), 'add'), array('_flash_message' => '新增失敗!', 'posts' => $posts));
     }
     if ($posts['tag_ids'] && ($tag_ids = column_array(ArticleTag::find('all', array('select' => 'id', 'conditions' => array('id IN (?)', $posts['tag_ids']))), 'id'))) {
         foreach ($tag_ids as $tag_id) {
             ArticleTagMapping::transaction(function () use($tag_id, $article) {
                 return verifyCreateOrm(ArticleTagMapping::create(array_intersect_key(array('article_tag_id' => $tag_id, 'article_id' => $article->id), ArticleTagMapping::table()->columns)));
             });
         }
     }
     if ($posts['sources']) {
         foreach ($posts['sources'] as $i => $source) {
             ArticleSource::transaction(function () use($i, $source, $article) {
                 return verifyCreateOrm(ArticleSource::create(array_intersect_key(array_merge($source, array('article_id' => $article->id, 'sort' => $i)), ArticleSource::table()->columns)));
             });
         }
     }
     return redirect_message(($url = Session::getData('admin_articles_index_url')) ? $url : array('admin', $this->get_class()), array('_flash_message' => '新增成功!'));
 }
 public function testWhereRelation()
 {
     $b = new WhereBuilder();
     $b->root('Article', 'articles');
     $b->whereRelation(Blog::relation('articles'), '_');
     $components = $b->build();
     $where[] = ['type' => 'Attribute', 'lTable' => 'articles', 'lCols' => ['blog_id'], 'op' => '=', 'rTable' => '_', 'rCols' => ['id'], 'logic' => 'AND'];
     $this->assertEquals($where, $components['where']);
     $this->assertEquals(Blog::table(), $b->getInvolvedTable('_'));
     // - - -
     // With many-to-many relation.
     $rel = Article::relation('readers');
     $mdName = $rel->getMiddleTableName();
     $mdAlias = $rel->getMiddleTableAlias();
     $b = new WhereBuilder();
     $b->root('User', 'readers');
     $b->whereRelation($rel, '_');
     $components = $b->build();
     $jc = new JoinClause($mdName, $mdAlias);
     $jc->on('readers', 'id', $mdAlias, 'user_id');
     $where = [['type' => 'Attribute', 'lTable' => $mdAlias, 'lCols' => ['article_id'], 'op' => '=', 'rTable' => '_', 'rCols' => ['id'], 'logic' => 'AND']];
     $this->assertEquals($where, $components['where']);
     $this->assertEquals(Article::table(), $b->getInvolvedTable('_'));
     $this->assertEquals($jc, $b->getJoinClause($mdAlias));
     try {
         $b->getJoinClause('_');
         $this->fail('Should have thrown an exception.');
     } catch (Exception $ex) {
         $this->assertContains('_', $ex->getMessage());
     }
 }
 /**
  * @covers ::distinct()
  */
 public function testDistinct()
 {
     $expected = ['columns' => ['_' => ['columns' => Article::table()->getColumns(), 'resAlias' => '']], 'from' => [new JoinClause('articles', '_')], 'distinct' => true];
     $b = new SelectBuilder();
     $b->root('Article');
     $b->distinct();
     $this->assertEquals($expected, $b->build());
     $b = new SelectBuilder();
     $b->root('Article');
     $b->distinct('*');
     $this->assertEquals($expected, $b->build());
     $b = new SelectBuilder();
     $b->root('Article');
     $b->distinct('*', false);
     $expected['columns'] = ['_' => ['columns' => ['*'], 'resAlias' => '']];
     $this->assertEquals($expected, $b->build());
     $b = new SelectBuilder();
     $b->root('Article');
     $b->distinct(['blogId', 'authorId']);
     $expected['columns'] = ['_' => ['columns' => ['blogId' => 'blog_id', 'authorId' => 'author_id', 'id' => 'id'], 'resAlias' => '']];
     $this->assertEquals($expected, $b->build());
 }
Example #5
0
 public function testGetID()
 {
     $a = new Article();
     $a->id = 2;
     $this->assertEquals([2], $a->id());
     Article::table()->primaryKey = ['id', 'blogId'];
     $a->id = 2;
     $a->blogId = 12;
     $this->assertEquals([2, 12], $a->id());
     Article::table()->primaryKey = ['id', 'title'];
     $a->id = 2;
     $a->title = 'string';
     $this->assertEquals([2, 'string'], $a->id());
     // Reset.
     Article::table()->primaryKey = ['id'];
 }