/**
  * 
  * @param Zend_Db_Adapter_Abstract $dbAdapter
  * @param string $tableVersion
  * @param string $defaultVersion
  * @return type
  */
 protected function _getVersion($dbAdapter, $tableVersion, $defaultVersion = 0)
 {
     if ($this->version > 0) {
         return $this->version;
     }
     $query = new EhrlichAndreas_Db_Select($dbAdapter);
     $query->from($tableVersion, array('version' => new EhrlichAndreas_Db_Expr('max(num)')));
     $sql = $query->assemble();
     try {
         $result = $dbAdapter->fetchAll($sql);
         if (isset($result[0]['version'])) {
             $versionDb = $result[0]['version'];
         } else {
             $versionDb = $defaultVersion;
         }
     } catch (Exception $e) {
         $versionDb = $defaultVersion;
         /*
                     echo $e;die();
                     try
                     {
            $result = $dbAdapter->fetchAll($sql);
         
            if (isset($result[0]['version']))
            {
                $versionDb = $result[0]['version'];
            }
            else
            {
                $versionDb = $defaultVersion;
            }
                     }
                     catch (Exception $e)
                     {
            $versionDb = $defaultVersion;
                     }
         * 
         */
     }
     $this->version = $versionDb;
     return $versionDb;
 }
 /**
  * Fetches all result rows as a sequential array.
  *
  * @param EhrlichAndreas_AbstractCms_Abstract_Adapter_Abstract|EhrlichAndreas_Db_Adapter_Abstract|Zend_Db_Adapter_Abstract $adapter
  * @param string $table
  * @param string|array $where
  *            OPTIONAL An SQL WHERE clause.
  * @param string|array $cols
  *            OPTIONAL The columns to select from the joined table.
  * @param string|array $order
  *            OPTIONAL The column(s) and direction to order by.
  * @param string|array $group
  *            OPTIONAL The column(s) to group by.
  * @param string|array $having
  *            OPTIONAL The HAVING condition.
  * @param int $count
  *            OPTIONAL An SQL LIMIT count.
  * @param int $offset
  *            OPTIONAL An SQL LIMIT offset.
  * @param string $returnAsString
  *            Return the computed query as string.
  * @return array The row results per assoc array fetch mode.
  * @throws Exception
  */
 public function fetchAll($adapter, $table, $where = null, $cols = null, $order = null, $group = null, $having = null, $count = null, $offset = null, $returnAsString = false)
 {
     $db = EhrlichAndreas_AbstractCms_Abstract_Model::getConnection($adapter);
     if (!EhrlichAndreas_Util_Object::isInstanceOf($where, 'EhrlichAndreas_Db_Select')) {
         $query = new EhrlichAndreas_Db_Select($db);
         $cols = $cols == null ? '*' : $cols;
         if (!is_array($cols)) {
             $cols = array($cols);
         }
         foreach ($cols as $key => $value) {
             $escape = true;
             if (is_array($value)) {
                 if (array_key_exists('escape', $value) && !is_null($value['escape'])) {
                     $escape = $value['escape'];
                 } elseif (count($value) == 2 && array_key_exists(0, $value) && array_key_exists(1, $value) && !is_null($value[1]) && is_bool($value[1])) {
                     $escape = $value[1];
                 }
                 if (array_key_exists('value', $value)) {
                     $value = $value['value'];
                 } elseif (array_key_exists(0, $value)) {
                     $value = $value[0];
                 } else {
                     $value = array_shift($value);
                 }
             } elseif (is_object($value) && method_exists($value, '__toString')) {
                 $value = $value->__toString();
                 $escape = false;
             }
             $escape = (bool) intval($escape);
             if (!$escape) {
                 $value = new EhrlichAndreas_Db_Expr($value);
             }
             $cols[$key] = $value;
         }
         $query->from($table, $cols);
         if ($where !== null) {
             $where = (array) $where;
             foreach ($where as $key => $value) {
                 $escape = true;
                 if (is_array($value)) {
                     if (array_key_exists('escape', $value) && !is_null($value['escape'])) {
                         $escape = $value['escape'];
                     } elseif (count($value) == 2 && array_key_exists(0, $value) && array_key_exists(1, $value) && !is_null($value[1]) && is_bool($value[1])) {
                         $escape = $value[1];
                     }
                     if (array_key_exists('value', $value)) {
                         $value = $value['value'];
                     } elseif (count($value) == 2 && array_key_exists(0, $value) && array_key_exists(1, $value) && !is_null($value[1]) && is_bool($value[1])) {
                         $value = $value[0];
                     } else {
                         //$value = array_shift($value);
                     }
                 } elseif (is_object($value) && method_exists($value, '__toString')) {
                     $value = $value->__toString();
                     $escape = false;
                 }
                 $escape = (bool) intval($escape);
                 if (!$escape) {
                     $value = new EhrlichAndreas_Db_Expr($value);
                 }
                 if (is_int($key)) {
                     // $value is the full condition
                     $query->where($value);
                 } elseif (!is_array($value)) {
                     // $key is the condition with placeholder,
                     // and $val is quoted into the condition
                     $query->where($db->quoteIdentifier($key) . ' = ?', $value);
                 } else {
                     // $key is the condition with placeholder,
                     // and $val is array quoted into the condition
                     $query->where($db->quoteIdentifier($key) . ' in (?)', $value);
                 }
             }
         }
         if ($order !== null) {
             if (is_string($order)) {
                 $order = explode(',', $order);
             }
             if (!is_array($order)) {
                 $order = array($order);
             }
             foreach ($order as $val) {
                 $query->order($val);
             }
         }
         if ($group !== null) {
             if (is_string($group)) {
                 $group = explode(',', $group);
             }
             if (!is_array($group)) {
                 $group = array($group);
             }
             foreach ($group as $val) {
                 $query->group($val);
             }
         }
         if ($having !== null) {
             if (!is_array($having)) {
                 $having = array($having);
             }
             foreach ($having as $val) {
                 $query->having($val);
             }
         }
         if ($count !== null || $offset !== null) {
             $query->limit($count, $offset);
         }
     } else {
         $query = $where;
     }
     $query = $query->assemble();
     if ($returnAsString) {
         return $query;
     }
     $fetchMode = $db->getFetchMode();
     $stmt = $db->query($query);
     $return = $stmt->fetchAll($fetchMode);
     $stmt->closeCursor();
     // $return = $db->fetchAll($query);
     return $return;
 }