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.phpdoctrine.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_AuditLog
|
24 |
*
|
25 |
* @package Doctrine
|
26 |
* @subpackage AuditLog
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
28 |
* @link www.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision$
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
32 |
*/
|
33 |
class Doctrine_AuditLog extends Doctrine_Plugin
|
34 |
{
|
35 |
protected $_options = array(
|
36 |
'className' => '%CLASS%Version',
|
37 |
'versionColumn' => 'version',
|
38 |
'generateFiles' => false,
|
39 |
'table' => false,
|
40 |
'pluginTable' => false,
|
41 |
);
|
42 |
|
43 |
public function __construct($options)
|
44 |
{
|
45 |
$this->_options = array_merge($this->_options, $options);
|
46 |
}
|
47 |
|
48 |
public function getVersion(Doctrine_Record $record, $version)
|
49 |
{
|
50 |
$className = $this->_options['className'];
|
51 |
|
52 |
$q = new Doctrine_Query();
|
53 |
|
54 |
$values = array();
|
55 |
foreach ((array) $this->_options['table']->getIdentifier() as $id) {
|
56 |
$conditions[] = $className . '.' . $id . ' = ?';
|
57 |
$values[] = $record->get($id);
|
58 |
}
|
59 |
$where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';
|
60 |
|
61 |
$values[] = $version;
|
62 |
|
63 |
$q->from($className)
|
64 |
->where($where);
|
65 |
|
66 |
return $q->execute($values, Doctrine::HYDRATE_ARRAY);
|
67 |
}
|
68 |
public function buildDefinition(Doctrine_Table $table)
|
69 |
{
|
70 |
$this->_options['className'] = str_replace('%CLASS%',
|
71 |
$this->_options['table']->getComponentName(),
|
72 |
$this->_options['className']);
|
73 |
|
74 |
$name = $table->getComponentName();
|
75 |
|
76 |
$className = $name . 'Version';
|
77 |
|
78 |
// check that class doesn't exist (otherwise we cannot create it)
|
79 |
if (class_exists($className)) {
|
80 |
return false;
|
81 |
}
|
82 |
|
83 |
$columns = $table->getColumns();
|
84 |
|
85 |
// remove all sequence, autoincrement and unique constraint definitions
|
86 |
foreach ($columns as $column => $definition) {
|
87 |
unset($columns[$column]['autoincrement']);
|
88 |
unset($columns[$column]['sequence']);
|
89 |
unset($columns[$column]['unique']);
|
90 |
}
|
91 |
|
92 |
// the version column should be part of the primary key definition
|
93 |
$columns[$this->_options['versionColumn']] = array('type' => 'integer',
|
94 |
'length' => 8,
|
95 |
'primary' => true);
|
96 |
|
97 |
$id = $table->getIdentifier();
|
98 |
|
99 |
$options = array('className' => $className);
|
100 |
|
101 |
$relations = array($name => array('local' => $id,
|
102 |
'foreign' => $id,
|
103 |
'onDelete' => 'CASCADE',
|
104 |
'onUpdate' => 'CASCADE'));
|
105 |
|
106 |
$this->generateClass($options, $columns, array());
|
107 |
|
108 |
$this->_options['pluginTable'] = $table->getConnection()->getTable($this->_options['className']);
|
109 |
|
110 |
return true;
|
111 |
}
|
112 |
}
|