Skip to content
Snippets Groups Projects
  • Jared Hancock's avatar
    oops: Fix crash on installation · ba0b54e3
    Jared Hancock authored
    User::fromVars in class ticket was the root. Eventually, in
    DynamicForm::getDynamicFields(), isset($this->id) was used to detect
    unsaved, new forms that have not been committed to the database; however,
    the isset() method was not implemented for the ORM.
    ba0b54e3
class.orm.php 30.98 KiB
<?php
/*********************************************************************
    class.orm.php

    Simple ORM (Object Relational Mapper) for PHP5 based on Django's ORM,
    except that complex filter operations are not supported. The ORM simply
    supports ANDed filter operations without any GROUP BY support.

    Jared Hancock <jared@osticket.com>
    Copyright (c)  2006-2013 osTicket
    http://www.osticket.com

    Released under the GNU General Public License WITHOUT ANY WARRANTY.
    See LICENSE.TXT for details.

    vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/

class VerySimpleModel {
    static $meta = array(
        'table' => false,
        'ordering' => false,
        'pk' => false
    );

    var $ht;
    var $dirty;
    var $__new__ = false;

    function __construct($row) {
        $this->ht = $row;
        $this->__setupForeignLists();
        $this->dirty = array();
    }

    function get($field) {
        return $this->ht[$field];
    }
    function __get($field) {
        if (array_key_exists($field, $this->ht))
            return $this->ht[$field];
        elseif (isset(static::$meta['joins'][$field])) {
            // TODO: Support instrumented lists and such
            $j = static::$meta['joins'][$field];
            $class = $j['fkey'][0];
            $v = $this->ht[$field] = $class::lookup(
                array($j['fkey'][1] => $this->ht[$j['local']]));
            return $v;
        }
    }

    function __isset($field) {
        return array_key_exists($field, $this->ht)
            || isset(static::$meta['joins'][$field]);
    }
    function __unset($field) {
        unset($this->ht[$field]);
    }

    function set($field, $value) {
        // Update of foreign-key by assignment to model instance
        if (isset(static::$meta['joins'][$field])) {
            $j = static::$meta['joins'][$field];
            if ($j['list'] && ($value instanceof InstrumentedList)) {
                // Magic list property
                $this->ht[$field] = $value;
                return;
            }
            // XXX: Ensure $value instanceof $j['fkey'][0]
            if ($value->__new__)