/** * Gets a row from ID. * If the primary key spans multiple columns then accepts * input only as an of column => value etc `array('key_name1'=> ...)` * Else also accepts input as a scalar value * * ``` * $post = Post::fromId(1); * ``` * * @param mixed $id * @throws \Exception * @return static */ static function fromId($id, $forUpdate = false) { $orm = ORM\Manager::getModel(TableReference::getByTableClass(get_called_class())); //Base SQL $sql = static::_select(); if ($forUpdate) { $sql->for_update($forUpdate); } //Build if ($id instanceof Parts\Where) { $sql->where($id); } else { $idk = $orm->id; if (is_array($id)) { if (count($id) != count($idk)) { throw new \Exception('Number of inputs doesnt match ' . count($id) . ' != ' . count($idk)); } if (isset($id[0])) { $idNew = array(); foreach ($idk as $k => $v) { $idk[$k] = $v; } $id = $idk; } $sql = static::_fromFields($id, $forUpdate); } else { //Input = String, Needed Array if (count($orm->id) > 1) { throw new \Exception('Needs more than one value for the ID of ' . $orm->tableInfo['name']); } $sql->where(array($idk[0] => $id)); } } $res = \Radical\DB::Query($sql); $row = $res->Fetch(); if ($row) { return new static($row); } }
function getORM() { return ORM\Manager::getModel($this); }