/** * Join the tables passed in based off the Schema. * * @return void * @author Justin Palmer **/ public function join($args) { $args = func_get_args(); foreach ($args as $key) { if (!$this->model->schema()->relationships->isKey($key)) { throw new NoSchemaRelationshipDefinedException($this->model->table_name(), $key); } $this->relationships[] = $this->model->schema()->relationships->get($key); } return $this->model; }
/** * Add the relationship * * @return Schema * @author Justin Palmer **/ private function addRelationship($name, $type) { $options = new stdClass(); $options->name = $name; $options->type = $type; $options->alias = Inflections::underscore(str_replace('-', '_', $name)); $options->table = Inflections::tableize($options->alias); $options->foreign_key = Inflections::foreignKey(Inflections::singularize($this->model->table_name())); $this->relationships->set($name, $options); $options->on = $this->autoGenerateOn($name); $this->relationships->set($name, $options); return $this; }
/** * Save the model record * * @return void * @author Justin Palmer **/ public function saveNow() { $database_name = $this->model->database_name(); $table_name = $this->model->table_name(); $primary_key_name = $this->model->primary_key(); $id = $primary_key_name; //var_dump($this->model->$id); if ($this->model->{$id} === null) { try { $this->model->created_at = new Expression('NOW()'); } catch (NoColumnInTableException $e) { } //print 'insert' . '<br/>'; $props = $this->model->props()->export(); $columns = $this->getInsertColumnNames($props); $marks = $this->getValues($props); $this->Statement = $this->prepare(sprintf("INSERT INTO `{$database_name}`.`{$table_name}` (%s) values (%s)", $columns, $marks)); $params = array_values($this->model->props()->export()); if ($this->Statement->execute($params)) { $ret = true; $this->model->{$primary_key_name} = $this->lastInsertId(); } else { $ret = (object) $this->Statement->errorInfo(); } return $ret; } else { try { $this->model->updated_at = new Expression('NOW()'); } catch (NoColumnInTableException $e) { } //print 'update' . '<br/>'; $id = $this->model->{$primary_key_name}; $this->model->removeProperty($primary_key_name); //Get the props before setting the primary key for the UpdateSet method $props = $this->model->props()->export(); $query = "UPDATE `{$database_name}`.`{$table_name}` SET %s WHERE `{$primary_key_name}` = ?"; $this->Statement = $this->prepare(sprintf($query, $this->getUpdateSet($props))); $this->model->{$primary_key_name} = $id; return $this->Statement->execute($this->getUpdateValues()) ? true : false; } }
/** * model * @param string $table [description] */ function M($table = '') { Model::$table_name = $table; Model::$tpr = C('DB_PREFIX'); return new Model(); }