public function testCanGetConnection() { $config = new OutletConfig(array('connection' => array('dsn' => $this->getSQLiteInMemoryDSN(), 'dialect' => 'sqlite'), 'classes' => array())); $this->assertThat($config->getConnection(), $this->isInstanceOf('OutletConnection')); $config = new OutletConfig(array('connection' => array('pdo' => $this->getSQLiteInMemoryPDOConnection(), 'dialect' => 'sqlite'), 'classes' => array())); $this->assertThat($config->getConnection(), $this->isInstanceOf('OutletConnection')); }
/** * Updates an entity in the database including relationships. * * It is safer to use the save function, it will take into account newness of the entity to determine insert vs update * * @see OutletMapper::save(&$obj) * @param object $obj entity to update */ public function update(&$obj) { // get the class $cls = self::getEntityClass($obj); // this first since this references the key $this->saveManyToOne($obj); if ($mod = $this->getModifiedFields($obj)) { $con = $this->config->getConnection(); $q = "UPDATE {" . $cls . "} \n"; $q .= "SET \n"; $ups = array(); foreach ($this->config->getEntity($cls)->getProperties() as $key => $f) { if (!in_array($key, $mod)) { // skip fields that were not modified continue; } if (@$f[2]['pk']) { // skip primary key continue; } $value = $this->getProp($obj, $key); if (is_null($value)) { $value = 'NULL'; } else { $value = $con->quote(self::toSqlValue($f, $value)); } $ups[] = " {" . $cls . '.' . $key . "} = {$value}"; } $q .= implode(", \n", $ups); $q .= "\nWHERE "; $clause = array(); foreach ($this->config->getEntity($cls)->getProperties() as $key => $pk) { if (!@$pk[2]['pk']) { // if it's not a primary key, skip it continue; } $value = $con->quote(self::toSqlValue($pk, self::getProp($obj, $key))); $clause[] = "{$pk['0']} = {$value}"; } $q .= implode(' AND ', $clause); $q = $this->processQuery($q); $con->exec($q); } // these last since they reference the key $this->saveOneToMany($obj); $this->saveManyToMany($obj); }
/** * Construct a new instance of OutletEntityConfig * @param OutletConfig $config outlet configuration * @param object $entity entity * @param object $conf configuration * @return OutletEntityConfig instance */ function __construct(OutletConfig $config, $entity, array $conf) { $this->config = $config; if (!isset($conf['table'])) { throw new OutletConfigException('Mapping for entity [' . $entity . '] is missing element [table]'); } if (!isset($conf['props'])) { throw new OutletConfigException('Mapping for entity [' . $entity . '] is missing element [props]'); } // i need to leave this for for the outletgen script //if (!class_exists($entity)) throw new OutletConfigException('Class does not exist for mapped entity ['.$entity.']'); // validate that there's a pk foreach ($conf['props'] as $p => $f) { if (@$f[2]['pk']) { $pk = $p; break; } } if (!isset($pk)) { throw new OutletConfigException("Entity [{$entity}] must have at least one column defined as a primary key in the configuration"); } // save basic data $this->table = $conf['table']; $this->clazz = $entity; $this->props = $conf['props']; $this->sequenceName = isset($conf['sequenceName']) ? $conf['sequenceName'] : ''; $this->useGettersAndSetters = isset($conf['useGettersAndSetters']) ? $conf['useGettersAndSetters'] : $config->useGettersAndSetters(); // Adjusts sequence name for postgres if it is not specified if ($config->getConnection()->getDialect() == 'pgsql' && $this->sequenceName == '') { foreach ($this->props as $key => $d) { // Property needs to be primary key and auto increment if (isset($d[2]['pk']) && $d[2]['pk'] && (isset($d[2]['autoIncrement']) && $d[2]['autoIncrement'])) { // default name for sequence = {table}_{column}_seq $this->sequenceName = $this->table . '_' . $d[0] . '_seq'; break; } } } }
/** * Construct a new instance of OutletEntityConfig * @param OutletConfig $config outlet configuration * @param object $entity entity * @param object $conf configuration * @return OutletEntityConfig instance */ function __construct(OutletConfig $config, $entity, array $conf) { $this->config = $config; if (!isset($conf['table'])) { throw new OutletConfigException('Mapping for entity [' . $entity . '] is missing element [table]'); } if (!isset($conf['props'])) { throw new OutletConfigException('Mapping for entity [' . $entity . '] is missing element [props]'); } // i need to leave this for for the outletgen script //if (!class_exists($entity)) throw new OutletConfigException('Class does not exist for mapped entity ['.$entity.']'); // validate that there's a pk foreach ($conf['props'] as $p => $f) { if (@$f[2]['pk']) { $pk = $p; break; } } if (!isset($pk)) { throw new OutletConfigException("Entity [{$entity}] must have at least one column defined as a primary key in the configuration"); } // save basic data $this->table = $conf['table']; $this->clazz = $entity; $this->props = $conf['props']; $this->sequenceName = isset($conf['sequenceName']) ? $conf['sequenceName'] : ''; $this->useGettersAndSetters = isset($conf['useGettersAndSetters']) ? $conf['useGettersAndSetters'] : $config->useGettersAndSetters; // if there's a plural defined at the foreign entity // else use the entity plus an 's' $this->plural = isset($conf['plural']) ? $conf['plural'] : $this->clazz . 's'; // Adjusts sequence name for postgres if it is not specified if ($config->getConnection()->getDialect() == 'pgsql' && $this->sequenceName == '') { foreach ($this->props as $key => $d) { // Property needs to be primary key and auto increment if (isset($d[2]['pk']) && $d[2]['pk'] && (isset($d[2]['autoIncrement']) && $d[2]['autoIncrement'])) { // default name for sequence = {table}_{column}_seq $this->sequenceName = $this->table . '_' . $d[0] . '_seq'; break; } } } // load associations if (isset($conf['associations'])) { foreach ($conf['associations'] as $assoc) { switch ($assoc[0]) { case 'one-to-many': $a = new OutletOneToManyConfig($this->config, $this->clazz, $assoc[1], $assoc[2]); break; case 'many-to-one': $a = new OutletManyToOneConfig($this->config, $this->clazz, $assoc[1], $assoc[2]); break; case 'many-to-many': $a = new OutletManyToManyConfig($this->config, $this->clazz, $assoc[1], $assoc[2]); break; case 'one-to-one': $a = new OutletOneToOneConfig($this->config, $this->clazz, $assoc[1], $assoc[2]); break; default: $a = new OutletAssociationConfig($this->config, $assoc[0], $this->clazz, $assoc[1], $assoc[2]); } $this->associations[] = $a; } } }