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.
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.org>.
23
* Listener for SoftDelete behavior which will allow you to turn on the behavior which
24
* sets a delete flag instead of actually deleting the record and all queries automatically
25
* include a check for the deleted flag to exclude deleted records.
28
* @subpackage Template
29
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30
* @link www.phpdoctrine.org
33
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
34
* @author Jonathan H. Wage <jonwage@gmail.com>
36
class Doctrine_Template_Listener_SoftDelete extends Doctrine_Record_Listener
39
* Array of SoftDelete options
43
protected $_options = array();
48
* @param string $options
51
public function __construct(array $options)
53
$this->_options = $options;
57
* Skip the normal delete options so we can override it with our own
59
* @param Doctrine_Event $event
62
public function preDelete(Doctrine_Event $event)
64
$event->skipOperation();
68
* Implement postDelete() hook and set the deleted flag to true
70
* @param Doctrine_Event $event
73
public function postDelete(Doctrine_Event $event)
75
$name = $this->_options['name'];
76
$event->getInvoker()->$name = true;
77
$event->getInvoker()->save();
81
* Implement preDqlDelete() hook and modify a dql delete query so it updates the deleted flag
82
* instead of deleting the record
84
* @param Doctrine_Event $event
87
public function preDqlDelete(Doctrine_Event $event)
89
$params = $event->getParams();
90
$field = $params['alias'] . '.' . $this->_options['name'];
91
$query = $event->getQuery();
92
if ( ! $query->contains($field)) {
93
$query->from('')->update($params['component']['table']->getOption('name') . ' ' . $params['alias']);
94
$query->set($field, '?', array('true'));
96
$field . ' = ' . $query->getConnection()->convertBooleans(false) . ' OR ' . $field . ' IS NULL'
102
* Implement preDqlSelect() hook and add the deleted flag to all queries for which this model
105
* @param Doctrine_Event $event
108
public function preDqlSelect(Doctrine_Event $event)
110
$params = $event->getParams();
111
$field = $params['alias'] . '.' . $this->_options['name'];
112
$query = $event->getQuery();
113
if ( ! $query->contains($field)) {
115
$field . ' = ' . $query->getConnection()->convertBooleans(false) . ' OR ' . $field . ' IS NULL'
b'\\ No newline at end of file'