Location: PHPKode > projects > Ace Framework > Ace-master/Ace/Form/Adapter/Doctrine.php
<?php
/**
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the new BSD license.
 *
 * @package     Ace
 * @license     http://www.opensource.org/licenses/BSD-3-Clause New BSD license
 * @since       1.0
 */
class Ace_Form_Adapter_Doctrine extends Ace_Form_Adapter
{
    /**
     * Constructor.
     * 
     * @param mixed $object
     * @param array $defaults
     * @throws Ace_Exception
     */
    public function __construct($object = null, $defaults = array())
    {
        $class = $this->getModelName();

        if(!$object)
        {
            $this->object = new $class();
        }
        else
        {
            if(!$object instanceof $class)
            {
                throw new Ace_Exception(sprintf('The "%s" form only accepts a "%s" object.', get_class($this), $class));
            }

            $this->object = $object;
            $this->is_new = !$this->getObject()->exists();
        }

        parent::__construct(array(), $defaults);

        $this->updateDefaultsFromObject();
    }

    /**
     * Gets the doctrine connection
     * 
     * @return Doctrine_Connection
     */
    public function getConnection()
    {
        return Doctrine_Manager::getInstance()->getConnectionForComponent($this->getModelName());
    }

    /**
     * @see Ace_Form_Adapter
     */
    protected function doUpdateObject($values)
    {
        $this->getObject()->fromArray($values);
    }

    /**
     * Processes cleaned up values with user defined methods.
     *
     * To process a value before it is used by the updateObject() method,
     * you need to define an updateXXXColumn() method where XXX is the PHP name
     * of the column.
     *
     * The method must return the processed value or false to remove the value
     * from the array of cleaned up values.
     *
     * @see Ace_Form_Adapter
     */
    public function processValues($values)
    {
        // see if the user has overridden some column setter
        $valuesToProcess = $values;
        foreach($valuesToProcess as $field => $value)
        {
            $method = sprintf('update%sColumn', $this->camelize($field));

            if(method_exists($this, $method))
            {
                if(false === $ret = $this->$method($value))
                {
                    unset($values[$field]);
                }
                else
                {
                    $values[$field] = $ret;
                }
            }
        }

        return $values;
    }

    /**
     * Updates the default values of the form with the current values of the current object.
     */
    protected function updateDefaultsFromObject()
    {
        $defaults = $this->getDefaults();

        // update defaults for the main object
        if($this->isNew())
        {
            $defaults = $defaults + $this->getObject()->toArray(false);
        }
        else
        {
            $defaults = $this->getObject()->toArray(false) + $defaults;
        }

        $this->setDefaults($defaults);
    }

    /**
     * Returns the Doctrine model name
     *
     * @return string
     */
    public function getModelName()
    {
        throw new RuntimeException('You must implement the function "getModelName()" in your form declaration');
    }

    /**
     * @see Ace_Form_Adapter
     */
    protected function setup()
    {
        parent::setup();
    }
}
Return current item: Ace Framework