Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id$
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Template_Timestampable
24
 *
25
 * Easily add created and updated at timestamps to your doctrine records that are automatically set
26
 * when records are saved
27
 *
28
 * @package     Doctrine
29
 * @subpackage  Template
30
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
31
 * @link        www.doctrine-project.org
32
 * @since       1.0
33
 * @version     $Revision$
34
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
35
 */
36
class Doctrine_Template_Timestampable extends Doctrine_Template
37
{
38
    /**
39
     * Array of Timestampable options
40
     *
41
     * @var string
42
     */
43
    protected $_options = array('created' =>  array('name'          =>  'created_at',
44
                                                    'alias'         =>  null,
45
                                                    'type'          =>  'timestamp',
46
                                                    'format'        =>  'Y-m-d H:i:s',
47
                                                    'disabled'      =>  false,
48
                                                    'expression'    =>  false,
49
                                                    'options'       =>  array('notnull' => true)),
50
                                'updated' =>  array('name'          =>  'updated_at',
51
                                                    'alias'         =>  null,
52
                                                    'type'          =>  'timestamp',
53
                                                    'format'        =>  'Y-m-d H:i:s',
54
                                                    'disabled'      =>  false,
55
                                                    'expression'    =>  false,
56
                                                    'onInsert'      =>  true,
57
                                                    'options'       =>  array('notnull' => true)));
58
 
59
    /**
60
     * Set table definition for Timestampable behavior
61
     *
62
     * @return void
63
     */
64
    public function setTableDefinition()
65
    {
66
        if ( ! $this->_options['created']['disabled']) {
67
            $name = $this->_options['created']['name'];
68
            if ($this->_options['created']['alias']) {
69
                $name .= ' as ' . $this->_options['created']['alias'];
70
            }
71
            $this->hasColumn($name, $this->_options['created']['type'], null, $this->_options['created']['options']);
72
        }
73
 
74
        if ( ! $this->_options['updated']['disabled']) {
75
            $name = $this->_options['updated']['name'];
76
            if ($this->_options['updated']['alias']) {
77
                $name .= ' as ' . $this->_options['updated']['alias'];
78
            }
79
            $this->hasColumn($name, $this->_options['updated']['type'], null, $this->_options['updated']['options']);
80
        }
81
 
82
        $this->addListener(new Doctrine_Template_Listener_Timestampable($this->_options));
83
    }
84
}