コード例 #1
0
ファイル: mysql.php プロジェクト: tapiau/muyo
 /**
  * WARNING: do not use if don't know internals (partially implemented)
  * Load model from SQL query to an array with joined columns as arrays
  * @param mixed $q
  * @param bool $collection
  * @param int $mode defines how results should be structured
  * @return array
  * @throws Exception
  * @fixme $q and $collection
  */
 public function loadArray($q = null, $collection = false, $mode = self::LOAD_ARRAY_MODE_NESTED_TABLE)
 {
     $alias = $this->getAlias();
     $descriptors = $this->getColumns();
     if (empty($descriptors)) {
         $this->setColumns(array_keys($this->schemaColumnsGet()), $alias);
         $descriptors = $this->getColumns();
     }
     if (!$collection) {
         $key_name = $this->getPrimaryKey();
         $key_idx = array_find_key($descriptors, zend_column_eq_dg($alias, $key_name));
         if (!debug_assert(!is_null($key_idx), "Key '{$key_name}' needs to be set to load hash-set of '{$alias}'")) {
             $this->setColumns([$key_name]);
             $descriptors = $this->getColumns();
             $key_idx = array_find_key($descriptors, zend_column_eq_dg($alias, $key_name));
             debug_enforce(!is_null($key_idx));
         }
     } else {
         $key_idx = null;
     }
     if ($q === null) {
         $q = $this->getSQL();
     }
     $db = $this->getDb();
     try {
         $rows = $db->fetchAll($q, [], Zend_Db::FETCH_NUM);
     } catch (Exception $e) {
         throw new Exception('Error while loading: ' . $e->getMessage() . ' | SQL: ' . $this->getSQL());
     }
     if (self::LOAD_ARRAY_MODE_RAW == $mode) {
         return $rows;
     }
     $ret = [];
     foreach ($rows as $row) {
         $record = [];
         foreach ($row as $idx => $column) {
             $descriptor = $descriptors[$idx];
             $colalias = strval(zend_column_name($descriptor));
             if (self::LOAD_ARRAY_MODE_NESTED_TABLE == $mode) {
                 $tblalias = zend_column_table($descriptor);
                 if (!array_key_exists($tblalias, $record)) {
                     $record[$tblalias] = [];
                 }
                 $record[$tblalias][$colalias] = $column;
             } else {
                 $record[$colalias] = $column;
             }
         }
         if ($collection) {
             $ret[] = $record;
         } else {
             $key = $row[$key_idx];
             $ret[$key] = $record;
         }
     }
     return $ret;
 }
コード例 #2
0
ファイル: zend.php プロジェクト: tapiau/muyo
 /**
  * Returns comparator for zend column $tableAlias AND $columnName
  * @param string|null $tableAlias
  * @param string|null $columnName
  * @return callable $zendColumnExpression => $isMatching
  */
 function zend_column_eq_dg($tableAlias, $columnName)
 {
     return function ($descriptor) use($columnName, $tableAlias) {
         $colname = zend_column_name($descriptor);
         $tblalias = zend_column_table($descriptor);
         return $tblalias === $tableAlias && $colname === $columnName;
     };
 }