Skip to content

Commit

Permalink
[Timestampable] Maded 'updated_at' column optional using a parameter. F…
Browse files Browse the repository at this point in the history
…ixes #370
  • Loading branch information
willdurand committed May 29, 2012
1 parent 24746a2 commit 3849529
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
7 changes: 4 additions & 3 deletions generator/lib/behavior/TimestampableBehavior.php
Expand Up @@ -20,8 +20,9 @@ class TimestampableBehavior extends Behavior
{ {
// default parameters value // default parameters value
protected $parameters = array( protected $parameters = array(
'create_column' => 'created_at', 'create_column' => 'created_at',
'update_column' => 'updated_at' 'update_column' => 'updated_at',
'disable_updated_at' => 'false',
); );


/** /**
Expand Down Expand Up @@ -199,6 +200,6 @@ public function firstCreatedFirst()


protected function withUpdatedAt() protected function withUpdatedAt()
{ {
return !$this->getTable()->hasBehavior('versionable'); return 'true' !== $this->getParameter('disable_updated_at');

This comment has been minimized.

Copy link
@jaugustin

jaugustin May 29, 2012

Member

you should use booleanValue()

This comment has been minimized.

Copy link
@willdurand

willdurand May 29, 2012

Author Contributor

what?

} }
} }
33 changes: 32 additions & 1 deletion test/testsuite/generator/behavior/TimestampableBehaviorTest.php
@@ -1,7 +1,6 @@
<?php <?php


/* /*
* $Id$
* This file is part of the Propel package. * This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
Expand All @@ -10,6 +9,10 @@
*/ */


require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php'; require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
require_once dirname(__FILE__) . '/../../../../generator/lib/config/GeneratorConfig.php';
require_once dirname(__FILE__) . '/../../../../generator/lib/model/Behavior.php';
require_once dirname(__FILE__) . '/../../../../generator/lib/behavior/TimestampableBehavior.php';
require_once dirname(__FILE__) . '/../../../../generator/lib/util/PropelQuickBuilder.php';


/** /**
* Tests for TimestampableBehavior class * Tests for TimestampableBehavior class
Expand Down Expand Up @@ -213,4 +216,32 @@ public function testQueryFirstCreatedFirst()
$this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first'); $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first');
} }


public function testDisableUpdatedAt()
{
$schema = <<<EOF
<database name="timestampable_database">
<table name="table_without_updated_at">
<column name="id" type="INTEGER" primaryKey="true" />
<behavior name="timestampable">
<parameter name="disable_updated_at" value="true" />
</behavior>
</table>
</database>
EOF;

$builder = new PropelQuickBuilder();
$builder->setSchema($schema);
$builder->build();

$this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt'));
$this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt'));
$this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt'));
$this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt'));

$obj = new TableWithoutUpdatedAt();
$this->assertNull($obj->getCreatedAt());
$this->assertEquals(1, $obj->save());
$this->assertNotNull($obj->getCreatedAt());
}
} }
Expand Up @@ -133,7 +133,7 @@ public function testBehaviors()
$bookTable = $this->databaseMap->getTableByPhpName('Book'); $bookTable = $this->databaseMap->getTableByPhpName('Book');
$this->assertEquals($bookTable->getBehaviors(), array(), 'getBehaviors() returns an empty array when no behaviors are registered'); $this->assertEquals($bookTable->getBehaviors(), array(), 'getBehaviors() returns an empty array when no behaviors are registered');
$tmap = Propel::getDatabaseMap(Table1Peer::DATABASE_NAME)->getTable(Table1Peer::TABLE_NAME); $tmap = Propel::getDatabaseMap(Table1Peer::DATABASE_NAME)->getTable(Table1Peer::TABLE_NAME);
$expectedBehaviorParams = array('timestampable' => array('create_column' => 'created_on', 'update_column' => 'updated_on')); $expectedBehaviorParams = array('timestampable' => array('create_column' => 'created_on', 'update_column' => 'updated_on', 'disable_updated_at' => 'false'));
$this->assertEquals($tmap->getBehaviors(), $expectedBehaviorParams, 'The map builder creates a getBehaviors() method to retrieve behaviors parameters when behaviors are registered'); $this->assertEquals($tmap->getBehaviors(), $expectedBehaviorParams, 'The map builder creates a getBehaviors() method to retrieve behaviors parameters when behaviors are registered');
} }


Expand Down
2 changes: 1 addition & 1 deletion test/testsuite/generator/model/BehaviorTest.php
Expand Up @@ -92,7 +92,7 @@ public function testXmlToAppData()
$this->assertEquals(1, count($behaviors), 'XmlToAppData ads as many behaviors as there are behaviors tags'); $this->assertEquals(1, count($behaviors), 'XmlToAppData ads as many behaviors as there are behaviors tags');
$behavior = $table->getBehavior('timestampable'); $behavior = $table->getBehavior('timestampable');
$this->assertEquals('table1', $behavior->getTable()->getName(), 'XmlToAppData sets the behavior table correctly'); $this->assertEquals('table1', $behavior->getTable()->getName(), 'XmlToAppData sets the behavior table correctly');
$this->assertEquals(array('create_column' => 'created_on', 'update_column' => 'updated_on'), $behavior->getParameters(), 'XmlToAppData sets the behavior parameters correctly'); $this->assertEquals(array('create_column' => 'created_on', 'update_column' => 'updated_on', 'disable_updated_at' => 'false'), $behavior->getParameters(), 'XmlToAppData sets the behavior parameters correctly');
} }


/** /**
Expand Down

0 comments on commit 3849529

Please sign in to comment.