Source for file ForeignKey.php

Documentation is available at ForeignKey.php

  1. <?php
  2. /*
  3.  *  $Id: ForeignKey.php 1517 2007-05-30 10:20:21Z 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.com>.
  20.  */
  21. Doctrine::autoload('Doctrine_Relation');
  22. /**
  23.  * Doctrine_Relation_ForeignKey
  24.  * This class represents a foreign key relation
  25.  *
  26.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  27.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  28.  * @package     Doctrine
  29.  * @category    Object Relational Mapping
  30.  * @link        www.phpdoctrine.com
  31.  * @since       1.0
  32.  * @version     $Revision: 1517 $
  33.  */
  34. {
  35.     /**
  36.      * fetchRelatedFor
  37.      *
  38.      * fetches a component related to given record
  39.      *
  40.      * @param Doctrine_Record $record 
  41.      * @return Doctrine_Record|Doctrine_Collection
  42.      */
  43.     public function fetchRelatedFor(Doctrine_Record $record)
  44.     {
  45.         $id array();
  46.         foreach ((array) $this->definition['local'as $local{
  47.            $value $record->get($local);
  48.            if (isset($value)) {
  49.                $id[$value;
  50.            }
  51.         }
  52.         if ($this->isOneToOne()) {
  53.             if $record->exists(|| empty($id|| 
  54.                  $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
  55.                 
  56.                 $related $this->getTable()->create();
  57.             else {
  58.                 $dql  'FROM ' $this->getTable()->getComponentName()
  59.                       . ' WHERE ' $this->getCondition();
  60.  
  61.                 $coll $this->getTable()->getConnection()->query($dql$id);
  62.                 $related $coll[0];
  63.             }
  64.  
  65.             $related->set($this->definition['foreign']$recordfalse);
  66.  
  67.         else {
  68.  
  69.             if $record->exists(|| empty($id|| 
  70.                  $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
  71.                 
  72.                 $related new Doctrine_Collection($this->getTable());
  73.             else {
  74.                 $query      $this->getRelationDql(1);
  75.                 $related    $this->getTable()->getConnection()->query($query$id);
  76.             }
  77.             $related->setReference($record$this);
  78.         }
  79.         return $related;
  80.     }
  81.     /**
  82.      * getCondition
  83.      *
  84.      * @param string $alias 
  85.      */
  86.     public function getCondition($alias null)
  87.     {
  88.         if $alias{
  89.            $alias $this->getTable()->getComponentName();
  90.         }
  91.         $conditions array();
  92.         foreach ((array) $this->definition['foreign'as $foreign{
  93.             $conditions[$alias '.' $foreign ' = ?';
  94.         }
  95.         return implode(' AND '$conditions);
  96.     }
  97. }