Public Member Functions | |
addFieldsToFilter ($fields) |
Definition at line 27 of file Collection.php.
addFieldsToFilter | ( | $ | fields | ) |
Definition at line 29 of file Collection.php.
00030 { 00031 if ($fields) { 00032 /*$entityIds = null;*/ 00033 $previousSelect = null; 00034 foreach ($fields as $table => $conditions) { 00035 foreach ($conditions as $attributeId => $conditionValue) { 00036 $bindVarName = 'attribute_'.$attributeId; 00037 $select = $this->getConnection()->select(); 00038 $select->from(array('t1' => $table), 'entity_id'); 00039 $conditionData = array(); 00040 00041 if (is_array($conditionValue)) { 00042 if (isset($conditionValue['in'])){ 00043 $conditionData[] = array('IN (?)', $conditionValue['in']); 00044 } 00045 elseif (isset($conditionValue['in_set'])) { 00046 $conditionData[] = array('REGEXP \'(^|,)('.join('|', $conditionValue['in_set']).')(,|$)\'', $conditionValue['in_set']); 00047 } 00048 elseif (isset($conditionValue['like'])) { 00049 $this->addBindParam($bindVarName, $conditionValue['like']); 00050 $conditionData[] = 'LIKE :'.$bindVarName; 00051 } 00052 elseif (isset($conditionValue['from']) && isset($conditionValue['to'])) { 00053 if ($conditionValue['from']) { 00054 if (!is_numeric($conditionValue['from'])){ 00055 $conditionValue['from'] = date("Y-m-d H:i:s", strtotime($conditionValue['from'])); 00056 } 00057 $conditionData[] = array('>= ?', $conditionValue['from']); 00058 } 00059 if ($conditionValue['to']) { 00060 if (!is_numeric($conditionValue['to'])){ 00061 $conditionValue['to'] = date("Y-m-d H:i:s", strtotime($conditionValue['to'])); 00062 } 00063 $conditionData[] = array('<= ?', $conditionValue['to']); 00064 } 00065 } 00066 } else { 00067 $conditionData[] = array('= ?', $conditionValue); 00068 } 00069 00070 if (!is_numeric($attributeId)) { 00071 foreach ($conditionData as $data) { 00072 if (is_array($data)) { 00073 $select->where('t1.'.$attributeId . ' ' . $data[0], $data[1]); 00074 } 00075 else { 00076 $select->where('t1.'.$attributeId . ' ' . $data); 00077 } 00078 } 00079 } 00080 else { 00081 $storeId = $this->getStoreId(); 00082 $select->joinLeft( 00083 array('t2' => $table), 00084 $this->getConnection()->quoteInto('t1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id=?', $storeId), 00085 array() 00086 ); 00087 $select->where('t1.store_id = ?', 0); 00088 $select->where('t1.attribute_id = ?', $attributeId); 00089 00090 foreach ($conditionData as $data) { 00091 if (is_array($data)) { 00092 $select->where('IFNULL(t2.value, t1.value) ' . $data[0], $data[1]); 00093 } 00094 else { 00095 $select->where('IFNULL(t2.value, t1.value) ' . $data); 00096 } 00097 } 00098 } 00099 00100 if (!is_null($previousSelect)) { 00101 $select->where('t1.entity_id IN(?)', new Zend_Db_Expr($previousSelect)); 00102 } 00103 $previousSelect = $select; 00104 } 00105 00106 /*if (!is_null($entityIds) && $entityIds) { 00107 $select->where('t1.entity_id IN(?)', $entityIds); 00108 } 00109 elseif (!is_null($entityIds) && !$entityIds) { 00110 continue; 00111 } 00112 00113 $entityIds = array(); 00114 $rowSet = $this->getConnection()->fetchAll($select); 00115 foreach ($rowSet as $row) { 00116 $entityIds[] = $row['entity_id']; 00117 }*/ 00118 } 00119 00120 /*if ($entityIds) { 00121 $this->addFieldToFilter('entity_id', array('IN', $entityIds)); 00122 } 00123 else { 00124 $this->addFieldToFilter('entity_id', 'IS NULL'); 00125 }*/ 00126 $this->addFieldToFilter('entity_id', array('in' => new Zend_Db_Expr($select))); 00127 } 00128 00129 return $this; 00130 }