From 8e7abbe65af749ada8cda83794d7c999a183edd8 Mon Sep 17 00:00:00 2001 From: mgralikowski Date: Sat, 8 Jun 2019 18:40:57 +0200 Subject: [PATCH] Add getValue method for models objects. (#837) * Add getValue method Add getValue method for models objects. * Update dbObject.md Add info about getValue() * Update dbObject.php Add has method to models --- dbObject.md | 2 +- dbObject.php | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/dbObject.md b/dbObject.md index b1233a19..e08f54e0 100644 --- a/dbObject.md +++ b/dbObject.md @@ -39,7 +39,7 @@ will not be working with an objects created with `table()` method. ### Selects -Retrieving objects from the database is pretty much the same process as a mysqliDb `get()`/`getOne()` methods without a need to specify table name. All mysqlidb functions like `where()`, `orWhere()`, `orderBy()`, `join()`, etc. are supported. +Retrieving objects from the database is pretty much the same process as a mysqliDb `get()`/`getOne()`/`getValue()` methods without a need to specify table name. All mysqlidb functions like `where()`, `orWhere()`, `orderBy()`, `join()`, etc. are supported. ## Retrieving All Records diff --git a/dbObject.php b/dbObject.php index 987a3a99..35757078 100644 --- a/dbObject.php +++ b/dbObject.php @@ -374,7 +374,34 @@ protected function getOne ($fields = null) { return $item; } + + /** + * A convenient SELECT COLUMN function to get a single column value from model object + * + * @param string $column The desired column + * @param int $limit Limit of rows to select. Use null for unlimited..1 by default + * + * @return mixed Contains the value of a returned column / array of values + * @throws Exception + */ + protected function getValue ($column, $limit = 1) { + $res = $this->db->ArrayBuilder()->getValue ($this->dbTable, $column, $limit); + if (!$res) + return null; + return $res; + } + /** + * A convenient function that returns TRUE if exists at least an element that + * satisfy the where condition specified calling the "where" method before this one. + * + * @return bool + * @throws Exception + */ + protected function has() { + return $this->db->has($this->dbTable); + } + /** * Fetch all objects *