/** * Create new model from the same base class * as $this. * * @param string $class * @param array $options * * @return Model */ public function newInstance($class = null, $options = []) { if ($class === null) { $class = get_class($this); } $m = $this->persistence->add($class, $options); return $m; }
/** * Associate model with the data driver. * * @param Model|string $m Model which will use this persistence * @param array $defaults Properties * * @return Model */ public function add($m, $defaults = []) { // Use our own classes for fields, references and expressions unless // $defaults specify them otherwise. $defaults = array_merge(['_default_class_addField' => $this->_default_class_addField, '_default_class_hasOne' => $this->_default_class_hasOne, '_default_class_hasMany' => $this->_default_class_hasMany, '_default_class_addExpression' => $this->_default_class_addExpression, '_default_class_join' => $this->_default_class_join], $defaults); $m = parent::add($m, $defaults); if (!isset($m->table) || !is_string($m->table) && $m->table !== false) { throw new Exception(['Property $table must be specified for a model', 'model' => $m]); } // When we work without table, we can't have any IDs if ($m->table === false) { $m->getElement('id')->destroy(); $m->addExpression('id', '1'); } else { // SQL databases use ID of int by default //$m->getElement('id')->type = 'integer'; } return $m; }