public function generateClassFromTable(Doctrine_Table $table) { $definition = array(); $definition['columns'] = $table->getColumns(); $definition['tableName'] = $table->getTableName(); $definition['actAs'] = $table->getTemplates(); $definition['generate_once'] = true; $generatedclass = $this->generateClass($definition); Doctrine::loadModels(sfConfig::get('sf_lib_dir') . '/model/doctrine/opCommunityTopicPlugin/base/'); return $generatedclass; }
public function parseTerm($term) { $negation = false; if (strpos($term, "'") === false) { $where = $this->parseWord($term); } else { $term = trim($term, "' "); $terms = $this->_tokenizer->quoteExplode($term); $where = $this->parseWord($terms[0]); foreach ($terms as $k => $word) { if ($k === 0) { continue; } $where .= ' AND (position + ' . $k . ') = (SELECT position FROM ' . $this->_table->getTableName() . ' WHERE ' . $this->parseWord($word) . ')'; } } return $where; }
/** * Inserts a table row with specified data. * * @param Doctrine_Table $table The table to insert data into. * @param array $values An associative array containing column-value pairs. * Values can be strings or Doctrine_Expression instances. * @return integer the number of affected rows. Boolean false if empty value array was given, */ public function insert(Doctrine_Table $table, array $fields) { $tableName = $table->getTableName(); // column names are specified as array keys $cols = array(); // the query VALUES will contain either expresions (eg 'NOW()') or ? $a = array(); foreach ($fields as $fieldName => $value) { $cols[] = $this->quoteIdentifier($table->getColumnName($fieldName)); if ($value instanceof Doctrine_Expression) { $a[] = $value->getSql(); unset($fields[$fieldName]); } else { $a[] = '?'; } } // build the statement $query = 'INSERT INTO ' . $this->quoteIdentifier($tableName) . ' (' . implode(', ', $cols) . ')' . ' VALUES (' . implode(', ', $a) . ')'; return $this->exec($query, array_values($fields)); }
/** * Generates a string representation of a table. * * This method returns an html dump of a table, containing component name * and table physical name. * @param Doctrine_Table $table * @return string */ public static function getTableAsString(Doctrine_Table $table) { $r[] = "<pre>"; $r[] = "Component : " . $table->getComponentName(); $r[] = "Table : " . $table->getTableName(); $r[] = "</pre>"; return implode("\n", $r) . "<br>"; }
/** * Inserts a table row with specified data. * * @param Doctrine_Table $table The table to insert data into. * @param array $values An associative array containing column-value pairs. * Values can be strings or Doctrine_Expression instances. * @return integer the number of affected rows. Boolean false if empty value array was given, */ public function insert(Doctrine_Table $table, array $fields) { $tableName = $table->getTableName(); // column names are specified as array keys $cols = array(); // the query VALUES will contain either expresions (eg 'NOW()') or ? $a = array(); foreach ($fields as $fieldName => $value) { if ($table->isIdentifier($fieldName) && $table->isIdentifierAutoincrement() && $value == null) { // Autoincrement fields should not be added to the insert statement // if their value is null unset($fields[$fieldName]); continue; } $cols[] = $this->quoteIdentifier($table->getColumnName($fieldName)); if ($value instanceof Doctrine_Expression) { $a[] = $value->getSql(); unset($fields[$fieldName]); } else { $a[] = '?'; } } if (count($fields) == 0) { // Real fix #1786 and #2327 (default values when table is just 'id' as PK) return $this->exec('INSERT INTO ' . $this->quoteIdentifier($tableName) . ' ' . ' VALUES (DEFAULT)'); } // build the statement $query = 'INSERT INTO ' . $this->quoteIdentifier($tableName) . ' (' . implode(', ', $cols) . ')' . ' VALUES (' . implode(', ', $a) . ')'; return $this->exec($query, array_values($fields)); }
/** * Generate a Doctrine_Record from a populated Doctrine_Table instance * * @param Doctrine_Table $table * @return void */ public function generateClassFromTable(Doctrine_Table $table) { $definition = array(); $definition['columns'] = $table->getColumns(); $definition['tableName'] = $table->getTableName(); $definition['actAs'] = $table->getTemplates(); return $this->generateClass($definition); }
/** * Returns the prefix to use when indexing an object from the supplied table. * * @param Doctrine_Table $table * @return string */ protected function _getRowKeyPrefix(Doctrine_Table $table) { return sprintf('(%s) ', $table->getTableName()); }
/** * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT * query, except that if there is already a row in the table with the same * key field values, the REPLACE query just updates its values instead of * inserting a new row. * * The REPLACE type of query does not make part of the SQL standards. Since * practically only MySQL implements it natively, this type of query is * emulated through this method for other DBMS using standard types of * queries inside a transaction to assure the atomicity of the operation. * * @access public * * @param string $table name of the table on which the REPLACE query will * be executed. * @param array $fields associative array that describes the fields and the * values that will be inserted or updated in the specified table. The * indexes of the array are the names of all the fields of the table. The * values of the array are also associative arrays that describe the * values and other properties of the table fields. * * Here follows a list of field properties that need to be specified: * * value: * Value to be assigned to the specified field. This value may be * of specified in database independent type format as this * function can perform the necessary datatype conversions. * * Default: * this property is required unless the Null property * is set to 1. * * type * Name of the type of the field. Currently, all types Metabase * are supported except for clob and blob. * * Default: no type conversion * * null * Boolean property that indicates that the value for this field * should be set to null. * * The default value for fields missing in INSERT queries may be * specified the definition of a table. Often, the default value * is already null, but since the REPLACE may be emulated using * an UPDATE query, make sure that all fields of the table are * listed in this function argument array. * * Default: 0 * * key * Boolean property that indicates that this field should be * handled as a primary key or at least as part of the compound * unique index of the table that will determine the row that will * updated if it exists or inserted a new row otherwise. * * This function will fail if no key field is specified or if the * value of a key field is set to null because fields that are * part of unique index they may not be null. * * Default: 0 * * @return integer the number of affected rows */ public function replace(Doctrine_Table $table, array $fields, array $keys) { if (empty($keys)) { throw new Doctrine_Connection_Exception('Not specified which fields are keys'); } $columns = array(); $values = array(); $params = array(); foreach ($fields as $fieldName => $value) { $columns[] = $table->getColumnName($fieldName); $values[] = '?'; $params[] = $value; } $query = 'REPLACE INTO ' . $this->quoteIdentifier($table->getTableName()) . ' (' . implode(',', $columns) . ') VALUES (' . implode(',', $values) . ')'; return $this->exec($query, $params); }
/** * Inserts a table row with specified data. * * @param Doctrine_Table $table The table to insert data into. * @param array $values An associative array containing column-value pairs. * Values can be strings or Doctrine_Expression instances. * @return integer the number of affected rows. Boolean false if empty value array was given, */ public function insert(Doctrine_Table $table, array $fields) { $identifiers = $table->getIdentifierColumnNames(); $settingNullIdentifier = false; $fields = array_change_key_case($fields); foreach ($identifiers as $identifier) { $lcIdentifier = strtolower($identifier); if (array_key_exists($lcIdentifier, $fields)) { if (is_null($fields[$lcIdentifier])) { $settingNullIdentifier = true; unset($fields[$lcIdentifier]); } } } // MSSQL won't allow the setting of identifier columns to null, so insert a default record and then update it if ($settingNullIdentifier) { $count = $this->exec('INSERT INTO ' . $this->quoteIdentifier($table->getTableName()) . ' DEFAULT VALUES'); if (!$count) { return $count; } $id = $this->lastInsertId($table->getTableName()); return $this->update($table, $fields, array($id)); } return parent::insert($table, $fields); }
/** * Sets Doctrine table. * * @param Doctrine_Table $table Doctrine_Table object. * * @return bool true on success, false otherwise. */ public function setDoctrineTable(Doctrine_Table $table) { $this->_doctrineTable = $table; $this->_table = $table->getTableName(); $this->_column = $this->getDoctrineTableColumns($table); return true; }
/** * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT * query, except that if there is already a row in the table with the same * key field values, the REPLACE query just updates its values instead of * inserting a new row. * * The REPLACE type of query does not make part of the SQL standards. Since * practically only MySQL implements it natively, this type of query is * emulated through this method for other DBMS using standard types of * queries inside a transaction to assure the atomicity of the operation. * * @access public * * @param string $table name of the table on which the REPLACE query will * be executed. * @param array $fields associative array that describes the fields and the * values that will be inserted or updated in the specified table. The * indexes of the array are the names of all the fields of the table. The * values of the array are also associative arrays that describe the * values and other properties of the table fields. * * Here follows a list of field properties that need to be specified: * * value: * Value to be assigned to the specified field. This value may be * of specified in database independent type format as this * function can perform the necessary datatype conversions. * * Default: * this property is required unless the Null property * is set to 1. * * type * Name of the type of the field. Currently, all types Metabase * are supported except for clob and blob. * * Default: no type conversion * * null * Boolean property that indicates that the value for this field * should be set to null. * * The default value for fields missing in INSERT queries may be * specified the definition of a table. Often, the default value * is already null, but since the REPLACE may be emulated using * an UPDATE query, make sure that all fields of the table are * listed in this function argument array. * * Default: 0 * * key * Boolean property that indicates that this field should be * handled as a primary key or at least as part of the compound * unique index of the table that will determine the row that will * updated if it exists or inserted a new row otherwise. * * This function will fail if no key field is specified or if the * value of a key field is set to null because fields that are * part of unique index they may not be null. * * Default: 0 * * @return integer the number of affected rows */ public function replace(Doctrine_Table $table, array $fields, array $keys) { $count = count($fields); $query = $values = ''; $keys = $colnum = 0; for (reset($fields); $colnum < $count; next($fields), $colnum++) { $name = key($fields); if ($colnum > 0) { $query .= ','; $values .= ','; } $query .= $table->getColumnName($name); if (isset($fields[$name]['null']) && $fields[$name]['null']) { $value = 'NULL'; } else { $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null; $value = $this->quote($fields[$name]['value'], $type); } $values .= $value; if (isset($fields[$name]['key']) && $fields[$name]['key']) { if ($value === 'NULL') { throw new Doctrine_Connection_Mysql_Exception('key value ' . $name . ' may not be NULL'); } $keys++; } } if ($keys == 0) { throw new Doctrine_Connection_Mysql_Exception('not specified which fields are keys'); } $query = 'REPLACE INTO ' . $table->getTableName() . ' (' . $query . ') VALUES (' . $values . ')'; return $this->exec($query); }