/** * 创建对象时,保存 many_to_many 关联 */ function testCreateWithManyToMany() { $tags = array('PHP', 'C++', 'Java'); $content = new Content(array('title' => 'title - ' . mt_rand(), 'author_id' => 0)); foreach ($tags as $tag_name) { $content->tags[] = new Tag(array('name' => $tag_name)); } $content->save(); $this->assertNotNull($content->id()); $row = $this->_queryContent($content->id()); $this->_checkContent($row, $content); foreach ($tags as $offset => $tag_name) { $row = $this->_queryTag($tag_name); $this->assertType('array', $row); $this->_checkTag($row, $content->tags[$offset]); } $mid_table_name = $content->getMeta()->associations['tags']->mid_table->qtable_name; $sql = "SELECT * FROM {$mid_table_name} WHERE content_id = ?"; $rowset = $this->_conn->getAll($sql, array($content->id())); $this->assertEquals(count($tags), count($rowset)); $tags = array_flip($tags); foreach ($rowset as $row) { $this->assertTrue(isset($tags[$row['tag_name']])); unset($tags[$row['tag_name']]); } }
function testGetAll() { $this->dbo->execute('DELETE FROM q_posts'); $idList = $this->_insertIntoPosts(10); $sql = "SELECT post_id FROM q_posts ORDER BY post_id ASC"; $rowset = $this->dbo->getAll($sql); for ($i = 0; $i < 10; $i++) { $this->assertEquals($idList[$i], $rowset[$i]['post_id']); } }