| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Record.php 1298 2007-05-01 19:26:03Z zYne $ |
| 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.org>. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Doctrine_Record_Filter_Compound |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Record |
| 27 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 29 | * @link www.phpdoctrine.org |
| 30 | * @since 1.0 |
| 31 | * @version $Revision: 1298 $ |
| 32 | */ |
| 33 | class Doctrine_Record_Filter_Compound extends Doctrine_Record_Filter |
| 34 | { |
| 35 | protected $_aliases = array(); |
| 36 | |
| 37 | public function __construct(array $aliases) |
| 38 | { |
| 39 | $this->_aliases = $aliases; |
| 40 | } |
| 41 | public function init() |
| 42 | { |
| 43 | // check that all aliases exist |
| 44 | foreach ($this->_aliases as $alias) { |
| 45 | $this->_table->getRelation($alias); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * filterSet |
| 51 | * defines an implementation for filtering the set() method of Doctrine_Record |
| 52 | * |
| 53 | * @param mixed $name name of the property or related component |
| 54 | */ |
| 55 | public function filterSet(Doctrine_Record $record, $name, $value) |
| 56 | { |
| 57 | foreach ($this->_aliases as $alias) { |
| 58 | if ( ! $record->exists()) { |
| 59 | if (isset($record[$alias][$name])) { |
| 60 | $record[$alias][$name] = $value; |
| 61 | |
| 62 | return $record; |
| 63 | } |
| 64 | } else { |
| 65 | // we do not want to execute N + 1 queries here, hence we cannot use get() |
| 66 | if (($ref = $record->reference($alias)) !== null) { |
| 67 | if (isset($ref[$name])) { |
| 68 | $ref[$name] = $value; |
| 69 | } |
| 70 | |
| 71 | return $record; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * filterGet |
| 79 | * defines an implementation for filtering the get() method of Doctrine_Record |
| 80 | * |
| 81 | * @param mixed $name name of the property or related component |
| 82 | */ |
| 83 | public function filterGet(Doctrine_Record $record, $name) |
| 84 | { |
| 85 | foreach ($this->_aliases as $alias) { |
| 86 | if ( ! $record->exists()) { |
| 87 | if (isset($record[$alias][$name])) { |
| 88 | return $record[$alias][$name]; |
| 89 | } |
| 90 | } else { |
| 91 | // we do not want to execute N + 1 queries here, hence we cannot use get() |
| 92 | if (($ref = $record->reference($alias)) !== null) { |
| 93 | if (isset($ref[$name])) { |
| 94 | return $ref[$name]; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |