Pop\Db\Record\Prepared::findById PHP Метод

findById() публичный Метод

Find a database row by the primary ID passed through the method argument.
public findById ( mixed $id, integer $limit = null, integer $offset = null ) : void
$id mixed
$limit integer
$offset integer
Результат void
    public function findById($id, $limit = null, $offset = null)
    {
        if (null === $this->primaryId) {
            throw new Exception('This primary ID of this table either is not set or does not exist.');
        }
        // Build the SQL.
        $this->sql->select();
        if (is_array($this->primaryId)) {
            if (!is_array($id) || count($id) != count($this->primaryId)) {
                throw new Exception('The array of ID values does not match the number of IDs.');
            }
            foreach ($id as $key => $value) {
                if (null === $value) {
                    $this->sql->select()->where()->isNull($this->primaryId[$key]);
                } else {
                    $this->sql->select()->where()->equalTo($this->primaryId[$key], $this->getPlaceholder($this->primaryId[$key], $key + 1));
                }
            }
        } else {
            $this->sql->select()->where()->equalTo($this->primaryId, $this->getPlaceholder($this->primaryId));
        }
        // Set the limit, if passed
        if (null !== $limit) {
            $this->sql->select()->limit($this->sql->adapter()->escape($limit));
        }
        // Set the offset, if passed
        if (null !== $offset) {
            $this->sql->select()->offset($this->sql->adapter()->escape($offset));
        }
        // Prepare the statement
        $this->sql->adapter()->prepare($this->sql->render(true));
        if (is_array($this->primaryId)) {
            $params = array();
            foreach ($id as $key => $value) {
                if (null !== $value) {
                    $params[$this->primaryId[$key]] = $value;
                }
            }
        } else {
            $params = array($this->primaryId => $id);
        }
        // Bind the parameters, execute the statement and set the return results.
        $this->sql->adapter()->bindParams((array) $params);
        $this->sql->adapter()->execute();
        $this->setResults();
    }