Witam,
szukałem odpowiedzi w necie ale nic sensownego nie znalazłem, więc może tutaj ktoś spotkał się z podobnym problemem i zna rozwiązanie. Na początek trochę kodu
class Model_DbTable_ProductTable extends Zend_Db_Table_Abstract {
protected $_name = 'products';
protected
$_dependentTables = array('Model_DbTable_ProductCommentTable');}
class Model_DbTable_CommentTable extends Zend_Db_Table_Abstract {
protected $_name = 'comments';
protected
$_dependentTables = array('Model_DbTable_ProductCommentTable');}
class Model_DbTable_ProductCommentTable extends Zend_Db_Table_Abstract {
protected $_name = 'products_comments';
protected
$_referenceMap = array( 'columns' => 'product_id',
'refTableClass' => 'Model_DbTable_ProductTable',
'refColumns' => 'product_id'
),
'columns' => 'comment_id',
'refTableClass' => 'Model_DbTable_CommentTable',
'refColumns' => 'comment_id'
)
);
}
mapper
class Model_Mapper_ProductMapper {
public function __construct() {
$this->_dbTable = new Model_DbTable_ProductTable();
}
public function getProducts() {
return $this->_dbTable->fetchAll(
$this->_dbTable -> select()
-> from($this->_dbTable)
-> where('product_status = ?', 1)
);
}
public function getComments() {
$products = $this->getProducts();
foreach($products as $k => $product) {
$rows[$k] = $this->_rowSet -> findManyToManyRowset(
'Model_DbTable_CommentTable',
'Model_DbTable_CommentProductTable'
);
}
}
}
jak widać w metodzie Model_Mapper_ProductMapper:: getComments() dla każdego produktu pobierane są komentarze. Jeżeli wyświetlam 20 produktów to dodatkowo muszę zrobić 20 zapytań... obłęd. Stąd moje pytanie jak elegancko pobrać produkty i komentarze w jednym zapytaniu? Oczywiście join() w select'cie odpada.
Ten post edytował CuteOne 22.07.2012, 04:47:43