| 1 | <?php |
| 2 | /* |
| 3 | * $Id: LocalKey.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $ |
| 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 | Doctrine::autoload('Doctrine_Relation'); |
| 22 | /** |
| 23 | * Doctrine_Relation_LocalKey |
| 24 | * This class represents a local key relation |
| 25 | * |
| 26 | * @package Doctrine |
| 27 | * @subpackage Relation |
| 28 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 29 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 30 | * @link www.phpdoctrine.org |
| 31 | * @since 1.0 |
| 32 | * @version $Revision: 2963 $ |
| 33 | */ |
| 34 | class Doctrine_Relation_LocalKey extends Doctrine_Relation |
| 35 | { |
| 36 | /** |
| 37 | * fetchRelatedFor |
| 38 | * |
| 39 | * fetches a component related to given record |
| 40 | * |
| 41 | * @param Doctrine_Record $record |
| 42 | * @return Doctrine_Record|Doctrine_Collection |
| 43 | */ |
| 44 | public function fetchRelatedFor(Doctrine_Record $record) |
| 45 | { |
| 46 | $id = $record->get($this->definition['local']); |
| 47 | |
| 48 | if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { |
| 49 | $related = $this->getTable()->create(); |
| 50 | } else { |
| 51 | $dql = 'FROM ' . $this->getTable()->getComponentName() |
| 52 | . ' WHERE ' . $this->getCondition(); |
| 53 | |
| 54 | $related = $this->getTable() |
| 55 | ->getConnection() |
| 56 | ->query($dql, array($id)) |
| 57 | ->getFirst(); |
| 58 | |
| 59 | if ( ! $related || empty($related)) { |
| 60 | $related = $this->getTable()->create(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $record->set($this->definition['local'], $related, false); |
| 65 | |
| 66 | return $related; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * getCondition |
| 71 | * |
| 72 | * @param string $alias |
| 73 | */ |
| 74 | public function getCondition($alias = null) |
| 75 | { |
| 76 | if ( ! $alias) { |
| 77 | $alias = $this->getTable()->getComponentName(); |
| 78 | } |
| 79 | return $alias . '.' . $this->definition['foreign'] . ' = ?'; |
| 80 | } |
| 81 | } |