Zend framework $\u referenceMap与Zend\u Db\u Table\u Abstract的关系创建了太多查询
这是我第一次在应用程序中使用Zend框架,我不知道我是否完全掌握了各种模型 我有四张桌子:购物车、产品、产品单元、经销商。 购物车有一个购物车id、产品id、单位id和地区id(购物车使用相应的id连接到其他表上) 在Zend之前,我会创建这样一个类:Zend framework $\u referenceMap与Zend\u Db\u Table\u Abstract的关系创建了太多查询,zend-framework,zend-db,zend-db-table,Zend Framework,Zend Db,Zend Db Table,这是我第一次在应用程序中使用Zend框架,我不知道我是否完全掌握了各种模型 我有四张桌子:购物车、产品、产品单元、经销商。 购物车有一个购物车id、产品id、单位id和地区id(购物车使用相应的id连接到其他表上) 在Zend之前,我会创建这样一个类: class ShoppingCart { function getItems() { $sql ="select * from shopping_cart, product, product_unit, distributor where s
class ShoppingCart
{
function getItems()
{
$sql ="select * from shopping_cart, product, product_unit, distributor
where
shopping_cart.product_id = product.id AND
shopping_cart.unit_id = product_unit.id AND
shopping_cart.dist_id = distributor.id AND
cart_id = xxx";
$items = $this->db->getAll($sql);
}
一个查询可以从连接的表中获取所有信息
在Zend_Db_Table_Abstract中设置关系映射时:
我的购物车模型:
class Application_Model_ShoppingCart
{
function __construct()
{
$this->ShoppingCartTable = new Application_Model_DbTable_ShoppingCart();
}
function getItems()
{
$cart_items = $this->ShoppingCartTable->getItems($this->GetCartId());
return $cart_items;
}
}
类应用程序\模型\数据库\购物车扩展了Zend \数据库\表\抽象
{
}
在我的控制器中:
$this->_shoppingCartModel = new Application_Model_ShoppingCart();
$items = $this->_shoppingCartModel->getItems();
我认为:
foreach($this->items AS $item)
{
$item_product = $item->findParentRow('Application_Model_DbTable_Product');
$item_dist = $item->findParentRow('Application_Model_DbTable_Distributor');
$item_unit = $item->findParentRow('Application_Model_DbTable_ProductUnit');
}
当我的购物车中有10个项目时,db profiler会显示超过60个查询(WHOA)以查看购物车项目(显示所有四个表中的信息-产品名称、单元描述、分销商名称)。
对于每种商品,它都会查询购物车,然后查询产品表、产品单位、分销商表
有没有办法让这个查询作为一个查询运行,通过Zend_Db_Table_抽象关系连接所有表?我必须在my Application\u Model\u ShoppingCart类中重新使用db适配器吗 我想抽象对表模型(Application_Model_DbTable_ShoppingCart)的所有数据访问,而不是将Application_Model_ShoppingCart绑定到db处理程序
提前感谢您的建议,我喜欢Zend框架,但是由于人们谈论使用模型的方式不同,模型对我来说仍然很难理解。简言之,不,不幸的是,不可能在一个查询中获得一组表行及其关系 只是所有处理关系的方法都是为行定义的,而不是为表定义的 但至少,您可以使用
Zend\u Db\u Table\u Select
来生成sql,而不是全部手动编写
Upd:我认为,您获取购物车的代码应该属于表(DbTable\u ShoppingCart
)。因此,您在开始时提供的代码可以转换为以下内容:
class Application_Model_DbTable_ShoppingCart extends Zend_Db_Table_Abstract {
public function getItem($cart_id) {
$select = $this->select()
->from( array('sc' => 'shopping_cart'), array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('p' => 'product'), 'sp.product_id = p.id', array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('pu' => 'product_unit'), 'sp.unit_id = pu.id', array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('d' => 'distributor'), 'sp.dist_id = d.id', array(Zend_Db_Select::SQL_WILDCARD) )
->where('sp.cart_id = ?', $cart_id)
->setIntegrityCheck(false);
return $this->fetchAll($select);
}
}
简而言之,不,很遗憾,在一个查询中不可能获得一组表行及其关系 只是所有处理关系的方法都是为行定义的,而不是为表定义的 但至少,您可以使用
Zend\u Db\u Table\u Select
来生成sql,而不是全部手动编写
Upd:我认为,您获取购物车的代码应该属于表(DbTable\u ShoppingCart
)。因此,您在开始时提供的代码可以转换为以下内容:
class Application_Model_DbTable_ShoppingCart extends Zend_Db_Table_Abstract {
public function getItem($cart_id) {
$select = $this->select()
->from( array('sc' => 'shopping_cart'), array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('p' => 'product'), 'sp.product_id = p.id', array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('pu' => 'product_unit'), 'sp.unit_id = pu.id', array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('d' => 'distributor'), 'sp.dist_id = d.id', array(Zend_Db_Select::SQL_WILDCARD) )
->where('sp.cart_id = ?', $cart_id)
->setIntegrityCheck(false);
return $this->fetchAll($select);
}
}
维卡:谢谢你的快速回复。。。如果我使用Zend table select。。在模型类应用程序\模型\购物车类或应用程序\模型\购物车\数据库表中执行自定义联接更好吗?Vika感谢您的快速响应。。。如果我使用Zend table select。。在模型类应用程序\模型\购物车类或应用程序\模型\购物车\数据库表中执行自定义联接更好吗?