/** * 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; }
/** * Return the ElasticSearch type * * @return string type name */ public static function getElasticSearchType() { $cls = get_called_class(); return Util::decamelize(Inflections::pluralize($cls)); }
/** * Returns the table name. * If not specified one, get the current class name and pluralize it. * * @param string $cls class name * * @return string table name */ public static function getTableName($cls = null) { $fullcls = $cls ? $cls : get_called_class(); if (array_key_exists($fullcls, self::$_table_name)) { return self::$_table_name[$fullcls]; } $cls = self::_extractClassName($fullcls); $name = Inflections::pluralize($cls); if (self::isIgnoringCase()) { $name = strtolower($name); } return $name; }
/** * Push an objet to a has many association * * @param mixed $obj object * * @return pushed */ public function push($obj) { if (!$obj) { return; } $cls = get_called_class(); $escape = Driver::$escape_char; $value = array_key_exists(self::getPK(), $this->_data) ? $this->_data[self::getPK()] : null; $other_cls = get_class($obj); $other_pk = $other_cls::getPK(); $other_value = $obj->get($other_pk); $table = Model::getTableName($other_cls); $foreign = self::hasManyForeignKey(Inflections::pluralize(strtolower($other_cls))); // if the current object exists ... if (!is_null($value)) { $obj->set(strtolower($foreign), $value); // if the pushed object is still not saved if (is_null($other_value)) { if (!$obj->save()) { return false; } $other_value = $obj->get($other_pk); } $foreign = self::$_mapping[$other_cls][$foreign]; $other_pk = self::$_mapping[$other_cls][$other_pk]; $sql = "update {$escape}{$table}{$escape} set {$escape}{$foreign}{$escape}={$value} where {$escape}{$other_pk}{$escape}={$other_value}"; $stmt = self::query($sql); $rst = $stmt->rowCount() == 1; self::closeCursor($stmt); return $rst; } // if current object does not exists ... if (is_null($value)) { $this->_pushLater($obj); } }
/** * @see Inflections::$pluralize() **/ function pluralize($string) { return Inflections::pluralize($string); }