sfDoctrineRecord.class.php 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<?php
/*
 * This file is part of the sfDoctrine package.
 * (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * @package    symfony.plugins
 * @subpackage sfDoctrine
 * @author     Olivier Verdier <Olivier.Verdier@gmail.com>
 * @version    SVN: $Id: sfDoctrineRecord.class.php 5050 2007-09-12 16:13:53Z Jonathan.Wage $
 */

class sfDoctrineRecord extends Doctrine_Record
{
  public function __toString()
  {
    // if the current object doesn't exist we return nothing
    if (!$this->exists())
    {
      return '-';
    }

    // we try to guess a column which would give a good description of the object
    foreach (array('name', 'title', 'description', 'id') as $descriptionColumn)
    {
      if ($this->getTable()->hasColumn($descriptionColumn))
      {
        return $this->get($descriptionColumn);
      }
    }

    return sprintf('No description for object of class "%s"', $this->getTable()->getComponentName());
  }
  
  // FIXME: All code should be updated to use the new identifier() method directly
  public function obtainIdentifier()
  {
    return $this->identifier();
  }

  public function set($name, $value, $load = true)
  {
    // ucfirst() is used instead of camelize() to be compatible with older version of Doctrine
    $filterMethod = 'filterSet'.ucfirst($name);

    if (method_exists($this, $filterMethod))
    {
      $value = $this->$filterMethod($value);
    }

    $setterMethod = 'set'.sfInflector::camelize($name);

    return method_exists($this, $setterMethod) ?  $this->$setterMethod($value) : parent::set($name, $value, $load);
  }

  public function get($name, $load = true)
  {
    $getterMethod = 'get'.sfInflector::camelize($name);

    $value = method_exists($this, $getterMethod) ? $this->$getterMethod() : parent::get($name, $load);
    
    // ucfirst() is used instead of camelize() to be compatible with older version of Doctrine
    $filterMethod = 'filterGet'.ucfirst($name);

    if (method_exists($this, $filterMethod))
    {
      $value = $this->$filterMethod($value);
    }

    return $value;
  }

  function rawSet($name, $value, $load = true)
  {
    return parent::set($name, $value, $load);
  }

  function rawGet($name)
  {
    return parent::rawGet($name);
  }

  public function __call($m, $a)
  {
    try {
      $verb = substr($m, 0, 3);

      if ($verb == 'set' || $verb == 'get')
      {
        $camelColumn = substr($m, 3);

        if (in_array($camelColumn, array_keys($this->getTable()->getRelations())))
        {
          // the column is actually a class name
          $column = $camelColumn;
        }
        else
        {
          // the relation was not found
          $column = sfInflector::underscore($camelColumn);
        }

        if ($verb == 'get')
        {
          return $this->get($column);
        }
        else // $verb must be 'set'...
        {
          return $this->set($column, $a[0]);
        }
      }
    } catch(Exception $e) {
      return parent::__call($m, $a);
    }
  }

  // added for compatibility with the _get_options_from_objects helper
  public function getPrimaryKey()
  {
    return $this->obtainIdentifier();
  }

  // to check for the existence of a record
  public function has($name)
  {
    return $this->get($name)->exists();
  }

  // Hook to update created_at and updated_at fields on record insert
  public function preInsert($event)
  {
    // Set created_at and update_at to now, if they exist in this record
    $now = date("Y-m-d H:i:s", time());
    if ($this->getTable()->hasColumn('created_at'))
    {
      $this->rawSet('created_at', $now);
    }
    if ($this->getTable()->hasColumn('updated_at'))
    {
      $this->rawSet('updated_at', $now);
    }
  }

  // Hook to update updated_at field on record update
  public function preUpdate($event)
  {
    // Set update_at to now, if it exists in this record
    $now = date("Y-m-d H:i:s", time());
    if ($this->getTable()->hasColumn('updated_at'))
    {
      $this->rawSet('updated_at', $now);
    }
  }
}