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_Link extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037 protected $_attributesTable;
00038 protected function _construct()
00039 {
00040 $this->_init('catalog/product_link', 'link_id');
00041 $this->_attributesTable = $this->getTable('catalog/product_link_attribute');
00042 }
00043
00044 public function saveProductLinks($product, $data, $typeId)
00045 {
00046 if (!is_array($data)) {
00047 $data = array();
00048 }
00049 $attributes = $this->getAttributesByType($typeId);
00050 $deleteCondition = $this->_getWriteAdapter()->quoteInto('product_id=?', $product->getId())
00051 . $this->_getWriteAdapter()->quoteInto(' AND link_type_id=?', $typeId);
00052
00053 $this->_getWriteAdapter()->delete($this->getMainTable(), $deleteCondition);
00054
00055 foreach ($data as $linkedProductId => $linkInfo) {
00056 $this->_getWriteAdapter()->insert($this->getMainTable(), array(
00057 'product_id' => $product->getId(),
00058 'linked_product_id' => $linkedProductId,
00059 'link_type_id' => $typeId
00060 ));
00061 $linkId = $this->_getWriteAdapter()->lastInsertId();
00062 foreach ($attributes as $attributeInfo) {
00063 $attributeTable = $this->getAttributeTypeTable($attributeInfo['type']);
00064 if ($attributeTable && isset($linkInfo[$attributeInfo['code']])) {
00065 $this->_getWriteAdapter()->insert($attributeTable, array(
00066 'product_link_attribute_id' => $attributeInfo['id'],
00067 'link_id' => $linkId,
00068 'value' => $linkInfo[$attributeInfo['code']]
00069 ));
00070 }
00071 }
00072 }
00073 return $this;
00074 }
00075
00076 public function getAttributesByType($typeId)
00077 {
00078 $select = $this->_getReadAdapter()->select()
00079 ->from($this->_attributesTable, array(
00080 'id' => 'product_link_attribute_id',
00081 'code' => 'product_link_attribute_code',
00082 'type' => 'data_type'
00083 ))
00084 ->where('link_type_id=?', $typeId);
00085 return $this->_getReadAdapter()->fetchAll($select);
00086 }
00087
00088 public function getAttributeTypeTable($type)
00089 {
00090 return $this->getTable('catalog/product_link_attribute_'.$type);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 public function getChildrenIds($parentId, $typeId)
00104 {
00105 $childrenIds = array();
00106 $select = $this->_getReadAdapter()->select()
00107 ->from(array('l' => $this->getMainTable()), array('product_id', 'linked_product_id'))
00108 ->where('product_id=?', $parentId)
00109 ->where('link_type_id=?', $typeId);
00110 if ($typeId == Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED) {
00111 $select->join(
00112 array('e' => $this->getTable('catalog/product')),
00113 'e.entity_id=l.linked_product_id AND e.required_options=0',
00114 array()
00115 );
00116 }
00117
00118 $childrenIds[$typeId] = array();
00119 foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
00120 $childrenIds[$typeId][$row['linked_product_id']] = $row['linked_product_id'];
00121 }
00122
00123 return $childrenIds;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 public function getParentIdsByChild($childId, $typeId)
00134 {
00135 $parentIds = array();
00136
00137 $select = $this->_getReadAdapter()->select()
00138 ->from($this->getMainTable(), array('product_id', 'linked_product_id'))
00139 ->where('linked_product_id IN(?)', $childId)
00140 ->where('link_type_id=?', $typeId);
00141 foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
00142 $parentIds[] = $row['product_id'];
00143 }
00144
00145 return $parentIds;
00146 }
00147 }