PHPEye开源社区 » Zend Framework 使用讨论 » 关于 zf 的关联问题
《Programming PHP》第二版上市
2007-12-23 19:28 cosnis
关于 zf 的关联问题

一个很简单的关联

表1: 文章表 ( 里面有一个字段是分类表的主键 )

表2: 分类表

在查询文章表的时候自动关联查询分类表中的信息

不用 foreach 的方法 该怎么操作呢??
难道真的手写SQL??

感觉 zend_db_table 在处理关联的时候真的太难受了....

2007-12-26 12:36 wps2000
Zend_Db_Table Relationships 不能做到?

手写SQL也不错,我觉得在Read的时候, 直接使用Zend_Db_Adapter 手写SQL是最快(开发效率)的,只是在Write的时候,我才使用Zend_Db_Table

2008-1-2 08:55 ringtail
有点没看懂问题,


[php]$select = $this->db->select();
$select->from('tableA', array('a','b'))
         ->join('tableB,'tableA.ID= tableB.ID',array('c','d'));
[/php]

这样是否可以解决你说的关联的问题?

2008-1-5 14:54 cosnis
晕倒....

article table
pky caption category
1    xxx       1,2,3
2    fff         1,2

category table
pky category_caption
1    ddd
2    kkk
3    sss


现在我要查所有的 article 表的内容

然后查出来的内容中都有 category 的属性

Relationships 我看了好像还是要用到循环  能不能尽量不用到循环.....

我现在的解决方法是多加一个中间表
然后用PHP  的循环来解决... 不过感觉很麻烦......  希望能用框架来搞(这不就是我们使用框架的初衷么?) 这样的解决用 fleaphp 的话就是一个 hasMany ...郁闷啊

2008-1-14 22:21 Cykit
像你这种属于 many to many 的本应该设计成三个表

article table
pky caption
1    xxx      
2    fff         

category table
pky category_caption
1    ddd
2    kkk
3    sss

articles_category
pky apky cpky
1    1       1
2    1       2
3    1       3
4    2       1
5    2       2

Zend_DB在0.3之前看了,那个时候还自己实现了has_many, belongs_to, has_and_belongs_to_many,至于ZF的Zend_DB_Table_Relationship应该是可以“优雅地”解决你说的问题的,不过还没证实,看完了Zend_DB在回来给答案!

2008-1-15 13:55 Cykit
续上

将表名改为复数,以符合惯例。[code]三个表分别是

articles
id caption
1    xxx      
2    fff         

categories
id category_caption
1    ddd
2    kkk
3    sss

articles_categories
id article_id category_id
1    1            1
2    1            2
3    1            3
4    2            1  
5    2            2[/code][php]

class Articles extends Zend_Db_Table_Abstract
{
     protected $_name                  = 'products';
     protected $_dependentTables =  array('ArticlesCategories');
}


class Categories extends Zend_Db_Table_Abstract
{
     protected $_name                  = 'categories';
     protected $_dependentTables =  array('ArticlesCategories');
}


class ArticlesCategories extends Zend_Db_Table_Abstract
{
      protected $_name                  = 'articles_categories';
      protected $_referenceMap    = array(
         'Article'  => array(
                'columns'        =>  array('article_id'),
                'refTableClass'  =>   'Articles',
                'refColumns'    =>   array('article_id')
         ),
         'Category'   =>  array(
                'columns'        =>  array('category_id'),
                'refTableClass'  =>   'Categories',
                'refColumns'    =>   array('category_id')            
        )
}



$articles = new Articles();



//遍历所有文章

foreach($articles->fetchAll() as $article)

{

     //获取类别

     $categories = $article->findCategoriesViaArticlesCategoriesByArticle();



    /*

       爱干啥干啥

    */

}

[/php]

[[i] 本帖最后由 Cykit 于 2008-1-15 14:15 编辑 [/i]]

2008-2-2 20:52 superwen
[quote]原帖由 [i]Cykit[/i] 于 2008-1-15 13:55 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=1204&ptid=294][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
续上

将表名改为复数,以符合惯例。三个表分别是

articles
id caption
1    xxx      
2    fff         

categories
id category_caption
1    ddd
2    kkk
3    sss

articles_categories
id article_id categ ... [/quote]


是的,这个列子太有代表性了,典型的many to many。在cakephp中的处理也和这个雷同,但是cakephp处理的时候要比这个简单易懂一些。

页: [1]
查看完整版本: 关于 zf 的关联问题


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.