2012-08-07 35 views
1

這是我當前的模式 - 部分顯示。Zend Framework - 顯示來自兩個表的數據

CREATE TABLE cart (
username VARCHAR(60), 
productid INT(5), 
quantity INT(6), 

PRIMARY KEY (username, productid), 
FOREIGN KEY (username) REFERENCES login(username), 
FOREIGN KEY(productid) REFERENCES product(productid) 
); 

CREATE TABLE product (
productid INT(5) NOT NULL AUTO_INCREMENT, 
name VARCHAR(30) NOT NULL, 
description VARCHAR(1000), 
price VARCHAR(6) NOT NULL, 
stocks INT(6) NOT NULL, 

PRIMARY KEY (productid) 
); 

我有我的CartController此功能:

public function indexAction() 
{ 
    $auth= Zend_Auth::getInstance(); 
    $user= $auth->getIdentity(); 
    $username = $user->username; 

    $cart = new Application_Model_DbTable_Cart(); 
    $fetch = $cart->fetchAll($cart->select()->where('username = ?', $username)); 
    $this->view->cart = $fetch;  
} 

正由我的index.phtml叫:

<?php foreach($this->cart as $cart) :?> 
<tr> 
<td><?php echo $this->escape($cart['productid']);?></td> 
<td></td> 
<td><?php echo $this->escape($cart['quantity']);?></td> 
<td></td> 
<td>  
    <a href="<?php echo $this->url(array('controller'=>'product', 
     'action'=>'edit', 'id'=>$product->productid));?>">Edit</a> 
</td> 
<td>  
    <a href="<?php echo $this->url(array('controller'=>'product', 
     'action'=>'delete', 'id'=>$product->productid));?>">Delete</a> 
</td> 
</tr> 
<?php endforeach; ?> 

什麼是顯示產品名稱的最優雅的方式和價格在表中?

回答

1

你在找什麼是一個加入。這是我通常會這樣做的。

$select = $cart->select() 
    ->from(array('c' => 'cart')) 
    ->join(array('p' => 'product', 'p.productid = c.productid') 
    ->where('username = ?', $username); 
$fetch = $cart->fetchAll($select); 

然後,您可以像購買數量一樣從$ cart變量中獲取數據。

+0

謝謝,這幾乎工作,但我不得不添加setIntegrityCheck(false)。 – Ayrx 2012-08-08 01:33:39

相關問題