Skip to content

Commit

Permalink
Merge pull request #1861 from atishamte/model
Browse files Browse the repository at this point in the history
New method Find Column w.r.t. #1619
  • Loading branch information
lonnieezell authored Apr 1, 2019
2 parents 11d527a + 53f2970 commit b1820ed
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
5 changes: 5 additions & 0 deletions system/Database/Exceptions/DataException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
23 changes: 12 additions & 11 deletions system/Language/en/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
25 changes: 25 additions & 0 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 28 additions & 1 deletion tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace CodeIgniter\Database\Live;

use CodeIgniter\Config\Config;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Model;
use CodeIgniter\Test\CIDatabaseTestCase;
Expand Down Expand Up @@ -69,6 +70,32 @@ public function testFindReturnsMultipleRows()

//--------------------------------------------------------------------

public function testGetColumnWithStringColumnName()
{
$model = new JobModel($this->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);
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down

0 comments on commit b1820ed

Please sign in to comment.