/** * Retrieve Tagged Products * * @param Mage_Tag_Model_Tag_Relation $model * @return array */ public function getProductIds($model) { $select = $this->_getReadAdapter()->select()->from($this->getMainTable(), 'product_id')->where("tag_id=?", $model->getTagId())->where('customer_id=?', $model->getCustomerId())->where('active=1'); if ($model->hasStoreId()) { $select->where('store_id = ?', $model->getStoreId()); } return $this->_getReadAdapter()->fetchCol($select); }
/** * Add TAG to PRODUCT relations * * @param Mage_Tag_Model_Tag_Relation $model * @return Mage_Tag_Model_Resource_Tag_Relation */ public function addRelations($model) { $addedIds = $model->getAddedProductIds(); $bind = array('tag_id' => $model->getTagId(), 'store_id' => $model->getStoreId()); $write = $this->_getWriteAdapter(); $select = $write->select()->from($this->getMainTable(), 'product_id')->where('tag_id = :tag_id')->where('store_id = :store_id'); $oldRelationIds = $write->fetchCol($select, $bind); $insert = array_diff($addedIds, $oldRelationIds); $delete = array_diff($oldRelationIds, $addedIds); if (!empty($insert)) { $insertData = array(); foreach ($insert as $value) { $insertData[] = array('tag_id' => $model->getTagId(), 'store_id' => $model->getStoreId(), 'product_id' => $value, 'customer_id' => $model->getCustomerId(), 'created_at' => $this->formatDate(time())); } $write->insertMultiple($this->getMainTable(), $insertData); } if (!empty($delete)) { $write->delete($this->getMainTable(), array('product_id IN (?)' => $delete, 'store_id = ?' => $model->getStoreId())); } return $this; }