Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Port PR #356 from Propel 1.6 #217

Merged
merged 2 commits into from

3 participants

@chmeliuk

issue #215

Denys Chmeliuk added some commits
@travisbot

This pull request passes (merged 380b1db into 302e854).

@chmeliuk

@willdurand feel free to close #216 without merging.. it's presented in this PR.

@willdurand willdurand merged commit c009eda into propelorm:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 11, 2012
  1. pre-defined key type for object builder.

    Denys Chmeliuk authored
Commits on May 12, 2012
  1. propelorm/Propel#356 to Propel2 #215

    Denys Chmeliuk authored
This page is out of date. Refresh to see the latest.
View
34 src/Propel/Generator/Builder/Om/ObjectBuilder.php
@@ -52,6 +52,17 @@ public function getNamespace()
}
/**
+ * Returns default key type. if not presented in configuration default will be 'TYPE_PHPNAME'
+ * @return string
+ */
+ public function getDefaultKeyType()
+ {
+ $defaultKeyType = $this->getBuildProperty('defaultKeyType') ? $this->getBuildProperty('defaultKeyType') : 'phpName';
+
+ return "TYPE_".strtoupper($defaultKeyType);
+ }
+
+ /**
* Returns the name of the current class being built.
* @return string
*/
@@ -2126,6 +2137,7 @@ protected function addToArray(&$script)
$hasFks = count($fks) > 0 || count($referrers) > 0;
$objectClassName = $this->getUnqualifiedClassName();
$pkGetter = $this->getTable()->hasCompositePrimaryKey() ? 'serialize($this->getPrimaryKey())' : '$this->getPrimaryKey()';
+ $defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Exports the object as an array.
@@ -2135,7 +2147,7 @@ protected function addToArray(&$script)
*
* @param string \$keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
* BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
- * Defaults to BasePeer::TYPE_PHPNAME.
+ * Defaults to BasePeer::$defaultKeyType.
* @param boolean \$includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE.
* @param array \$alreadyDumpedObjects List of objects to skip to avoid recursion";
if ($hasFks) {
@@ -2146,7 +2158,7 @@ protected function addToArray(&$script)
*
* @return array an associative array containing the field names (as keys) and field values
*/
- public function toArray(\$keyType = BasePeer::TYPE_PHPNAME, \$includeLazyLoadColumns = true, \$alreadyDumpedObjects = array()" . ($hasFks ? ", \$includeForeignObjects = false" : '') . ")
+ public function toArray(\$keyType = BasePeer::$defaultKeyType, \$includeLazyLoadColumns = true, \$alreadyDumpedObjects = array()" . ($hasFks ? ", \$includeForeignObjects = false" : '') . ")
{
if (isset(\$alreadyDumpedObjects['$objectClassName'][$pkGetter])) {
return '*RECURSION*';
@@ -2216,6 +2228,7 @@ protected function addGetByName(&$script)
**/
protected function addGetByNameComment(&$script)
{
+ $defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Retrieves a field from the object by name passed in as a string.
@@ -2223,7 +2236,8 @@ protected function addGetByNameComment(&$script)
* @param string \$name name
* @param string \$type The type of fieldname the \$name is of:
* one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
- * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM
+ * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
+ * Defaults to BasePeer::$defaultKeyType.
* @return mixed Value of field.
*/";
}
@@ -2235,8 +2249,9 @@ protected function addGetByNameComment(&$script)
**/
protected function addGetByNameOpen(&$script)
{
+ $defaultKeyType = $this->getDefaultKeyType();
$script .= "
- public function getByName(\$name, \$type = BasePeer::TYPE_PHPNAME)
+ public function getByName(\$name, \$type = BasePeer::$defaultKeyType)
{";
}
@@ -2347,6 +2362,7 @@ protected function addGetByPositionClose(&$script)
protected function addSetByName(&$script)
{
+ $defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Sets a field from the object by name passed in as a string.
@@ -2355,10 +2371,11 @@ protected function addSetByName(&$script)
* @param mixed \$value field value
* @param string \$type The type of fieldname the \$name is of:
* one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
- * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM
+ * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
+ * Defaults to BasePeer::$defaultKeyType.
* @return void
*/
- public function setByName(\$name, \$value, \$type = BasePeer::TYPE_PHPNAME)
+ public function setByName(\$name, \$value, \$type = BasePeer::$defaultKeyType)
{
\$pos = ".$this->getPeerClassName()."::translateFieldName(\$name, \$type, BasePeer::TYPE_NUM);
@@ -2416,6 +2433,7 @@ public function setByPosition(\$pos, \$value)
protected function addFromArray(&$script)
{
+ $defaultKeyType = $this->getDefaultKeyType();
$table = $this->getTable();
$script .= "
/**
@@ -2429,13 +2447,13 @@ protected function addFromArray(&$script)
* You can specify the key type of the array by additionally passing one
* of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
* BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
- * The default key type is the column's phpname (e.g. 'AuthorId')
+ * The default key type is the column's BasePeer::$defaultKeyType.
*
* @param array \$arr An array to populate the object from.
* @param string \$keyType The type of keys the array uses.
* @return void
*/
- public function fromArray(\$arr, \$keyType = BasePeer::TYPE_PHPNAME)
+ public function fromArray(\$arr, \$keyType = BasePeer::$defaultKeyType)
{
\$keys = ".$this->getPeerClassName()."::getFieldNames(\$keyType);
";
View
3  src/Propel/Generator/Builder/Om/QueryBuilder.php
@@ -974,6 +974,7 @@ public function filterBy$relationName($objectName, \$comparison = null)
if (!$fk->isComposite()) {
$localColumnConstant = $this->getColumnConstant($fk->getLocalColumn());
$foreignColumnName = $fk->getForeignColumn()->getPhpName();
+ $keyColumn = $fk->getForeignTable()->hasCompositePrimaryKey() ? $foreignColumnName : 'PrimaryKey';
$script .= "
} elseif ($objectName instanceof Collection) {
if (null === \$comparison) {
@@ -981,7 +982,7 @@ public function filterBy$relationName($objectName, \$comparison = null)
}
return \$this
- ->addUsingAlias($localColumnConstant, {$objectName}->toKeyValue('PrimaryKey', '$foreignColumnName'), \$comparison);";
+ ->addUsingAlias($localColumnConstant, {$objectName}->toKeyValue('$keyColumn', '$foreignColumnName'), \$comparison);";
}
$script .= "
} else {";
View
15 tests/Fixtures/bookstore/schema.xml
@@ -350,4 +350,19 @@
<column name="name" type="VARCHAR" />
</table>
+ <table name="record_label">
+ <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true" />
+ <column name="abbr" type="VARCHAR" size="5" primaryKey="true" required="true"/>
+ <column name="name" type="VARCHAR" />
+ </table>
+
+ <table name="release_pool">
+ <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true" />
+ <column name="record_label_id" type="INTEGER" required="true"/>
+ <column name="name" type="VARCHAR" />
+ <foreign-key foreignTable="record_label" onDelete="cascade">
+ <reference local="record_label_id" foreign="id" />
+ </foreign-key>
+ </table>
+
</database>
View
28 tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectTest.php
@@ -49,8 +49,12 @@
use Propel\Runtime\Query\Criteria;
use Propel\Runtime\Util\BasePeer;
+use Propel\Generator\Util\QuickBuilder;
+
use \DateTime;
+use MyNameSpace\TestKeyTypeTable;
+
/**
* Tests the generated Object classes.
*
@@ -749,6 +753,30 @@ public function testToArrayKeyType()
$this->assertEquals('Don Juan', $arr1[BookPeer::TITLE], 'toArray() returns an associative array representation of the object');
}
+ public function testToArrayKeyTypePreDefined()
+ {
+ $schema = <<<EOF
+<database name="test" namespace="MyNameSpace">
+ <table name="test_key_type_table">
+ <column name="id_key_type" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
+ <column name="name_key_type" type="VARCHAR" />
+ </table>
+</database>
+EOF;
+
+ $builder = new QuickBuilder();
+ $builder->setSchema($schema);
+ $builder->getConfig()->setBuildProperty('defaultKeyType', 'studlyPhpName');
+ $builder->buildClasses();
+
+ $expectedKeys = array(
+ 'idKeyType',
+ 'nameKeyType',
+ );
+ $object = new TestKeyTypeTable();
+ $this->assertEquals($expectedKeys, array_keys($object->toArray()), 'toArray() returns an associative array with pre-defined key type in properties.');
+ }
+
/**
* Test that setting the auto-increment primary key will result in exception.
*/
View
4 tests/Propel/Tests/Generator/Builder/Om/ObjectBuilderTest.php
@@ -64,6 +64,10 @@ public function testGetDefaultValueString($column, $value)
$this->assertEquals($value, $this->builder->getDefaultValueString($column));
}
+ public function testGetDefaultKeyType()
+ {
+ $this->assertEquals('TYPE_PHPNAME', $this->builder->getDefaultKeyType());
+ }
}
View
30 tests/Propel/Tests/Generator/Builder/Om/QueryBuilderTest.php
@@ -31,6 +31,11 @@
use Propel\Tests\Bookstore\ReaderFavoriteQuery;
use Propel\Tests\Bookstore\PublisherPeer;
+use Propel\Tests\Bookstore\RecordLabelPeer;
+use Propel\Tests\Bookstore\RecordLabelQuery;
+use Propel\Tests\Bookstore\ReleasePoolPeer;
+use Propel\Tests\Bookstore\ReleasePoolQuery;
+
use Propel\Runtime\Propel;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Query\Criteria;
@@ -714,6 +719,31 @@ public function testFilterByRefFkSimpleKey()
$this->assertEquals($q1, $q, 'filterByRefFk() accepts an optional comparison operator');
}
+ public function testFilterByRelationNameCompositePk()
+ {
+ BookstoreDataPopulator::depopulate();
+ BookstoreDataPopulator::populate();
+
+ $testLabel = RecordLabelQuery::create()
+ ->limit(2)
+ ->find($this->con);
+
+ $testRelease = ReleasePoolQuery::create()
+ ->addJoin(ReleasePoolPeer::RECORD_LABEL_ID, RecordLabelPeer::ID)
+ ->filterByRecordLabel($testLabel)
+ ->find($this->con);
+ $q1 = $this->con->getLastExecutedQuery();
+
+ $releasePool = ReleasePoolQuery::create()
+ ->addJoin(ReleasePoolPeer::RECORD_LABEL_ID, RecordLabelPeer::ID)
+ ->add(ReleasePoolPeer::RECORD_LABEL_ID, $testLabel->toKeyValue('Id', 'Id'), Criteria::IN)
+ ->find($this->con);
+ $q2 = $this->con->getLastExecutedQuery();
+
+ $this->assertEquals($q2, $q1, 'filterBy{RelationName}() only accepts arguments of type {RelationName} or PropelCollection');
+ $this->assertEquals($releasePool, $testRelease);
+ }
+
public function testFilterByRefFkCompositeKey()
{
BookstoreDataPopulator::depopulate();
View
36 tests/Propel/Tests/Helpers/Bookstore/BookstoreDataPopulator.php
@@ -42,6 +42,9 @@
use Propel\Tests\Bookstore\Review;
use Propel\Tests\Bookstore\ReviewPeer;
+use Propel\Tests\Bookstore\RecordLabel;
+use Propel\Tests\Bookstore\ReleasePool;
+
define('_LOB_SAMPLE_FILE_PATH', __DIR__ . '/../../../../Fixtures/etc/lob');
/**
@@ -230,6 +233,37 @@ static public function populate($con = null)
$summary->setSummary("Harry Potter does some amazing magic!");
$summary->save();
+ // Add release_pool and record_label
+ $acuna = new RecordLabel();
+ $acuna->setAbbr('acuna');
+ $acuna->setName('Acunadeep');
+ $acuna->save();
+
+ $fade = new RecordLabel();
+ $fade->setAbbr('fade');
+ $fade->setName('Fade Records');
+ $fade->save();
+
+ $pool = new ReleasePool();
+ $pool->setName('D.Chmelyuk - Revert Me Back');
+ $pool->setRecordLabel($acuna);
+ $pool->save();
+
+ $pool = new ReleasePool();
+ $pool->setName('VIF & Lola Palmer - Dreamer');
+ $pool->setRecordLabel($acuna);
+ $pool->save();
+
+ $pool = new ReleasePool();
+ $pool->setName('Lola Palmer - Do You Belong To Me');
+ $pool->setRecordLabel($acuna);
+ $pool->save();
+
+ $pool = new ReleasePool();
+ $pool->setName('Chris Forties - Despegue (foem.info Runners Up Remixes)');
+ $pool->setRecordLabel($fade);
+ $pool->save();
+
$con->commit();
}
@@ -278,6 +312,8 @@ static public function depopulate($con = null)
'\Propel\Tests\Bookstore\ReaderFavoritePeer',
'\Propel\Tests\Bookstore\ReviewPeer',
'\Propel\Tests\Bookstore\BookSummaryPeer',
+ '\Propel\Tests\Bookstore\RecordLabelPeer',
+ '\Propel\Tests\Bookstore\ReleasePoolPeer',
);
// free the memory from existing objects
foreach ($peerClasses as $peerClass) {
View
2  tools/generator/default.properties
@@ -101,6 +101,8 @@ propel.defaultTimeStampFormat =
propel.defaultTimeFormat =
propel.defaultDateFormat =
+propel.defaultKeyType = phpName
+
propel.namespace.om = Base
propel.namespace.map = Map
propel.namespace.autoPackage = true
Something went wrong with that request. Please try again.