[英]Zend Db Table Double JOIN
1. plan_payment_type:
id
plan_id
2. plan
id
payment_period
它是plan_payment_type.plan_id
和plan.id
之間的外鍵。 在我的代碼中,我知道$ plan_payment_type_id。
我的任務是選擇payment_period
。 腳步:
plan_id
從plan_payment_type
payment_period from
具有所選plan_id
plan
中選擇plan_id
我該如何在Zend Framework中使用一個查詢呢? 非常感謝你。
以下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);
在一切之前,我會以不同的方式組織它們。 不是plan_types應該具有plan.id。 這是不合邏輯的。 您擁有一個具有plan_types的表,該表具有plan.id和plan.name,並且該關系通過plan.type_id在計划表中。
此依賴性通過以下方式解決:
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');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.