这是我当前的架构 - 部分显示。
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; ?>
在表格中显示产品名称和价格的最优雅方式是什么?
答案 0 :(得分:1)
您正在寻找的是加入。这就是我通常会这样做的。
$select = $cart->select()
->from(array('c' => 'cart'))
->join(array('p' => 'product', 'p.productid = c.productid')
->where('username = ?', $username);
$fetch = $cart->fetchAll($select);
然后,您可以像购买数量一样从$ cart变量中获取数据。