/** * build the additional items to the query * * @return stdClass * @author Justin Palmer **/ public function build($query) { //print 'build'; //Add the select $query = str_replace('?', $this->select, $query); $query .= ' AS ' . $this->model->alias(); //build all of the joins that are called by join() $query .= $this->buildAllJoins(); //class to hold the query and the params to return. $result = new stdClass(); //any conditions? if (!empty($this->conditions)) { $query .= ' WHERE ' . array_shift($this->conditions); } //any order? if (!empty($this->order)) { $query .= ' ORDER BY ' . array_shift($this->order); } //any limit? if ($this->limit != '') { $query .= ' LIMIT ' . $this->limit; } $result->params = array_merge($this->conditions, $this->order); $result->query[] = $query; foreach ($this->has_many as $many) { $result->query[$many->alias] = "SELECT * FROM `" . $many->table . "` WHERE " . $many->on; } return $result; }
/** * manufactore and return a model */ public static function model($table, $alias = null, $pk = null) { if (!$table instanceof \Simple\Model) { $model = new Model(['table' => $table]); } if ($alias) { $model->alias($alias); } if ($pk) { $model->pk($pk); } return $model; }
/** * AutoGenerate the on for the specified relationship. * * @return string * @author Justin Palmer **/ private function autoGenerateOn($name) { $options = $this->relationships->get($name); $ret = ''; switch ($options->type) { case 'has-one': $ret = $this->model->alias() . "." . $this->model->primary_key() . " = " . $options->alias . "." . $options->foreign_key; break; case 'has-many': $ret = $options->table . "." . $options->foreign_key . " = ?"; break; } return $ret; }
/** * Find by primary key * * @todo No Builder direct access to conditions * @return void * @author Justin Palmer **/ public function find($id = null) { if ($id == null) { $primary_key = $this->model->primary_key(); if ($this->model->{$primary_key} == null) { throw new Exception('Model::find did not receive an id and the model does not have the id.'); } else { $id = $this->model->{$primary_key}; } } $database_name = $this->model->database_name(); $table_name = $this->model->table_name(); $primary_key = $this->model->alias() . '.' . $this->model->primary_key(); $primary = " ({$primary_key} = ?)"; if (!empty($this->builder->conditions)) { $and = $this->builder->conditions[0] != '' ? ' AND' : ''; $this->builder->conditions[0] = $this->builder->conditions[0] . $and . $primary; } else { $this->builder->conditions[] = $primary; } $this->builder->conditions[] = $id; $query = $this->builder->build("SELECT ? FROM `{$database_name}`.`{$table_name}`"); $this->builder->reset(); $this->Statement = $this->prepare(array_shift($query->query)); $params = $query->params; $this->Statement->execute($query->params); try { //$model = get_class($this->model); $result = ResultFactory::factory($this->Statement); foreach ($query->query as $key => $query) { //print $key; $key = Inflections::pluralize($key); $stmt = $this->prepare($query); //print $stmt->queryString . "<br/>"; //var_dump($query->params); $stmt->execute($params); $result->{$key} = ResultFactory::factory($stmt, true); } } catch (RecordNotFoundException $e) { throw $e; } return $result; }
/** * Process a model recursively and pull out all the * model names converting them to fixture names. * * @param Model $subject A Model class to scan for associations and pull fixtures off of. * @return void */ protected function _processModel($subject) { $this->_addFixture($subject->alias()); foreach ($subject->associations()->keys() as $alias) { $assoc = $subject->association($alias); $target = $assoc->target(); $name = $target->alias(); $subjectClass = get_class($subject); if ($subjectClass !== 'Cake\\ORM\\Table' && $subjectClass === get_class($target)) { continue; } if (!isset($this->_fixtures[$name])) { $this->_processModel($target); } if ($assoc->type() === Association::MANY_TO_MANY) { $junction = $assoc->junction(); if (!isset($this->_fixtures[$junction->alias()])) { $this->_processModel($junction); } } } }
/** * Process a model recursively and pull out all the * model names converting them to fixture names. * * @param Model $subject A Model class to scan for associations and pull fixtures off of. * @return void */ protected function _processModel($subject) { $this->_addFixture($subject->alias()); foreach ($subject->associations()->keys() as $alias) { $assoc = $subject->association($alias); $name = $assoc->target()->alias(); if (!isset($this->_fixtures[$name])) { $this->_processModel($assoc->target()); } if ($assoc->type() === Association::MANY_TO_MANY) { $junction = $assoc->junction(); if (!isset($this->_fixtures[$junction->alias()])) { $this->_processModel($junction); } } } }