00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Compare_Item extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038
00039
00040
00041 protected function _construct()
00042 {
00043 $this->_init('catalog/compare_item', 'catalog_compare_item_id');
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053 public function loadByProduct(Mage_Catalog_Model_Product_Compare_Item $object, $product)
00054 {
00055 $read = $this->_getReadAdapter();
00056 if ($product instanceof Mage_Catalog_Model_Product) {
00057 $productId = $product->getId();
00058 }
00059 else {
00060 $productId = (int) $product;
00061 }
00062
00063 $select = $read->select()->from($this->getMainTable())
00064 ->where('product_id=?', $productId)
00065 ->where('visitor_id=?', $object->getVisitorId());
00066 if ($object->getCustomerId()) {
00067 $select->where('customer_id=?', $object->getCustomerId());
00068 }
00069
00070 $data = $read->fetchRow($select);
00071
00072 if (!$data) {
00073 return false;
00074 }
00075
00076 $object->setData($data);
00077
00078 $this->_afterLoad($object);
00079 return true;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089 public function getCount($customerId, $visitorId)
00090 {
00091 $select = $this->_getReadAdapter()->select()->from($this->getMainTable(), 'COUNT(*)')
00092 ->where('visitor_id=?', $visitorId);
00093 if ($customerId) {
00094 $select->where('customer_id=?', $customerId);
00095 }
00096 return $this->_getReadAdapter()->fetchOne($select);
00097 }
00098
00099
00100
00101
00102
00103
00104
00105 public function clean(Mage_Catalog_Model_Product_Compare_Item $object)
00106 {
00107 while (true) {
00108 $select = $this->_getReadAdapter()->select()
00109 ->from(array('compare_table' => $this->getMainTable()), array('catalog_compare_item_id'))
00110 ->joinLeft(
00111 array('visitor_table' => $this->getTable('log/visitor')),
00112 '`visitor_table`.`visitor_id`=`compare_table`.`visitor_id`',
00113 array())
00114 ->where('compare_table.visitor_id>?',0)
00115 ->where('`visitor_table`.`visitor_id` IS NULL')
00116 ->limit(1000);
00117 $itemIds = $this->_getReadAdapter()->fetchCol($select);
00118
00119 if (!$itemIds) {
00120 break;
00121 }
00122
00123 $this->_getWriteAdapter()->delete(
00124 $this->getMainTable(),
00125 $this->_getWriteAdapter()->quoteInto('catalog_compare_item_id IN(?)', $itemIds)
00126 );
00127 }
00128
00129 return $this;
00130 }
00131 }