1

这是我当前的模式 - 部分显示。

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; ?>

在表格中显示产品名称和价格的最优雅的方式是什么?

4

1 回答 1

1

您正在寻找的是加入。这就是我通常会这样做的方式。

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

然后,您可以像使用数量一样从 $cart 变量中获取数据。

于 2012-08-07T19:55:30.613 回答