1
0
mirror of synced 2025-01-20 15:31:40 +03:00

optional fields

This commit is contained in:
gnat 2007-11-27 20:16:16 +00:00
parent 542f9b0d72
commit 536c4fd55a
3 changed files with 45 additions and 11 deletions

View File

@ -58,12 +58,16 @@ class Doctrine_Template_Listener_Timestampable extends Doctrine_Record_Listener
*/ */
public function preInsert(Doctrine_Event $event) public function preInsert(Doctrine_Event $event)
{ {
if(!$this->_options['created']['disabled']) {
$createdName = $this->_options['created']['name']; $createdName = $this->_options['created']['name'];
$updatedName = $this->_options['updated']['name'];
$event->getInvoker()->$createdName = $this->getTimestamp('created'); $event->getInvoker()->$createdName = $this->getTimestamp('created');
}
if(!$this->_options['updated']['disabled']) {
$updatedName = $this->_options['updated']['name'];
$event->getInvoker()->$updatedName = $this->getTimestamp('updated'); $event->getInvoker()->$updatedName = $this->getTimestamp('updated');
} }
}
/** /**
* preUpdate * preUpdate
@ -73,10 +77,11 @@ class Doctrine_Template_Listener_Timestampable extends Doctrine_Record_Listener
*/ */
public function preUpdate(Doctrine_Event $event) public function preUpdate(Doctrine_Event $event)
{ {
if(!$this->_options['updated']['disabled']) {
$updatedName = $this->_options['updated']['name']; $updatedName = $this->_options['updated']['name'];
$event->getInvoker()->$updatedName = $this->getTimestamp('updated'); $event->getInvoker()->$updatedName = $this->getTimestamp('updated');
} }
}
/** /**
* getTimestamp * getTimestamp

View File

@ -42,10 +42,12 @@ class Doctrine_Template_Timestampable extends Doctrine_Template
protected $_options = array('created' => array('name' => 'created_at', protected $_options = array('created' => array('name' => 'created_at',
'type' => 'timestamp', 'type' => 'timestamp',
'format' => 'Y-m-d H:i:s', 'format' => 'Y-m-d H:i:s',
'disabled' => false,
'options' => array()), 'options' => array()),
'updated' => array('name' => 'updated_at', 'updated' => array('name' => 'updated_at',
'type' => 'timestamp', 'type' => 'timestamp',
'format' => 'Y-m-d H:i:s', 'format' => 'Y-m-d H:i:s',
'disabled' => false,
'options' => array())); 'options' => array()));
/** /**
@ -66,9 +68,12 @@ class Doctrine_Template_Timestampable extends Doctrine_Template
*/ */
public function setTableDefinition() public function setTableDefinition()
{ {
if(!$this->_options['created']['disabled']) {
$this->hasColumn($this->_options['created']['name'], $this->_options['created']['type'], null, $this->_options['created']['options']); $this->hasColumn($this->_options['created']['name'], $this->_options['created']['type'], null, $this->_options['created']['options']);
}
if(!$this->_options['updated']['disabled']) {
$this->hasColumn($this->_options['updated']['name'], $this->_options['updated']['type'], null, $this->_options['updated']['options']); $this->hasColumn($this->_options['updated']['name'], $this->_options['updated']['type'], null, $this->_options['updated']['options']);
}
$this->addListener(new Doctrine_Template_Listener_Timestampable($this->_options)); $this->addListener(new Doctrine_Template_Listener_Timestampable($this->_options));
} }
} }

View File

@ -273,10 +273,12 @@ class User extends Doctrine_Record
$this->actAs('Timestampable', array('created' => array('name' => 'created_at', $this->actAs('Timestampable', array('created' => array('name' => 'created_at',
'type' => 'timestamp', 'type' => 'timestamp',
'format' => 'Y-m-d H:i:s', 'format' => 'Y-m-d H:i:s',
'disabled' => false,
'options' => array()), 'options' => array()),
'updated' => array('name' => 'updated_at', 'updated' => array('name' => 'updated_at',
'type' => 'timestamp', 'type' => 'timestamp',
'format' => 'Y-m-d H:i:s', 'format' => 'Y-m-d H:i:s',
'disabled' => false,
'options' => array()))); 'options' => array())));
} }
} }
@ -305,6 +307,28 @@ User:
type: string(255) type: string(255)
</code> </code>
If you are only interested in keeping using only one of the columns, such as a created_at timestamp, but not a an updated_at field, set the flag disabled=>true for either of the fields as in the example below.
YAML Example
<code type="yaml">
---
User:
actAs:
Timestampable:
created:
name: created_at
type: timestamp
format:Y-m-d H:i:s
options: []
updated:
disabled: true
columns:
username:
type: string(125)
password:
type: string(255)
</code>
+++ Sluggable +++ Sluggable
If you do not specify the columns to create the slug from, it will default to just using the toString() method on the model. If you do not specify the columns to create the slug from, it will default to just using the toString() method on the model.