public function testColumnMappings()
 {
     $contestTable = $this->databaseMap->getTableByPhpName('Propel\\Tests\\BookstoreSchemas\\BookstoreContest');
     $del = $this->getPlatform()->getSchemaDelimiter();
     $this->assertEquals(array('contest' . $del . 'bookstore_contest.BOOKSTORE_ID' => 'bookstore_schemas' . $del . 'bookstore.ID'), $contestTable->getRelation('Bookstore')->getColumnMappings(), 'getColumnMappings returns local to foreign by default');
     $this->assertEquals(array('contest' . $del . 'bookstore_contest.BOOKSTORE_ID' => 'bookstore_schemas' . $del . 'bookstore.ID'), $contestTable->getRelation('Bookstore')->getColumnMappings(RelationMap::LEFT_TO_RIGHT), 'getColumnMappings returns local to foreign when asked left to right for a many to one relationship');
     $bookTable = $this->databaseMap->getTableByPhpName('Propel\\Tests\\BookstoreSchemas\\Bookstore');
     $this->assertEquals(array('contest' . $del . 'bookstore_contest.BOOKSTORE_ID' => 'bookstore_schemas' . $del . 'bookstore.ID'), $bookTable->getRelation('BookstoreContest')->getColumnMappings(), 'getColumnMappings returns local to foreign by default');
     $this->assertEquals(array('bookstore_schemas' . $del . 'bookstore.ID' => 'contest' . $del . 'bookstore_contest.BOOKSTORE_ID'), $bookTable->getRelation('BookstoreContest')->getColumnMappings(RelationMap::LEFT_TO_RIGHT), 'getColumnMappings returns foreign to local when asked left to right for a one to many relationship');
     $bookCustomerTable = $this->databaseMap->getTableByPhpName('Propel\\Tests\\BookstoreSchemas\\Customer');
     $this->assertEquals(array('bookstore_schemas' . $del . 'customer_account.CUSTOMER_ID' => 'bookstore_schemas' . $del . 'customer.ID'), $bookCustomerTable->getRelation('CustomerAccount')->getColumnMappings(), 'getColumnMappings returns local to foreign by default');
     $this->assertEquals(array('bookstore_schemas' . $del . 'customer.ID' => 'bookstore_schemas' . $del . 'customer_account.CUSTOMER_ID'), $bookCustomerTable->getRelation('CustomerAccount')->getColumnMappings(RelationMap::LEFT_TO_RIGHT), 'getColumnMappings returns foreign to local when asked left to right for a one to one relationship');
 }
Example #2
0
 /**
  * Adds a RelationMap to the table
  *
  * @param  string                          $name          The relation name
  * @param  string                          $tablePhpName  The related table name
  * @param  integer                         $type          The relation type (either RelationMap::MANY_TO_ONE, RelationMap::ONE_TO_MANY, or RelationMAp::ONE_TO_ONE)
  * @param  array                           $columnMapping An associative array mapping column names (local => foreign)
  * @param  string                          $onDelete      SQL behavior upon deletion ('SET NULL', 'CASCADE', ...)
  * @param  string                          $onUpdate      SQL behavior upon update ('SET NULL', 'CASCADE', ...)
  * @param  string                          $pluralName    Optional plural name for *_TO_MANY relationships
  * @return \Propel\Runtime\Map\RelationMap the built RelationMap object
  */
 public function addRelation($name, $tablePhpName, $type, $columnMapping = array(), $onDelete = null, $onUpdate = null, $pluralName = null)
 {
     // note: using phpName for the second table allows the use of DatabaseMap::getTableByPhpName()
     // and this method autoloads the TableMap if the table isn't loaded yet
     $relation = new RelationMap($name);
     $relation->setType($type);
     $relation->setOnUpdate($onUpdate);
     $relation->setOnDelete($onDelete);
     if (null !== $pluralName) {
         $relation->setPluralName($pluralName);
     }
     // set tables
     if (RelationMap::MANY_TO_ONE === $type) {
         $relation->setLocalTable($this);
         $relation->setForeignTable($this->dbMap->getTableByPhpName($tablePhpName));
     } else {
         $relation->setLocalTable($this->dbMap->getTableByPhpName($tablePhpName));
         $relation->setForeignTable($this);
         $columnMapping = array_flip($columnMapping);
     }
     // set columns
     foreach ($columnMapping as $local => $foreign) {
         $relation->addColumnMapping($relation->getLocalTable()->getColumn($local), $relation->getForeignTable()->getColumn($foreign));
     }
     $this->relations[$name] = $relation;
     return $relation;
 }
Example #3
0
 /**
  * Adds a RelationMap to the table
  *
  * @param  string                          $name          The relation name
  * @param  string                          $tablePhpName  The related table name
  * @param  integer                         $type          The relation type (either RelationMap::MANY_TO_ONE, RelationMap::ONE_TO_MANY, or RelationMAp::ONE_TO_ONE)
  * @param  array                           $joinConditionMapping Arrays in array defining a normalize join condition [[':foreign_id', ':id', '='], [':foreign_type', 'value', '=']]
  * @param  string                          $onDelete      SQL behavior upon deletion ('SET NULL', 'CASCADE', ...)
  * @param  string                          $onUpdate      SQL behavior upon update ('SET NULL', 'CASCADE', ...)
  * @param  string                          $pluralName    Optional plural name for *_TO_MANY relationships
  * @param  boolean                         $polymorphic    Optional plural name for *_TO_MANY relationships
  *
  * @return RelationMap the built RelationMap object
  */
 public function addRelation($name, $tablePhpName, $type, $joinConditionMapping = [], $onDelete = null, $onUpdate = null, $pluralName = null, $polymorphic = false)
 {
     // note: using phpName for the second table allows the use of DatabaseMap::getTableByPhpName()
     // and this method autoloads the TableMap if the table isn't loaded yet
     $relation = new RelationMap($name);
     $relation->setType($type);
     $relation->setOnUpdate($onUpdate);
     $relation->setOnDelete($onDelete);
     $relation->setPolymorphic($polymorphic);
     if (null !== $pluralName) {
         $relation->setPluralName($pluralName);
     }
     // set tables
     if (RelationMap::MANY_TO_ONE === $type) {
         $relation->setLocalTable($this);
         $relation->setForeignTable($this->dbMap->getTableByPhpName($tablePhpName));
     } else {
         $relation->setLocalTable($this->dbMap->getTableByPhpName($tablePhpName));
         $relation->setForeignTable($this);
     }
     // set columns
     foreach ($joinConditionMapping as $map) {
         list($local, $foreign) = $map;
         $relation->addColumnMapping($this->getColumnOrValue($local, $relation->getLocalTable()), $this->getColumnOrValue($foreign, $relation->getForeignTable()));
     }
     $this->relations[$name] = $relation;
     return $relation;
 }
Example #4
0
 /**
  * Load Map builders.
  *
  * @param string $connectionName A connection name.
  */
 protected function loadMapBuilders($connectionName = null)
 {
     if (null !== $this->dbMap) {
         return;
     }
     $this->dbMap = Propel::getDatabaseMap($connectionName);
     if (0 === count($this->dbMap->getTables())) {
         $finder = new Finder();
         $files = $finder->files()->name('*TableMap.php')->in($this->getModelSearchPaths($connectionName))->notName('TableMap.php')->exclude('PropelBundle')->exclude('Tests');
         foreach ($files as $file) {
             $class = $this->guessFullClassName($file->getRelativePath(), basename($file, '.php'));
             if (null !== $class && $this->isInDatabase($class, $connectionName)) {
                 $this->dbMap->addTableFromMapClass($class);
             }
         }
     }
 }
Example #5
0
 /**
  * @see parent::cleanupSQL()
  *
  * @param string      $sql
  * @param array       $params
  * @param Criteria    $values
  * @param DatabaseMap $dbMap
  */
 public function cleanupSQL(&$sql, array &$params, Criteria $values, DatabaseMap $dbMap)
 {
     $i = 1;
     foreach ($params as $param) {
         $tableName = $param['table'];
         $columnName = $param['column'];
         $value = $param['value'];
         // this is to workaround for a bug with pdo_sqlsrv inserting or updating blobs with null values
         // http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/5a755bdd-41e9-45cb-9166-c9da4475bb94
         if (null !== $tableName) {
             $cMap = $dbMap->getTable($tableName)->getColumn($columnName);
             if (null === $value && $cMap->isLob()) {
                 $sql = str_replace(":p{$i}", "CONVERT(VARBINARY(MAX), :p{$i})", $sql);
             }
         }
         $i++;
     }
 }
Example #6
0
 /**
  * Binds values in a prepared statement.
  *
  * This method is designed to work with the Criteria::createSelectSql() method, which creates
  * both the SELECT SQL statement and populates a passed-in array of parameter
  * values that should be substituted.
  *
  * <code>
  * $adapter = Propel::getServiceContainer()->getAdapter($criteria->getDbName());
  * $sql = $criteria->createSelectSql($params);
  * $stmt = $con->prepare($sql);
  * $params = array();
  * $adapter->populateStmtValues($stmt, $params, Propel::getServiceContainer()->getDatabaseMap($critera->getDbName()));
  * $stmt->execute();
  * </code>
  *
  * @param StatementInterface $stmt
  * @param array              $params array('column' => ..., 'table' => ..., 'value' => ...)
  * @param DatabaseMap        $dbMap
  */
 public function bindValues(StatementInterface $stmt, array $params, DatabaseMap $dbMap)
 {
     $position = 0;
     foreach ($params as $param) {
         $position++;
         $parameter = ':p' . $position;
         $value = $param['value'];
         if (null === $value) {
             $stmt->bindValue($parameter, null, \PDO::PARAM_NULL);
             continue;
         }
         $tableName = $param['table'];
         if (null === $tableName) {
             $type = isset($param['type']) ? $param['type'] : \PDO::PARAM_STR;
             $stmt->bindValue($parameter, $value, $type);
             continue;
         }
         $cMap = $dbMap->getTable($tableName)->getColumn($param['column']);
         $this->bindValue($stmt, $parameter, $value, $cMap, $position);
     }
 }
Example #7
0
 /**
  * @see parent::cleanupSQL()
  *
  * @param string      $sql
  * @param array       $params
  * @param Criteria    $values
  * @param DatabaseMap $dbMap
  */
 public function cleanupSQL(&$sql, array &$params, Criteria $values, DatabaseMap $dbMap)
 {
     $i = 1;
     $paramCols = [];
     foreach ($params as $param) {
         if (null !== $param['table']) {
             $column = $dbMap->getTable($param['table'])->getColumn($param['column']);
             /* MSSQL pdo_dblib and pdo_mssql blob values must be converted to hex and then the hex added
              * to the query string directly.  If it goes through PDOStatement::bindValue quotes will cause
              * an error with the insert or update.
              */
             if (is_resource($param['value']) && $column->isLob()) {
                 // we always need to make sure that the stream is rewound, otherwise nothing will
                 // get written to database.
                 rewind($param['value']);
                 $hexArr = unpack('H*hex', stream_get_contents($param['value']));
                 $sql = str_replace(":p{$i}", '0x' . $hexArr['hex'], $sql);
                 unset($hexArr);
                 fclose($param['value']);
             } else {
                 $paramCols[] = $param;
             }
         }
         $i++;
     }
     // if we made changes re-number the params
     if ($params != $paramCols) {
         $params = $paramCols;
         unset($paramCols);
         preg_match_all('/:p\\d/', $sql, $matches);
         foreach ($matches[0] as $key => $match) {
             $sql = str_replace($match, ':p' . ($key + 1), $sql);
         }
     }
 }