/** * Gets the propertly escaped (and quoted) value for a column. * @param ColumnValue $colValue * @return mixed The proper value to be added to the string. */ protected function getColumnValueSql(ColumnValue $colValue) { $column = $colValue->getColumn(); $creoleTypeString = PropelTypes::getCreoleType($column->getPropelType()); $creoleTypeCode = CreoleTypes::getCreoleCode($creoleTypeString); $method = 'get' . CreoleTypes::getAffix($creoleTypeCode) . 'Sql'; return $this->{$method}($colValue->getValue()); }
/** * Populates values in a prepared statement. * * @param PreparedStatement $stmt * @param array $params array('column' => ..., 'table' => ..., 'value' => ...) * @param DatabaseMap $dbMap * @return int The number of params replaced. */ private static function populateStmtValues($stmt, $params, DatabaseMap $dbMap) { $i = 1; foreach ($params as $param) { $tableName = $param['table']; $columnName = $param['column']; $value = $param['value']; if ($value === null) { $stmt->setNull($i++); } else { $cMap = $dbMap->getTable($tableName)->getColumn($columnName); $setter = 'set' . CreoleTypes::getAffix($cMap->getCreoleType()); $stmt->{$setter}($i++, $value); } } // foreach }
/** * Adds the hydrate() method, which sets attributes of the object based on a ResultSet. */ protected function addHydrate(&$script) { $table = $this->getTable(); $script .= "\n\t/**\n\t * Hydrates (populates) the object variables with values from the database resultset.\n\t *\n\t * An offset (1-based \"start column\") is specified so that objects can be hydrated\n\t * with a subset of the columns in the resultset rows. This is needed, for example,\n\t * for results of JOIN queries where the resultset row includes columns from two or\n\t * more tables.\n\t *\n\t * @param ResultSet \$rs The ResultSet class with cursor advanced to desired record pos.\n\t * @param int \$startcol 1-based offset column which indicates which restultset column to start with.\n\t * @return int next starting column\n\t * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.\n\t */\n\tpublic function hydrate(ResultSet \$rs, \$startcol = 1)\n\t{\n\t\ttry {\n"; $n = 0; foreach ($table->getColumns() as $col) { if (!$col->isLazyLoad()) { $affix = CreoleTypes::getAffix(CreoleTypes::getCreoleCode($col->getType())); $clo = strtolower($col->getName()); switch ($col->getType()) { case PropelTypes::DATE: case PropelTypes::TIME: case PropelTypes::TIMESTAMP: $script .= "\n\t\t\t\$this->{$clo} = \$rs->get{$affix}(\$startcol + {$n}, null);\n"; break; default: $script .= "\n\t\t\t\$this->{$clo} = \$rs->get{$affix}(\$startcol + {$n});\n"; } $n++; } // if col->isLazyLoad() } /* foreach */ if ($this->getBuildProperty("addSaveMethod")) { $script .= "\n\t\t\t\$this->resetModified();\n"; } $script .= "\n\t\t\t\$this->setNew(false);\n\n\t\t\t// FIXME - using NUM_COLUMNS may be clearer.\n\t\t\treturn \$startcol + {$n}; // {$n} = " . $this->getPeerClassname() . "::NUM_COLUMNS - " . $this->getPeerClassname() . "::NUM_LAZY_LOAD_COLUMNS).\n\n\t\t} catch (Exception \$e) {\n\t\t\tthrow new PropelException(\"Error populating " . $table->getPhpName() . " object\", \$e);\n\t\t}\n\t}\n"; }
/** * This method refreshes this Record's Value's. It can only be performed on * a Record that has not been modified and has been created with a TableDataSet * and corresponding KeyDef. * * @param Connection $conn * @throws DataSetException * @throws SQLException */ public function refresh(Connection $conn = null) { if ($conn === null) { $conn = $this->ds->connection(); } if ($this->toBeSavedWithDelete()) { return; } elseif ($this->toBeSavedWithInsert()) { throw new DataSetException("There is no way to refresh a record which has been created with addRecord()."); } elseif ($this->ds instanceof QueryDataSet) { throw new DataSetException("You can only perform a refresh on Records created with a TableDataSet."); } $stmt = null; try { $stmt = $conn->prepareStatement($this->getRefreshSql()); $ps = 1; $kd = $this->ds->keydef(); for ($i = 1, $kdsize = $kd->size(); $i <= $kdsize; $i++) { $val = $this->getValue($kd->getAttrib($i)); if ($val == null) { throw new DataSetException("You cannot execute an update with a null value for a KeyDef."); } $setter = 'set' . CreoleTypes::getAffix($table->getColumn($col)->getType()); $stmt->{$setter}($ps++, $val); } $rs = $stmt->executeQuery(); $rs->next(); $this->initializeRecord(); $this->createValues($rs); } catch (SQLException $e) { if ($stmt) { $stmt->close(); } throw $e; } }
public static function doSelectWithTitle(Criteria $c, $culture = null, $include_unpublished_pages = false, $con = null) { $dbMap = Propel::getDatabaseMap($c->getDbName()); if ($con === null) { $con = Propel::getConnection($c->getDbName()); } if ($culture === null) { $culture = sfContext::getInstance()->getUser()->getCulture(); } // Set the correct dbName if it has not been overridden if ($c->getDbName() == Propel::getDefaultDB()) { $c->setDbName(self::DATABASE_NAME); } self::addSelectColumns($c); $startcol = self::NUM_COLUMNS - self::NUM_LAZY_LOAD_COLUMNS + 1; $c->addSelectColumn(sfSimpleCMSSlotPeer::VALUE); if (!$include_unpublished_pages) { $c->add(self::IS_PUBLISHED, true); } // Start of the complicated stuff // ------------------------------ // big hack to have the join operate on three conditions $c->addJoin(sfSimpleCMSSlotPeer::PAGE_ID, sfSimpleCMSPagePeer::ID . ' AND ' . sfSimpleCMSSlotPeer::CULTURE . ' = ? AND ' . sfSimpleCMSSlotPeer::NAME . ' = \'title\'', Criteria::RIGHT_JOIN); // but now we need to populate the statement by hand $params = array(); $sql = BasePeer::createSelectSql($c, $params); array_unshift($params, array('column' => sfSimpleCMSSlotPeer::CULTURE, 'table' => sfSimpleCMSSlotPeer::TABLE_NAME, 'value' => $culture)); $stmt = $con->prepareStatement($sql); $stmt->setLimit($c->getLimit()); $stmt->setOffset($c->getOffset()); $i = 1; foreach ($params as $param) { $tableName = $param['table']; $columnName = $param['column']; $value = $param['value']; if ($value === null) { $stmt->setNull($i++); } else { $cMap = $dbMap->getTable($tableName)->getColumn($columnName); $setter = 'set' . CreoleTypes::getAffix($cMap->getCreoleType()); $stmt->{$setter}($i++, $value); } } $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); // ---------------------------- // End of the complicated stuff /* // The complicated code above is there just to add a join on the three conditions // It could be achieved in a simpler way with these lines $c->addJoin(sfSimpleCMSSlotPeer::PAGE_ID, sfSimpleCMSPagePeer::ID, Criteria::RIGHT_JOIN); $c->add(sfSimpleCMSSlotPeer::CULTURE, $culture); $c->add(sfSimpleCMSSlotPeer::NAME, 'title'); // But then pages with no title would not be visible in menus // So we do it with more code and it's both safe and functional */ $results = array(); while ($rs->next()) { $page = new sfSimpleCMSPage(); $page->hydrate($rs); //$page->setCulture($culture); $page->setTitle($rs->getString($startcol)); $results[] = $page; } return $results; }
/** * populates stmt values (?,?,?) on sql querys * @param PreparedStatement, stmt, the prepared statement. * @param array, fields, the affected fields */ private static function populateStmtValues($stmt, $fields) { $i = 1; foreach ($fields as $field) { if ($field->getValue() === NULL) { $stmt->setNull($i++); } else { $setter = 'set' . CreoleTypes::getAffix(CreoleTypes::getCreoleCode(strtoupper($field->type))); $stmt->{$setter}($i++, $field->getValue()); } } }