From d69939ee6c759b49c68443976b2ff9476a9812e8 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 23 Mar 2019 11:38:27 +0530 Subject: [PATCH 1/9] New method Find Column w.r.t. #1619 --- system/Model.php | 31 ++++++++++++ tests/system/Database/Live/ModelTest.php | 61 ++++++++++++++++++++++++ user_guide_src/source/models/model.rst | 10 ++++ 3 files changed, 102 insertions(+) diff --git a/system/Model.php b/system/Model.php index d23dba3fd8a8..d96c947af96d 100644 --- a/system/Model.php +++ b/system/Model.php @@ -349,6 +349,37 @@ public function find($id = null) //-------------------------------------------------------------------- + /** + * Fetches the column of database from $this->table with a primary key + * matching $id. + * + * @param string|array $column_name Column name + * @param integer|string|array $id One primary key or an array of primary keys + * + * @return array|null array of table's column values + */ + public function findColumn($columnName, $id = null): ?array + { + $tmp = $this->select($columnName) + ->asArray() + ->find($id); + + if (count($tmp)) + { + $data = []; + foreach ($tmp as $item) + { + $data[] = $item[$columnName]; + } + + return $data; + } + + return 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..3a8235c87018 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -69,6 +69,67 @@ public function testFindReturnsMultipleRows() //-------------------------------------------------------------------- + public function testGetColumnWithStringColumnName() + { + $model = new JobModel($this->db); + + $job = $model->findColumn('name'); + + $this->assertEquals('Developer', $job[0]->name); + $this->assertEquals('Politician', $job[1]->name); + $this->assertEquals('Accountant', $job[2]->name); + $this->assertEquals('Musician', $job[3]->name); + } + + //-------------------------------------------------------------------- + + public function testGetColumnsWithMultipleColumnNames() + { + $model = new JobModel($this->db); + + $job = $model->findColumn('name,description'); + + $this->assertEquals('Developer', $job[0]->name); + $this->assertEquals('Awesome job, but sometimes makes you bored', $job[0]->description); + } + + //-------------------------------------------------------------------- + + public function testGetColumnsWithArrayOfColumnNames() + { + $model = new JobModel($this->db); + + $job = $model->findColumn(['name','description']); + + $this->assertEquals('Developer', $job[0]->name); + $this->assertEquals('Awesome job, but sometimes makes you bored', $job[0]->description); + } + + //-------------------------------------------------------------------- + + public function testGetColumnWithSinglePrimaryKey() + { + $model = new JobModel($this->db); + + $job = $model->findColumn('name',1); + + $this->assertEquals('Developer', $job[0]->name); + } + + //-------------------------------------------------------------------- + + public function testGetColumnWithArrayOfPrimaryKey() + { + $model = new JobModel($this->db); + + $job = $model->findColumn('name', [1, 3]); + + $this->assertEquals('Developer', $job[0]->name); + $this->assertEquals('Accountant', $job[1]->name); + } + + //-------------------------------------------------------------------- + public function testFindActsAsGetWithNoParams() { $model = new JobModel($this->db); diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index f38f4af42375..55428c1342cc 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -228,6 +228,16 @@ 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 for a specified $id:: + + $user = $userModel->find($column_name, $id); + + First parameter is either name of single column, comma separated column names, or array of column names. + + If the second parameter equals null, will return complete result set. + **findAll()** Returns all results:: From 87d8288a1f2582bcfb5b68bba5123fc646b7608b Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 23 Mar 2019 12:11:15 +0530 Subject: [PATCH 2/9] New method Find Column w.r.t. #1619 --- system/Model.php | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/system/Model.php b/system/Model.php index d96c947af96d..75f60f6fc9c6 100644 --- a/system/Model.php +++ b/system/Model.php @@ -356,26 +356,12 @@ public function find($id = null) * @param string|array $column_name Column name * @param integer|string|array $id One primary key or an array of primary keys * - * @return array|null array of table's column values + * @return array|object|null The resulting row of data, or null. */ public function findColumn($columnName, $id = null): ?array { - $tmp = $this->select($columnName) - ->asArray() + return $this->select($columnName) ->find($id); - - if (count($tmp)) - { - $data = []; - foreach ($tmp as $item) - { - $data[] = $item[$columnName]; - } - - return $data; - } - - return null; } //-------------------------------------------------------------------- From ddc9092fb95471304001e19ac9d934a056b9a9a5 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 23 Mar 2019 12:21:02 +0530 Subject: [PATCH 3/9] New method Find Column w.r.t. #1619 --- system/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Model.php b/system/Model.php index 75f60f6fc9c6..1ea06e0f4e6b 100644 --- a/system/Model.php +++ b/system/Model.php @@ -358,7 +358,7 @@ public function find($id = null) * * @return array|object|null The resulting row of data, or null. */ - public function findColumn($columnName, $id = null): ?array + public function findColumn($columnName, $id = null) { return $this->select($columnName) ->find($id); From 1f78333bacf0695617af927aafea8b481ea7c442 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 23 Mar 2019 12:32:40 +0530 Subject: [PATCH 4/9] New method Find Column w.r.t. #1619 --- tests/system/Database/Live/ModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 3a8235c87018..2aa077890548 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -113,7 +113,7 @@ public function testGetColumnWithSinglePrimaryKey() $job = $model->findColumn('name',1); - $this->assertEquals('Developer', $job[0]->name); + $this->assertEquals('Developer', $job->name); } //-------------------------------------------------------------------- From 7c22d62f2e27495c3aa7ac97ebf26e262dcdea30 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 23 Mar 2019 16:52:28 +0530 Subject: [PATCH 5/9] Update model.rst --- user_guide_src/source/models/model.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index 55428c1342cc..8077cd35eaf5 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -232,7 +232,7 @@ like findAll(), though less explicit. Returns null or an indexed array of column values for a specified $id:: - $user = $userModel->find($column_name, $id); + $user = $userModel->findColumn($column_name, $id); First parameter is either name of single column, comma separated column names, or array of column names. From f90e02228c7259e75f861941f04ef3c96f84fd17 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Tue, 26 Mar 2019 22:58:00 +0530 Subject: [PATCH 6/9] Changes in findColumn Method --- system/Database/Exceptions/DataException.php | 5 ++ system/Language/en/Database.php | 23 ++++----- system/Model.php | 35 ++++++++++---- tests/system/Database/Live/ModelTest.php | 49 +++----------------- 4 files changed, 51 insertions(+), 61 deletions(-) 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 1ea06e0f4e6b..06b4031c51a4 100644 --- a/system/Model.php +++ b/system/Model.php @@ -350,18 +350,37 @@ public function find($id = null) //-------------------------------------------------------------------- /** - * Fetches the column of database from $this->table with a primary key - * matching $id. + * Fetches the column of database from $this->table * - * @param string|array $column_name Column name - * @param integer|string|array $id One primary key or an array of primary keys + * @param string $column_name Column name * - * @return array|object|null The resulting row of data, or null. + * @return array|null The resulting row of data, or null if no data found. + * + * @throws \CodeIgniter\Database\Exceptions\DataException */ - public function findColumn($columnName, $id = null) + public function findColumn(string $columnName) { - return $this->select($columnName) - ->find($id); + if (strpos($columnName, ',') !== false) + { + throw DataException::forFindColumnHaveMultipleColumns(); + } + + $resultSet = $this->select($columnName) + ->asArray() + ->find(); + + if (count($resultSet)) + { + $data = []; + foreach ($resultSet as $item) + { + $data[] = $item[$columnName]; + } + + return $data; + } + + return null; } //-------------------------------------------------------------------- diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 2aa077890548..a4a107d66f44 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -75,10 +75,10 @@ public function testGetColumnWithStringColumnName() $job = $model->findColumn('name'); - $this->assertEquals('Developer', $job[0]->name); - $this->assertEquals('Politician', $job[1]->name); - $this->assertEquals('Accountant', $job[2]->name); - $this->assertEquals('Musician', $job[3]->name); + $this->assertEquals('Developer', $job[0]); + $this->assertEquals('Politician', $job[1]); + $this->assertEquals('Accountant', $job[2]); + $this->assertEquals('Musician', $job[3]); } //-------------------------------------------------------------------- @@ -89,43 +89,8 @@ public function testGetColumnsWithMultipleColumnNames() $job = $model->findColumn('name,description'); - $this->assertEquals('Developer', $job[0]->name); - $this->assertEquals('Awesome job, but sometimes makes you bored', $job[0]->description); - } - - //-------------------------------------------------------------------- - - public function testGetColumnsWithArrayOfColumnNames() - { - $model = new JobModel($this->db); - - $job = $model->findColumn(['name','description']); - - $this->assertEquals('Developer', $job[0]->name); - $this->assertEquals('Awesome job, but sometimes makes you bored', $job[0]->description); - } - - //-------------------------------------------------------------------- - - public function testGetColumnWithSinglePrimaryKey() - { - $model = new JobModel($this->db); - - $job = $model->findColumn('name',1); - - $this->assertEquals('Developer', $job->name); - } - - //-------------------------------------------------------------------- - - public function testGetColumnWithArrayOfPrimaryKey() - { - $model = new JobModel($this->db); - - $job = $model->findColumn('name', [1, 3]); - - $this->assertEquals('Developer', $job[0]->name); - $this->assertEquals('Accountant', $job[1]->name); + $this->expectException('\CodeIgniter\Database\Exceptions\DataException'); + $this->expectExceptionMessage('Only single column allowed in Column name.'); } //-------------------------------------------------------------------- @@ -1167,7 +1132,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() From 9ac5953f198dae8ed475f3f418e93b3d26c15545 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Tue, 26 Mar 2019 23:11:00 +0530 Subject: [PATCH 7/9] Changes in findColumn Method --- tests/system/Database/Live/ModelTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index a4a107d66f44..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,description'); - - $this->expectException('\CodeIgniter\Database\Exceptions\DataException'); + $this->expectException(DataException::class); $this->expectExceptionMessage('Only single column allowed in Column name.'); + + $job = $model->findColumn('name,description'); } //-------------------------------------------------------------------- From 45af416eaeb0a7b87be19f7c2be13fb522c3ab5f Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Tue, 26 Mar 2019 23:23:09 +0530 Subject: [PATCH 8/9] Committing findColumn user guide --- user_guide_src/source/models/model.rst | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index 8077cd35eaf5..3d7ee2b4e91d 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -230,13 +230,11 @@ like findAll(), though less explicit. **findColumn()** - Returns null or an indexed array of column values for a specified $id:: + Returns null or an indexed array of column values:: - $user = $userModel->findColumn($column_name, $id); + $user = $userModel->findColumn($column_name); - First parameter is either name of single column, comma separated column names, or array of column names. - - If the second parameter equals null, will return complete result set. + $column_name should be a name of single column else you will get the DataException. **findAll()** From 53f29700f6a66ddd914dbf03a9289c392e36cfe3 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Fri, 29 Mar 2019 09:49:59 +0530 Subject: [PATCH 9/9] Update Model.php --- system/Model.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/system/Model.php b/system/Model.php index 06b4031c51a4..8536ed7c5f83 100644 --- a/system/Model.php +++ b/system/Model.php @@ -369,18 +369,7 @@ public function findColumn(string $columnName) ->asArray() ->find(); - if (count($resultSet)) - { - $data = []; - foreach ($resultSet as $item) - { - $data[] = $item[$columnName]; - } - - return $data; - } - - return null; + return (!empty($resultSet)) ? array_column($resultSet, $columnName) : null; } //--------------------------------------------------------------------