File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -398,7 +398,7 @@ public function first()
398398
399399 // Some databases, like PostgreSQL, need order
400400 // information to consistently return correct results.
401- if (empty ($ builder ->QBOrderBy ))
401+ if (empty ($ builder ->QBOrderBy ) && ! empty ( $ this -> primaryKey ) )
402402 {
403403 $ builder ->orderBy ($ this ->table . '. ' . $ this ->primaryKey , 'asc ' );
404404 }
Original file line number Diff line number Diff line change @@ -85,6 +85,16 @@ public function up()
8585 ]);
8686 $ this ->forge ->addKey ('id ' , true );
8787 $ this ->forge ->createTable ('empty ' , true );
88+
89+ //No Primary Key
90+ $ this ->forge ->addField ([
91+ 'key ' => [
92+ 'type ' => 'VARCHAR ' ,
93+ 'constraint ' => 40 ,
94+ ],
95+ 'value ' => ['type ' => 'TEXT ' ],
96+ ]);
97+ $ this ->forge ->createTable ('secondary ' , true );
8898 }
8999
90100 //--------------------------------------------------------------------
@@ -95,6 +105,7 @@ public function down()
95105 $ this ->forge ->dropTable ('job ' );
96106 $ this ->forge ->dropTable ('misc ' );
97107 $ this ->forge ->dropTable ('empty ' );
108+ $ this ->forge ->dropTable ('secondary ' );
98109 }
99110
100111 //--------------------------------------------------------------------
Original file line number Diff line number Diff line change 1+ <?php namespace Tests \Support \Models ;
2+
3+ use CodeIgniter \Model ;
4+
5+ class SecondaryModel extends Model
6+ {
7+ protected $ table = 'secondary ' ;
8+
9+ protected $ primaryKey = null ;
10+
11+ protected $ returnType = 'object ' ;
12+
13+ protected $ useSoftDeletes = false ;
14+
15+ protected $ dateFormat = 'integer ' ;
16+
17+ protected $ allowedFields = [
18+ 'key ' ,
19+ 'value ' ,
20+ ];
21+ }
Original file line number Diff line number Diff line change 77use Tests \Support \Models \EntityModel ;
88use Tests \Support \Models \EventModel ;
99use Tests \Support \Models \JobModel ;
10+ use Tests \Support \Models \SecondaryModel ;
1011use Tests \Support \Models \SimpleEntity ;
1112use Tests \Support \Models \UserModel ;
1213use Tests \Support \Models \ValidModel ;
@@ -196,6 +197,25 @@ public function testFirstRespectsSoftDeletes()
196197 $ this ->assertEquals (1 , $ user ->id );
197198 }
198199
200+ public function testFirstWithNoPrimaryKey ()
201+ {
202+ $ model = new SecondaryModel ();
203+
204+ $ this ->db ->table ('secondary ' )->insert ([
205+ 'key ' => 'foo ' ,
206+ 'value ' => 'bar ' ,
207+ ]);
208+ $ this ->db ->table ('secondary ' )->insert ([
209+ 'key ' => 'bar ' ,
210+ 'value ' => 'baz ' ,
211+ ]);
212+
213+ $ record = $ model ->first ();
214+
215+ $ this ->assertEquals (1 , count ($ record ));
216+ $ this ->assertEquals ('foo ' , $ record ->key );
217+ }
218+
199219 //--------------------------------------------------------------------
200220
201221 public function testSaveNewRecordObject ()
You can’t perform that action at this time.
0 commit comments