示例#1
0
文件: Model.php 项目: abcarroll/DABL
 /**
  * Creates and executes INSERT query string for this object
  * @return int
  */
 protected function insert()
 {
     $conn = static::getConnection();
     $pk = static::$_primaryKey;
     $fields = array();
     $values = array();
     $placeholders = array();
     foreach (static::$_columnNames as &$column) {
         $value = $this->{$column};
         if ($value === null && !$this->isColumnModified($column)) {
             continue;
         }
         $fields[] = $conn->quoteIdentifier($column, true);
         $values[] = $value;
         $placeholders[] = '?';
     }
     $quoted_table = $conn->quoteIdentifier(static::getTableName(), true);
     $query_s = 'INSERT INTO ' . $quoted_table . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $placeholders) . ') ';
     if ($pk && $this->isAutoIncrement() && $conn instanceof DBPostgres) {
         $query_s .= ' RETURNING ' . $conn->quoteIdentifier($pk, true);
     }
     $statement = new QueryStatement($conn);
     $statement->setString($query_s);
     $statement->setParams($values);
     $result = $statement->bindAndExecute();
     $count = $result->rowCount();
     if ($pk && $this->isAutoIncrement()) {
         $id = null;
         if ($conn instanceof DBPostgres) {
             $id = $result->fetchColumn(0);
         } elseif ($conn->isGetIdAfterInsert()) {
             $id = $conn->lastInsertId();
             if (empty($id) && $conn instanceof DBRedshift) {
                 $id = $conn->query('SELECT MAX(' . $conn->quoteIdentifier($pk) . ") FROM {$quoted_table}")->fetchColumn();
             }
         }
         if (null !== $id) {
             $this->{"set{$pk}"}($id);
         }
     }
     $this->resetModified();
     $this->setNew(false);
     $this->insertIntoPool($this);
     return $count;
 }
示例#2
0
 /**
  * @param DABLPDO $conn
  * @return QueryStatement
  */
 function getQueryStatement(DABLPDO $conn = null)
 {
     $statement = new QueryStatement($conn);
     $table = $this->_table;
     $on_clause = $this->_onClause;
     $join_type = $this->_joinType;
     $alias = $this->_alias;
     if ($table instanceof Query) {
         $table_statement = $table->getQuery($conn);
         $table = '(' . $table_statement->getString() . ')';
         $statement->addParams($table_statement->getParams());
         $statement->addIdentifiers($table_statement->getIdentifiers());
     } else {
         $statement->addIdentifier($table);
         $table = QueryStatement::IDENTIFIER;
     }
     if ($alias) {
         $table .= " AS {$alias}";
     }
     if ($this->_isLikePropel) {
         $statement->addIdentifiers(array($this->_leftColumn, $this->_rightColumn));
         $on_clause = QueryStatement::IDENTIFIER . ' = ' . QueryStatement::IDENTIFIER;
     } elseif (null === $on_clause) {
         $on_clause = '1 = 1';
     } elseif ($on_clause instanceof Condition) {
         $on_clause_statement = $on_clause->getQueryStatement();
         $on_clause = $on_clause_statement->getString();
         $statement->addParams($on_clause_statement->getParams());
         $statement->addIdentifiers($on_clause_statement->getIdentifiers());
     }
     if ('' !== $on_clause) {
         $on_clause = "ON ({$on_clause})";
     }
     $statement->setString("{$join_type} {$table} {$on_clause}");
     return $statement;
 }