现在的位置: 首页 > 综合 > 正文

Zend_Db_Table Relationships 关联查询实战(一)

2018年01月30日 ⁄ 综合 ⁄ 共 3026字 ⁄ 字号 评论关闭

我们通过四个数据表articles(文章表),categories(分类表),tags(标签表),articles_tags(文章_标签对应表)
来学习使用Zend_Db_Table Relationships关联查询
以下是创建这四个表的SQL语句:

复制内容到剪贴板

PHP代码:
-- --------------------------------------------------------

--

-- 表的结构 `articles`

--

CREATE TABLE IF NOT EXISTS `articles` (

   `id` int(10) unsigned NOT NULL auto_increment,

   `cat_id` tinyint(3) unsigned NOT NULL default '1',

   `title` varchar(100) NOT NULL,

  PRIMARY KEY  (`id`),

  KEY `title` (`title`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--

-- 表的结构 `articles_tags`

--

CREATE TABLE IF NOT EXISTS `articles_tags` (

   `article_id` int(10) unsigned NOT NULL,

   `tag_id` int(10) unsigned NOT NULL,

  PRIMARY KEY  (`article_id`,`tag_id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--

-- 表的结构 `categories`

--

CREATE TABLE IF NOT EXISTS `categories` (

   `cat_id` tinyint(3) unsigned NOT NULL auto_increment,

   `parent_id` tinyint(3) unsigned NOT NULL default '0',

   `name` varchar(50) NOT NULL,

  PRIMARY KEY  (`cat_id`),

  KEY `name` (`name`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--

-- 表的结构 `tags`

--

CREATE TABLE IF NOT EXISTS `tags` (

   `tag_id` int(10) unsigned NOT NULL auto_increment,

   `name` varchar(100) NOT NULL,

  PRIMARY KEY  (`tag_id`),

  KEY `name` (`name`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

同时我们在文章表和分类表中插入一些测试数据

复制内容到剪贴板

PHP代码:
INSERT INTO `categories` (`cat_id`, `parent_id`, `name`) VALUES

(1, 0, '分类一'),

(2, 0, '分类二');



INSERT INTO `articles` (`id`, `cat_id`, `title`) VALUES

(1, 1, '这是一篇属于分类一的文章'),

(2, 1, '这是一篇属于分类一的文章'),

(3, 2, '这是一篇属于分类二的文章'),

(4, 2, '这是一篇属于分类二的文章');

下面分别定义文章模型文件(Article.php)和分类模型文件(Category.php):
models/Article.php

复制内容到剪贴板

PHP代码:
class Article extends Zend_Db_Table {

         protected $_name = 'articles';

         protected $_primary = 'id';

}

models/Category.php

复制内容到剪贴板

PHP代码:
class Category extends Zend_Db_Table {

         protected $_name = 'categories';

         protected $_primary = 'cat_id';

}

我们通过Zend_Db_Table的$_referenceMap属性来定义数据表的关联关系
models/Article.php

复制内容到剪贴板

PHP代码:
class Article extends Zend_Db_Table {

         protected $_name = 'articles';

         protected $_primary = 'id';

         protected $_referenceMap = array(

        'category' => array(                     //关联名称     

             'columns' => 'cat_id',                             

             'refTableClass' => 'Category',                

             'refColumns' => 'cat_id',                        

                 ),

         );

}

$_referenceMap 的相关属性说明:
columns:指定当前数据表通过那个字段和外表关联,一般指外键(PK)
refTableClass:关联表所对应的类名称
refColumns:关联表使用那个字段和其它表关联,一般指主键(FK)

现在我们已经将文章数据表和分类数据表关联起来了,下面我们看来如何在查询某一篇文章的同时查询出相关的分类信息,
修改modles/Article.php,添加下面方法:

复制内容到剪贴板

PHP代码:
public function getById($id)

{

        $where = $this -> select() -> where('id = ?', $id);

        $article = $this -> fetchRow($where);        

        $select = $this-> select() -> from('categories', array('name'));

        $category = $article -> findParentRow('Category', null, $select) -> toArray();

        $row = $article -> toArray();

        $row['category'] = $category;

         return $row;

}

然后在任意controller里,比如IndexController.php里

复制内容到剪贴板

PHP代码:
class IndexController extends Zend_Controller_Action {

         public function init()

         {        

         }

         public function indexAction()

         {

                $modelArticle = new Article();

                $article = $modelArticle -> getById(3);

                

                Zend_Debug::dump($article);

         }

        

}

运行程序结果

复制内容到剪贴板

PHP代码:
array(4) {

   ["id"] => string(1) "3"

  ["cat_id"] => string(1) "2"

  ["title"] => string(24) "这是一篇属于分类二的文章"

  ["category"] => array(1) {

     ["name"] => string(6) "分类二"

  }

}
 

 

 

 

抱歉!评论已关闭.