Skip to content

Commit

Permalink
Don't attach order by to query when using first() from model. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Dec 10, 2018
1 parent 3c0f2c3 commit 55da35a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public function first()

// Some databases, like PostgreSQL, need order
// information to consistently return correct results.
if (empty($builder->QBOrderBy))
if (empty($builder->QBOrderBy) && ! empty($this->primaryKey))
{
$builder->orderBy($this->table . '.' . $this->primaryKey, 'asc');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ public function up()
]);
$this->forge->addKey('id', true);
$this->forge->createTable('empty', true);

//No Primary Key
$this->forge->addField([
'key' => [
'type' => 'VARCHAR',
'constraint' => 40,
],
'value' => ['type' => 'TEXT'],
]);
$this->forge->createTable('secondary', true);
}

//--------------------------------------------------------------------
Expand All @@ -95,6 +105,7 @@ public function down()
$this->forge->dropTable('job');
$this->forge->dropTable('misc');
$this->forge->dropTable('empty');
$this->forge->dropTable('secondary');
}

//--------------------------------------------------------------------
Expand Down
21 changes: 21 additions & 0 deletions tests/_support/Models/SecondaryModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php namespace Tests\Support\Models;

use CodeIgniter\Model;

class SecondaryModel extends Model
{
protected $table = 'secondary';

protected $primaryKey = null;

protected $returnType = 'object';

protected $useSoftDeletes = false;

protected $dateFormat = 'integer';

protected $allowedFields = [
'key',
'value',
];
}
20 changes: 20 additions & 0 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Tests\Support\Models\EntityModel;
use Tests\Support\Models\EventModel;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\SecondaryModel;
use Tests\Support\Models\SimpleEntity;
use Tests\Support\Models\UserModel;
use Tests\Support\Models\ValidModel;
Expand Down Expand Up @@ -196,6 +197,25 @@ public function testFirstRespectsSoftDeletes()
$this->assertEquals(1, $user->id);
}

public function testFirstWithNoPrimaryKey()
{
$model = new SecondaryModel();

$this->db->table('secondary')->insert([
'key' => 'foo',
'value' => 'bar',
]);
$this->db->table('secondary')->insert([
'key' => 'bar',
'value' => 'baz',
]);

$record = $model->first();

$this->assertEquals(1, count($record));
$this->assertEquals('foo', $record->key);
}

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

public function testSaveNewRecordObject()
Expand Down

0 comments on commit 55da35a

Please sign in to comment.