Saya mencoba membangun koleksi khusus untuk kisi-kisi di modul admin Magento. Saya telah membuat metode pengumpulan baru yang disebut "addAttributeHaving" yang hanya melakukan hal berikut:
public function addAttributeHaving($value)
{
$this->getSelect()->having($value);
return $this;
}
Lihat kode koleksi:
$collection->addFieldToSelect(
array(
'entity_id',
'created_at',
'increment_id',
'customer_email',
'customer_firstname',
'customer_lastname',
'grand_total',
'status'
)
);
$collection->getSelect()->joinLeft(array('sfop' => 'sales_flat_order_payment'), 'main_table.entity_id = sfop.parent_id', 'sfop.amount_authorized');
$collection->getSelect()->columns('sum(sfop.amount_authorized) AS AUTHD');
$collection->getSelect()->columns('grand_total - sum(sfop.amount_authorized) AS DIF_AU');
$collection->addFieldToFilter('main_table.state', array('in' => array('new','payment_review')));
$collection->addFieldToFilter('main_table.sd_order_type', array('neq' => 7));
$collection->addFieldToFilter('sfop.method', array('neq' => 'giftcard'));
$collection->addFieldToFilter('main_table.created_at', array('gt' => $this->getFilterDate()));
$collection->getSelect()->group(array('main_table.entity_id'));
$collection->addAttributeHaving('DIF_AU <> 0');
$collection->load(true,true);
$this->setCollection($collection);
Ini menghasilkan SQL berikut yang dieksekusi dengan sangat baik dan menghasilkan hasil yang diharapkan ketika dijalankan di luar Magento.
[METHOD=Varien_Data_Collection_Db->printLogQuery] SELECT `main_table`.`entity_id`, `main_table`.`entity_id`, `main_table`.`created_at`, `main_table`.`increment_id`, `main_table`.`customer_email`, `main_table`.`customer_firstname`, `main_table`.`customer_lastname`, `main_table`.`grand_total`, `main_table`.`status`, `sfop`.`amount_authorized`, sum(sfop.amount_authorized) AS `AUTHD`, grand_total - sum(sfop.amount_authorized) AS `DIF_AU` FROM `sales_flat_order` AS `main_table` LEFT JOIN `sales_flat_order_payment` AS `sfop` ON main_table.entity_id = sfop.parent_id WHERE (main_table.state in ('new', 'payment_review')) AND (main_table.sd_order_type != 7) AND (sfop.method != 'giftcard') AND (main_table.created_at > '2013-04-07') GROUP BY `main_table`.`entity_id` HAVING (DIF_AU <> 0)
Namun, ketika saya mencoba memuat kotak di dalam Magento saya mendapatkan kesalahan berikut:
SQLSTATE [42S22]: Kolom tidak ditemukan: 1054 Kolom tidak dikenal 'DIF_AU' di 'memiliki klausa'
Selain itu, jika saya menghapus klausa memiliki (yang merusak hasil saya), saya dapat menggunakan kolom DIF_AU untuk sumber data di Grid.
sd_order_type
berasal?