public function addTable(Table $table, $alias = null)
 {
     if ($alias == null) {
         $alias = $table->getTableName();
     }
     $this->_tables[$alias] = $table;
     return $this;
 }
Esempio n. 2
0
 /**
  * 
  * @param string/\ORC\DAO\Table $table
  * @param string $op
  * @return \ORC\DBAL\Common\Common
  */
 protected static function create($table, $op = self::SELECT)
 {
     if (is_string($table)) {
         $table = new Table($table);
     }
     if ($table instanceof Table) {
         //first find the engine
         $engine = self::getEngine($table->getServerName());
         $class_name = "\\ORC\\DBAL\\" . $engine . "\\" . $op;
         return new $class_name($table);
     }
 }
 public function loadTable(Table $table, DataRow $dataRow = null)
 {
     $schema = $table->getSchema();
     $fields = $schema['fields'];
     $obj = new \ORC\MVC\Request\Object();
     foreach ($fields as $field_name => $field) {
         if (!isset($_REQUEST[$field_name])) {
             continue;
         }
         switch ($field['type']) {
             case 'smallint':
             case 'int':
             case 'mediumint':
                 if ($field['unsigned']) {
                     $filter = 'posint';
                 } else {
                     $filter = 'int';
                 }
                 break;
             case 'decimal':
                 $filter = 'numeric';
                 break;
             case 'enum':
                 $filter = array('enum' => $field['value']);
                 break;
             default:
                 $filter = 'safe';
                 break;
         }
         $default_value = null;
         if ($dataRow) {
             if ($dataRow->exists($field_name)) {
                 $default_value = $dataRow->get($field_name);
             }
         }
         $value = $this->applyFilter($_REQUEST[$field_name], $filter, $default_value);
         $this->set($field_name, $obj->{$field_name} = $value);
     }
     return $obj;
 }
 protected function getFieldName($key)
 {
     return $this->_table->getFilterName($key);
 }
 /**
  * 保存数据(根据主键自动选择更新或者插入)
  * @param array $data
  * @throws SystemException
  * @return Ambigous <\ORC\DBAL\Common\int/string, boolean>
  */
 public function save(array $data)
 {
     $table = new Table($this->table_name);
     $pk = $table->getPrimaryKey();
     if ($pk) {
         $pks = explode(',', $pk);
     } else {
         $pks = array();
     }
     $is_new = true;
     $pk_values = array();
     $values = array();
     foreach ($data as $key => $value) {
         if (in_array($key, $pks)) {
             //说明有主键值,是更新
             $is_new = false;
             $pk_values[$key] = $value;
         } else {
             $values[$key] = $value;
         }
     }
     if ($is_new == false) {
         if (count($pks) != count($pk_values)) {
             throw new SystemException('主键数量不匹配');
         }
         //如果是指定pk值但是是新建
         $is_update = false;
         $data = $this->getAll();
         //比较pk vlaue
         foreach ($data as $row) {
             $found = true;
             foreach ($pk_values as $k => $v) {
                 if ($row->get($k) != $v) {
                     $found = false;
                     break;
                 }
             }
             if ($found) {
                 $is_update = true;
                 break;
             }
         }
         if ($is_update == false) {
             $is_new = true;
             //说明虽然指定了主键值,但是数据库里找不到对应的值,所以仍然是插入
         }
     }
     if ($is_new) {
         $dbal = DBAL::insert($this->table_name);
     } else {
         $dbal = DBAL::update($this->table_name);
         foreach ($pk_values as $key => $value) {
             $dbal->findBy($key, array($value));
         }
     }
     foreach ($values as $key => $value) {
         $dbal->set($key, $value);
     }
     $result = $dbal->execute();
     $this->flushCache();
     return $result;
 }