findParentRow 以及findDependentRowSet 仅适用于单条(行)数据。
这里有三张表:文章表、文章分类表以及文章内容表,文章表通过分类id来关联分类信息、通过文章id来关联文章的内容,现在是想通过文章id把文章的信息读取出来,包括文章的分类名称以及文章内容。以下是创建这三张表的SQL语句:
[php] view plaincopy
--
-- 表的结构 `article`
--
CREATE TABLE IF NOT EXISTS `article` (
`id` int(6) NOT NULL AUTO_INCREMENT COMMENT '文章id',
`c_id` int(6) NOT NULL COMMENT '分类id',
`title` varchar(20) NOT NULL COMMENT '文章标题',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=2 ;
--
-- 转存表中的数据 `article`
--
INSERT INTO `article` (`id`, `c_id`, `title`) VALUES
(1, 1, '王菲要开演唱会啦~');
-- --------------------------------------------------------
--
-- 表的结构 `category`
--
CREATE TABLE IF NOT EXISTS `category` (
`id` int(6) NOT NULL COMMENT '分类id',
`name` varchar(20) NOT NULL COMMENT '分类名称',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
--
-- 转存表中的数据 `category`
--
INSERT INTO `category` (`id`, `name`) VALUES
(1, '娱乐新闻');
-- --------------------------------------------------------
--
-- 表的结构 `content`
--
CREATE TABLE IF NOT EXISTS `content` (
`id` int(6) NOT NULL COMMENT '文章id',
`content` varchar(1000) NOT NULL COMMENT '文章内容',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
--
-- 转存表中的数据 `content`
--
INSERT INTO `content` (`id`, `content`) VALUES
(1, '新浪娱乐讯 离开歌坛已经六年歌坛天后王菲终于回归,正式确定复出举办巡回演唱会。演唱会分别在北京、上海两地举行,共十场,北京五场敲定为10月29-31日、11月5-6日五棵松体育馆,上海五场定为11月19-20日、26-28日世博文化中心。');
接下来便是利用Zend_Db_Table Relationships 实现关联查询的代码,先是在必要的Model 中定义好主外键的关系(protected $_referenceMap),然后在Article模型中的getArticleById 方法中利用findParentRow 以及findDependentRowSet 方法实现对主表和从表的关联查询,以达到最终的目的
[php] view plaincopy
<?php
require_once 'Zend/Db/Table.php';
$params = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'relationship_test',
'charset' => 'GBK'
);
$db = Zend_Db::factory('Pdo_Mysql', $params);
Zend_Db_Table::setDefaultAdapter($db);
/**
* 文章model,字段有id、c_id、title
*/
class Article extends Zend_Db_Table
{
protected $_referenceMap = array(
'CategoryRef' => array(
'columns' => 'c_id',
'refTableClass' => 'Category',
'refColumns' => 'id'
)
);
/**
* getArticleById 方法
* 通过过id获取一篇文章的信息,包括分类名称和文章内容
*/
public function getArticleById($id)
{
$row = $this->find($id)->current();
$category = $row->findParentRow('Category')->toArray();
$content = $row->findDependentRowSet('Content')->toArray();
$article = $row->toArray();
$article['category'] = $category['name'];
$article['content'] = $content[0]['content'];
return $article;
}
}
/**
* 分类model,字段有id、name
*/
class Category extends Zend_Db_Table
{
protected $_sequence = false;
}
/**
* 文章内容model,字段有id、content
*/
class Content extends Zend_Db_Table
{
protected $_sequence = false;
protected $_referenceMap = array(
'ArticleRef' => array(
'columns' => 'id',
'refTableClass' => 'Article',
'refColumns' => 'id'
)
);
}
/**** 测试 ****/
$article = new Article();
$test_row = $article->getArticleById(1);
print_r($test_row);
输出的结果是:
[php] view plaincopy
Array
(
[id] => 1
[c_id] => 1
[title] => 王菲要开演唱会啦~
[category] => 娱乐新闻
[content] => 五场定为11月19-20日、26-28日世博文化中心。
)