Zend 与 select 的关系 [英] zend relationships with select

查看:24
本文介绍了Zend 与 select 的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Zend 的新手.我被要求重新开发一个曾经用纯 PHP 编写的网站并将其放入 zend 框架中.

I am new to zend. I have been asked to redevelop a website that was once written in plain PHP and put it into the zend framework.

我在数据库关系方面遇到了很多麻烦,我似乎无法定义和查询关系.

I am having a lot of trouble with database relationships, I cant seem to get my head round defining and querying relationships.

我想找到一个类别.我希望能够从该类别中找到与其关联的所有 CategoryInfo,并能够查询/排序/限制该数据集.

I would like to find a Category. From that Category I would like to be able to find all the CategoryInfo associated with it, and be able to query/sort/limit that dataset.

这是我的模型.

类别.php

<?php
  class Default_Model_Categorys extends Zend_Db_Table_Abstract
  {
      protected $_name = 'Categorys';
      protected $_primary = 'id';

      protected $_dependentTables = array('Default_Model_CategoryInfo');
 }
?>

类别信息.php

<?php
class Default_Model_CategoryInfo extends Zend_Db_Table_Abstract
{
    protected $_name = 'Category_Info';
    protected $_primary = 'id';

    protected $_referenceMap = array(
        'Categorys' => array(
            'columns' => array('cat_id'),
            'refTableClass' => 'Default_Model_Categorys',
           'refColumns' => array('id')
        )
      );
}
?>

类别控制器.php

<?php
  class CategorysController extends Zend_Controller_Action
  {
      public function indexAction()
      {
        /*
          this should redirect to all games
        */
          return $this->_forward("index", "games");
      }

      public function categoryAction()
      {
          /*
            shows a specific category
          */
          $id = (int) $this->_request->getParam('id');
          $category = new Default_Model_Categorys();
          $this->view->category = $category->fetchRow(
              $category->select()->where('id = ?', $id)
          );

          $categoryInfo = $this->view->category->findDependentRowset('Default_Model_CategoryInfo');

      }
  }

首先……我做错了什么吗?

Firstly... am I doing anything wrong?

其次...如何查询相关行集?

Secondly... how do I go about querying the dependent rowset?

推荐答案

首先,如果您要通过主键搜索类别,使用 find() 方法会更简单:

First, if you're searching for a category by its primary key, it's simpler to use the find() method:

$id = (int) $this->_request->getParam('id');
$category = new Default_Model_Categorys();
$this->view->category = $category->find($id)->current();

其次,要限制或排序依赖的Category_Info 行,您可以使用Zend_Db_Table_Select 对象作为findDependentRowset() 的可选参数.举个例子:

Second, to restrict or sort dependent Category_Info rows, you can use a Zend_Db_Table_Select object as an optional parameter of findDependentRowset(). Here's an example:

$select = $category->select()->where("info_type = 'PRICE'")
                             ->order("info_date")
                             ->limit(3);
$categoryInfo = $this->view->category->findDependentRowset(
    'Default_Model_CategoryInfo', null, $select);

请注意,您可以使用任何表格对象来创建该选择对象.由于该选择的FROM"子句将由 findDependentRowset() 方法设置,因此您只需添加其他子句然后将其传入即可.

Notice you can use any table object to create that select object. Since the "FROM" clause for that select will be set by the findDependentRowset() method, you just add other clauses and then pass it in.

PS:您根本不需要声明$_dependentTables,除非您打算通过PHP 代码使用级联更新或级联删除.我强烈建议反对这样做——让 RDBMS 处理这些级联操作效率更高.

PS: You don't need to declare $_dependentTables at all, unless you're going to use cascading update or cascading delete via PHP code. I recommend strongly against doing that -- it's far more efficient to let the RDBMS handle those cascading operations.

同样,如果您的数据库表实际上声明了主键约束,则永远不必声明 $_primary.Zend_Db_Table_Abstract 知道如何检查元数据以获取主键列.

Likewise you should never have to declare $_primary if your database tables actually declare primary key constraints. Zend_Db_Table_Abstract knows how to inspect metadata to get the primary key column(s).

这篇关于Zend 与 select 的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆