Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New method Find Column w.r.t. #1619 #1861

Merged
merged 9 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.',
];
36 changes: 36 additions & 0 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,42 @@ 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();

if (count($resultSet))
{
$data = [];
foreach ($resultSet as $item)
{
$data[] = $item[$columnName];
}

atishhamte marked this conversation as resolved.
Show resolved Hide resolved
return $data;
}

return 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