diff --git a/system/Database/Exceptions/DataException.php b/system/Database/Exceptions/DataException.php index 1d8006d7dfc7..7230eca20493 100644 --- a/system/Database/Exceptions/DataException.php +++ b/system/Database/Exceptions/DataException.php @@ -50,4 +50,9 @@ public static function forTableNotFound(string $table) { return new static(lang('Database.tableNotFound', [$table])); } + + public static function forFindColumnHaveMultipleColumns() + { + return new static(lang('Database.forFindColumnHaveMultipleColumns')); + } } diff --git a/system/Language/en/Database.php b/system/Language/en/Database.php index 4c9976c62463..8f8149f2c982 100644 --- a/system/Language/en/Database.php +++ b/system/Language/en/Database.php @@ -15,15 +15,16 @@ */ return [ - 'invalidEvent' => '{0} is not a valid Model Event callback.', - 'invalidArgument' => 'You must provide a valid {0}.', - 'invalidAllowedFields' => 'Allowed fields must be specified for model: {0}', - 'emptyDataset' => 'There is no data to {0}.', - 'failGetFieldData' => 'Failed to get field data from database.', - 'failGetIndexData' => 'Failed to get index data from database.', - 'failGetForeignKeyData' => 'Failed to get foreign key data from database.', - 'parseStringFail' => 'Parsing key string failed.', - 'featureUnavailable' => 'This feature is not available for the database you are using.', - 'tableNotFound' => 'Table `{0}` was not found in the current database.', - 'noPrimaryKey' => '`{0}` model class does not specify a Primary Key.', + 'invalidEvent' => '{0} is not a valid Model Event callback.', + 'invalidArgument' => 'You must provide a valid {0}.', + 'invalidAllowedFields' => 'Allowed fields must be specified for model: {0}', + 'emptyDataset' => 'There is no data to {0}.', + 'failGetFieldData' => 'Failed to get field data from database.', + 'failGetIndexData' => 'Failed to get index data from database.', + 'failGetForeignKeyData' => 'Failed to get foreign key data from database.', + 'parseStringFail' => 'Parsing key string failed.', + 'featureUnavailable' => 'This feature is not available for the database you are using.', + 'tableNotFound' => 'Table `{0}` was not found in the current database.', + 'noPrimaryKey' => '`{0}` model class does not specify a Primary Key.', + 'forFindColumnHaveMultipleColumns' => 'Only single column allowed in Column name.', ]; diff --git a/system/Model.php b/system/Model.php index 62aeb6a37a52..6c633a0a8c0d 100644 --- a/system/Model.php +++ b/system/Model.php @@ -351,6 +351,31 @@ public function find($id = null) //-------------------------------------------------------------------- + /** + * Fetches the column of database from $this->table + * + * @param string $column_name Column name + * + * @return array|null The resulting row of data, or null if no data found. + * + * @throws \CodeIgniter\Database\Exceptions\DataException + */ + public function findColumn(string $columnName) + { + if (strpos($columnName, ',') !== false) + { + throw DataException::forFindColumnHaveMultipleColumns(); + } + + $resultSet = $this->select($columnName) + ->asArray() + ->find(); + + return (!empty($resultSet)) ? array_column($resultSet, $columnName) : null; + } + + //-------------------------------------------------------------------- + /** * Works with the current Query Builder instance to return * all results, while optionally limiting them. diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 3500c06dc356..c2d37f100182 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1,6 +1,7 @@ db); + + $job = $model->findColumn('name'); + + $this->assertEquals('Developer', $job[0]); + $this->assertEquals('Politician', $job[1]); + $this->assertEquals('Accountant', $job[2]); + $this->assertEquals('Musician', $job[3]); + } + + //-------------------------------------------------------------------- + + public function testGetColumnsWithMultipleColumnNames() + { + $model = new JobModel($this->db); + + $this->expectException(DataException::class); + $this->expectExceptionMessage('Only single column allowed in Column name.'); + + $job = $model->findColumn('name,description'); + } + + //-------------------------------------------------------------------- + public function testFindActsAsGetWithNoParams() { $model = new JobModel($this->db); @@ -1106,7 +1133,7 @@ public function testValidationIncludingErrors() } /** - * @expectedException CodeIgniter\Exceptions\ModelException + * @expectedException \CodeIgniter\Exceptions\ModelException * @expectedExceptionMessage `Tests\Support\Models\UserModel` model class does not specify a Primary Key. */ public function testThrowsWithNoPrimaryKey() diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index f38f4af42375..3d7ee2b4e91d 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -228,6 +228,14 @@ of just one:: If no parameters are passed in, will return all rows in that model's table, effectively acting like findAll(), though less explicit. +**findColumn()** + + Returns null or an indexed array of column values:: + + $user = $userModel->findColumn($column_name); + + $column_name should be a name of single column else you will get the DataException. + **findAll()** Returns all results::