Zend Db Table Double JOIN

时间:2010-10-13 06:36:20

标签: php mysql zend-framework join

1. plan_payment_type:
   id
   plan_id

2. plan
   id
   payment_period

plan_payment_type.plan_idplan.id之间的外键。在我的代码中,我知道$ plan_payment_type_id。

我的任务是选择payment_period。 步骤进行:

  • plan_id
  • 中选择plan_payment_type
  • 使用选定的payment_period from
  • 选择plan plan_id

如何在Zend Framework中使用一个查询? 非常感谢你。

2 个答案:

答案 0 :(得分:2)

以下SQL语句和PHP代码相互对应:

SELECT
  t.id AS plan_payment_type_id,
  p.id AS plan_id,
  p.payment_period
FROM
  plan_payment_type AS t
  INNER JOIN plan AS p ON p.id = t.plan_id
WHERE
  p.id = { SQL query param for $plan_payment_type_id }


$select = $db->select()
          ->from(array('t' => 'plan_payment_type'),
                 array('id' => 'plan_payment_type_id'))
          ->join(array('p' => 'plan'), 'p.id = t.plan_id',
                 array('id' => 'plan_id', 'payment_period'))
          ->where('p.id = ?', $plan_payment_type_id);

答案 1 :(得分:1)

在所有事情之前,我会以不同的方式组织它不是plan_types应该有plan.id.这不符合逻辑。你有一个带有plan_types的表,它有一个plan.id和一个plan.name,并且该关系在plan.type_id的计划表中。

dependencies以这种方式解决:

class Payment_Plan extends Zend_Db_Table_Abstract
{
    protected $_name            = 'plan';
    protected $_referenceMap    = array(
        'Plan' => array(
            'columns'           => 'plan_id',
            'refTableClass'     => 'Plan',
            'refColumns'        => 'id'
        )

}

class Plan_Type extends Zend_Db_Table_Abstract
{
    protected $_name            = 'plan_types';
    protected $_dependentTables = array('Plan_Type');
}

稍后您可以在PlanRow类中拥有一个函数:

public function type() {
    return $this->findParentRow('Plan_Type');
}

并获取Plan_type行类中所有x类型的计划

public function entries() {
    return $row->findDependentRowset('Plan');
}
相关问题