Пример #1
0
 public function init()
 {
     // Parent
     $fullClassName = get_class($this);
     $fullClassName = explode("\\", $fullClassName);
     $this->_table = strtolower($fullClassName[count($fullClassName) - 1]);
     parent::init();
     //
     $this->app = \IFR\Main\App::get();
     // PDO
     $config = getConfig(CONF_PATH . '/application.xml', getCurrentEnv());
     $config = $config->db->toArray();
     $this->_db = $this->app->getMysqlDb($config);
     // Build table
     $this->_db->exec("DROP TABLE IF EXISTS {$this->_table};");
     $filePath = sprintf('./resources/sql/%s.sql', $this->_table);
     assertTrue(file_exists($filePath), "SQL file '{$filePath}' does not exist");
     $this->_db->exec(file_get_contents($filePath));
     // Build settings
     $columns = array_merge(self::$DEFAULT_COLUMNS, $this->getColumnsDefinition());
     foreach ($columns as $name => $column) {
         $this->setColumns($name)->schemaColumnSet($name, $column);
     }
 }
Пример #2
0
 /**
  * WARNING: Only columns copying supported.
  * @param Lib_Model_Db_Mysql $model
  * @param string|null $thisKeyCol may contain table prefix or not
  * @param string|null $thatKeyCol may contain table prefix or not
  * @param string $conditions
  * @return $this
  */
 public function joinFrom($model, $thisKeyCol, $thatKeyCol = null, $conditions = '')
 {
     if ($thatKeyCol === null) {
         debug_assert($thisKeyCol !== null);
         $thatKeyCol = $model->getPrimaryKey();
     } elseif ($thisKeyCol === null) {
         debug_assert($thatKeyCol !== null);
         $thisKeyCol = $this->getPrimaryKey();
     }
     $this->prefixColumn($model, $thatKeyCol);
     $this->prefixColumn($this, $thisKeyCol);
     $conditions = str_replace('{that}', $model->getAlias(), $conditions);
     $conditions = str_replace('{this}', $this->getAlias(), $conditions);
     $this->mapPartWhere($this->addAliasToConditionDg());
     $model->mapPartWhere($model->addAliasToConditionDg());
     $thisFrom = $this->_select->getPart(Zend_Db_Select::FROM);
     $modelColumns = array_chain($model->_select->getPart(Zend_Db_Select::COLUMNS), array_group_dg(array_get_dg(return_dg(0))), array_map_val_dg(array_chain_dg(array_map_val_dg(function ($descriptor) {
         return null === $descriptor[2] ? $descriptor[1] : [$descriptor[2] => $descriptor[1]];
     }), function ($columns) {
         $outArray = [];
         array_map_val($columns, function ($column) use(&$outArray) {
             if (is_array($column)) {
                 array_map_val($column, function ($column, $alias) use(&$outArray) {
                     $outArray[$alias] = $column;
                 });
             } else {
                 $outArray[] = $column;
             }
         });
         return $outArray;
     })));
     array_each($model->_select->getPart(Zend_Db_Select::FROM), function ($descriptor, $alias) use($modelColumns, $thisFrom, $model, $thisKeyCol, $thatKeyCol, $conditions) {
         debug_enforce(!array_key_exists($alias, $thisFrom), "Alias `{$alias}` already used for table `{$descriptor['tableName']}`");
         switch ($descriptor['joinType']) {
             case Zend_Db_Select::FROM:
                 $this->_select->joinLeft([$model->getAlias() => $model->getTable()], "{$thisKeyCol}={$thatKeyCol} " . $conditions, array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']);
                 break;
             case Zend_Db_Select::INNER_JOIN:
                 $this->_select->joinInner([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']);
                 break;
             case Zend_Db_Select::LEFT_JOIN:
                 $this->_select->joinLeft([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']);
                 break;
             case Zend_Db_Select::RIGHT_JOIN:
                 $this->_select->joinRight([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']);
                 break;
             case Zend_Db_Select::FULL_JOIN:
                 $this->_select->joinFull([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']);
                 break;
             case Zend_Db_Select::CROSS_JOIN:
                 $this->_select->joinCross([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']);
                 break;
             case Zend_Db_Select::NATURAL_JOIN:
                 $this->_select->joinNatural([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']);
                 break;
             default:
                 debug_assert(false, "Unknown join type " . var_dump_human_compact($descriptor['joinType']));
                 break;
         }
     });
     $this->settingsJoin($model);
     return $this;
 }