/** * <p>Sortierungs-Funktion zur Nutzung mittels * {@link http://de.php.net/manual/de/function.usort.php}.</p> * @param array|ArrayAccess $a * @param array|ArrayAccess $b * @return int <p>-1, wenn das $a vor $b im Array stehen soll, ansonsten 1.</p> */ public function sort($a, $b) { $compare = 0; $orders = $this->_criteria->getOrders(); foreach ($orders as $key => $direction) { $key = $this->_transformAlias($key); if (is_array($a) && is_array($b) || $a instanceof ArrayAccess && $b instanceof ArrayAccess) { $compare = strCaseCmp($a[$key], $b[$key]); } else { $compare = strCaseCmp($a->{$key}, $b->{$key}); } if ($direction == Dkplus_Model_Criteria::ORDER_ASC) { $compare *= -1; } if ($compare != 0) { break; } } return $compare; }
/** * * @param Dkplus_Model_Criteria_Interface $crit * @return Zend_Db_Table_Select */ protected function _critToQuery(Dkplus_Model_Criteria_Interface $crit) { $sel = $this->_getDbTable()->select(); //Where if (count($crit->getWheres()) > 0) { $arrWherePart = array(); $adapter = $this->_getDbTable()->getAdapter(); foreach ($crit->getWheres() as $arrWhere) { //Alias umwandeln if ($this->_hasAlias($arrWhere['col'])) { $arrWhere['col'] = $this->_transformAlias($arrWhere['col']); } //Zusammenfügen, wenn der Haupt-Connektor wieder Auftritt if ($arrWhere['connector'] == $crit->getWhereConnector()) { if ($crit->getWhereConnector() == Dkplus_Model_Criteria::WHERE_OR) { $sel->orWhere(' ( ' . implode(' AND ', $arrWherePart) . ' ) '); } else { $sel->where(' ( ' . implode(' OR ', $arrWherePart) . ' ) '); } $arrWherePart = array(); } //Hinzufügen einer Bedingung if (is_array($arrWhere['value'])) { switch ($arrWhere['type']) { case Dkplus_Model_Criteria::WHERE_IS: $strCrit = ' IN(?)'; break; case Dkplus_Model_Criteria::WHERE_IS_NOT: $strCrit = ' NOT IN(?)'; break; } } else { switch ($arrWhere['type']) { case Dkplus_Model_Criteria::WHERE_IS: if (is_null($arrWhere['value'])) { $strCrit = ' IS NULL'; break; } $strCrit = ' = ?'; break; case Dkplus_Model_Criteria::WHERE_IS_NOT: if (is_null($arrWhere['value'])) { $strCrit = ' IS NOT NULL'; break; } $strCrit = ' != ?'; break; case Dkplus_Model_Criteria::WHERE_LIKE: if (is_null($arrWhere['value'])) { $strCrit = ' IS NULL'; break; } $strCrit = ' LIKE ?'; $arrWhere['value'] = '%' . strToLower($arrWhere['value']) . '%'; break; case Dkplus_Model_Criteria::WHERE_NOT_LIKE: if (is_null($arrWhere['value'])) { $strCrit = ' IS NOT NULL'; break; } $strCrit = ' NOT LIKE ?'; $arrWhere['value'] = '%' . strToLower($arrWhere['value']) . '%'; break; case Dkplus_Model_Criteria::WHERE_BIGGER: $strCrit = ' > ?'; $arrWhere['value'] = strToLower($arrWhere['value']); break; case Dkplus_Model_Criteria::WHERE_SMALLER: $strCrit = ' < ?'; $arrWhere['value'] = strToLower($arrWhere['value']); break; } } $arrWherePart[] = is_null($arrWhere['value']) ? $arrWhere['col'] . $strCrit : $adapter->quoteInto($arrWhere['col'] . $strCrit, $arrWhere['value']); } //Hinzufügen der letzten Bedingung if ($crit->getWhereConnector() == Dkplus_Model_Criteria::WHERE_OR) { $sel->orWhere(' ( ' . implode(' AND ', $arrWherePart) . ' ) '); } else { $sel->where(' ( ' . implode(' OR ', $arrWherePart) . ' ) '); } } //Order-Part der Query: foreach ($crit->getOrders() as $col => $order) { $col = $this->_hasAlias($col) ? $this->_transformAlias($col) : $col; $sel->order($col . ' ' . ($order == Dkplus_Model_Criteria::ORDER_ASC ? 'ASC' : 'DESC')); } //Hinzufügen der Limit-Klausel if (!is_null($crit->getLimitCount())) { $sel->limit($crit->getLimitCount(), $crit->getLimitStart()); } return $sel; }