Example #1
0
 /**
  * Create a WHERE clause for use with Zend_Db_Select
  *
  * @param array $query WHERE options
  * @param string $separator AND/OR
  * @param bool $useJointView Wether to use the *_joint view or the table.
  * @return string WHERE clause
  */
 protected function _createWhereClause(array $query, $separator = 'AND', $useJointView = true)
 {
     $where = array();
     $adapter = $this->_model->getAdapter();
     $nativeColumns = $this->_model->info(Zend_Db_Table_Abstract::COLS);
     if ($useJointView) {
         $tableName = $this->_getTableName($this->_model);
         $mockTable = new Zend_Db_Table(array(Zend_Db_Table_Abstract::NAME => $tableName, Zend_Db_Table_Abstract::PRIMARY => $this->_model->info(Zend_Db_Table_Abstract::PRIMARY)));
         $nativeColumns = $mockTable->info(Zend_Db_Table_Abstract::COLS);
     } else {
         $tableName = $this->_model->getName();
     }
     // change native columns to lowercase
     // because when columnName is configured in camelcase in the model config
     // it causes problems when checking if refColumn is a native column
     $nativeColumns = array_map(function ($column) {
         return strtolower($column);
     }, $nativeColumns);
     foreach ($query as $column => $value) {
         if (strtoupper($column) === 'OR' && is_array($value)) {
             $where[] = $this->_createWhereClause($value, 'OR');
         } elseif (is_array($value)) {
             $where[] = $adapter->quoteInto($adapter->quoteIdentifier($tableName) . '.' . $column . ' IN(?)', $value);
         } elseif (is_null($value)) {
             if (substr($column, -2) == '<>') {
                 $column = preg_replace('/<>$/', '', $column);
                 $where[] = $column . ' IS NOT NULL';
             } else {
                 $where[] = $column . ' IS NULL';
             }
         } elseif (is_scalar($value)) {
             // Use $refColumn to see if this column is native to the current
             // model.
             $refColumn = null;
             if (!preg_match('/(>=?|<=?|like|<>)/i', $column, $matches)) {
                 $refColumn = $column;
                 $column = $adapter->quoteIdentifier($column) . ' =';
             } else {
                 // explode column so the actual column name can be quoted
                 $parts = explode(' ', $column);
                 $refColumn = $parts[0];
                 $column = $adapter->quoteIdentifier($parts[0]) . ' ' . $parts[1];
             }
             if (strpos($refColumn, '.') === false && in_array($refColumn, $nativeColumns)) {
                 $column = $adapter->quoteIdentifier($tableName) . '.' . $column;
             }
             $where[] = $adapter->quoteInto($column . ' ?', $value);
         }
     }
     return '(' . implode(" {$separator} ", $where) . ')';
 }
Example #2
0
 /**
  * Rollback all inserts when the import throws an error halfway
  * @param Garp_Model $model
  * @param Array $primaryKeys Collection of primary keys
  * @return Void
  */
 public function rollback(Garp_Model $model, array $primaryKeys)
 {
     if (empty($primaryKeys)) {
         return;
     }
     $primaryCols = (array) $model->info(Zend_Db_Table::PRIMARY);
     $where = array();
     foreach ($primaryKeys as $pk) {
         $recordWhere = array();
         foreach ((array) $pk as $i => $key) {
             $recordWhere[] = $model->getAdapter()->quoteIdentifier(current($primaryCols)) . ' = ' . $model->getAdapter()->quote($key);
         }
         $recordWhere = implode(' AND ', $recordWhere);
         $recordWhere = '(' . $recordWhere . ')';
         $where[] = $recordWhere;
         reset($primaryCols);
     }
     $where = implode(' OR ', $where);
     if (empty($where)) {
         return;
     }
     $model->delete($where);
 }
Example #3
0
 /**
  * Return the current weight of a set of records.
  * Note that only the first record found will be used. Working with multiple records
  * (which is possible using Zend's update() functionality) is not implemented.
  * @param Garp_Model $model
  * @param String $where
  * @param Array $modelRelationConfig 
  * @return Int
  */
 public function findCurrentWeight(Garp_Model $model, $where, $foreignKey, array $modelRelationConfig)
 {
     $foreignKeyColumn = $model->getAdapter()->quoteIdentifier($modelRelationConfig[self::FOREIGN_KEY_COLUMN_KEY]);
     $weightColumn = $modelRelationConfig[self::WEIGHT_COLUMN_KEY];
     $select = $model->select()->from($model->getName(), array('weight' => $weightColumn))->where($foreignKeyColumn . ' = ?', $foreignKey);
     $where = (array) $where;
     foreach ($where as $w) {
         $select->where($w);
     }
     $result = $model->fetchRow($select);
     if ($result && $result->weight) {
         return $result->weight;
     }
     return 0;
 }