zend框架中如何对两个表进行分页

发布于 2024-10-13 20:22:05 字数 602 浏览 5 评论 0原文

图书具有以下字段

  • book_id
  • book_name
  • book_auther
  • book_pub_date

类别表有

  • category_id
  • Category_name

展示位置表有

  • placement_idplacement_category_id
  • (FK)
  • placement_book_id(FK)

现在我们想在索引控制器中使用分页来 选择特定类别的书籍?
所有表都在一个数据库中


注意:我为每个表设置了单独的模型,并且所有表都通过 $_referenceMap 相互关联
我使用 $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
问题是:如何进行 $select ?

Books has the following fields

  • book_id
  • book_name
  • book_auther
  • book_pub_date

category table has

  • category_id
  • category_name

placement table has

  • placement_id
  • placement_category_id(FK)
  • placement_book_id(FK)

Now we want to use pagination in index controller to
select books with specific category ?
all tables are in one database


Note: I have separated model for each table and all tables are related each other with $_referenceMap
I use $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
the question is : how to make $select ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的味道 2024-10-20 20:22:05

书籍和类别之间存在多对多关系,并且您的放置表是交集表。因此,我认为构建 $select 的一种方法是使用内部联接,如下所示:

    $placementModel = new Your_Model_Table_Placement();

    $select = $placementModel->select(Zend_Db_Table::SELECT_WITH_FROM_PART)->setIntegrityCheck(false);
    $select->joinInner('BOOKS', 'BOOKS.book_id = PLACEMENT.placement_book_id');
    $select->where('PLACEMENT.placement_category_id = ?', $categoryID);

    $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);

    // check the result if they are what you expect
    var_dump($adapter->getItems(0, 5)->toArray());

当然,表和模型的名称必须与您的真实姓名匹配。
另一种方法是在数据库中创建视图。然后您将为视图创建一个模型。这将使 $select 更短。

You have many-to-many relationship between books and categories, and your placement table is an intersection table. Thus I think that one way your $select could be constructed is using inner join as follows:

    $placementModel = new Your_Model_Table_Placement();

    $select = $placementModel->select(Zend_Db_Table::SELECT_WITH_FROM_PART)->setIntegrityCheck(false);
    $select->joinInner('BOOKS', 'BOOKS.book_id = PLACEMENT.placement_book_id');
    $select->where('PLACEMENT.placement_category_id = ?', $categoryID);

    $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);

    // check the result if they are what you expect
    var_dump($adapter->getItems(0, 5)->toArray());

Off course the names of tables and models must match your real names.
Another way would be to create a view in your database. Then you would create a model for the view. This would make the $select shorter.

挽心 2024-10-20 20:22:05

您可以使用此分页类:

http://www .catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/

设置和使用非常简单,然后将其应用于 JOINed 查询,该查询从特定的位置选择所有书籍类别(这里很多人会比我解释得更好)。

You can use this pagination class:

http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/

it's really simple to setup and use, and then you apply it to a JOINed query which selects all the books from the specific category (which many here would explain better than me).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文