/** * Constructor. */ function __construct($params = null) { // Apply model definition. $params = array_merge((array) $this->define(), (array) $params); foreach ($params as $key => $val) { if (is_array($this->{$key})) { if (is_array($val)) { $this->{$key} = array_merge($this->{$key}, $val); } else { $this->{$key}[] = $val; } } else { $this->{$key} = $val; } } // Apply default definition. foreach ((array) $this->define_default() as $key => $val) { if (empty($this->{$key})) { $this->{$key} = $val; } else { if (is_array($this->{$key})) { if (is_array($val)) { $this->{$key} = array_merge($val, $this->{$key}); } else { $this->{$key}[] = $val; } } } } // Apply event binds. if ($this->binds) { foreach ((array) $this->binds as $event => $callback) { $this->bind($event, $callback); } } // Init db adapter? if (!$this->db) { if ($this->adapter) { $this->db = Database::get_adapter($this->adapter, array('database' => $this->database, 'name' => $this->name, 'fields' => $this->fields, 'search_fields' => $this->search_fields, 'indexes' => $this->indexes, 'pk' => $this->pk, 'auto_increment' => $this->auto_increment, 'auto_increment_start' => $this->auto_increment_start)); } else { throw new Exception('Model adapter not specified for ' . $this->name); } } }