CodeIgniter Forums
Why the $this->find( $id ) is generating error but get()->getRow() is working? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Why the $this->find( $id ) is generating error but get()->getRow() is working? (/showthread.php?tid=75387)



Why the $this->find( $id ) is generating error but get()->getRow() is working? - webdevron - 01-31-2020

In my model, I am trying to get an item by its ID. My code is given bellow:


PHP Code:
// namespace App\Models;
// use CodeIgniter\Model;

public function getItemByID$cid ){
    return $this->find($cid);


The above code generating the following error:

Code:
TypeError
Argument 1 passed to CodeIgniter\Database\BaseResult::getFirstRow() must be of the type string, null given, called in C:\Users\RON\OneDrive\Dev\oneb\core\Model.php on line 375

But the following code is working fine:

PHP Code:
public function getItemByID$cid ){
    return $this->where([$this->primaryKey => $cid])->get()->getRow();


What is the reason behind this?

Thanks in advance.


RE: Why the $this->find( $id ) is generating error but get()->getRow() is working? - zahhar - 02-01-2020

Check if column that stores primary key in your table is named "id" and indexed as primary key. Otherwise (e.g. name is "some_id") you should define it in your model:

<?php namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'some_id';
?>


RE: Why the $this->find( $id ) is generating error but get()->getRow() is working? - guilherme - 02-02-2020

My model is also showing this problem in the find method. It seems that he is not able to identify the return type, using entities or just defining array / object, the method does not run


RE: Why the $this->find( $id ) is generating error but get()->getRow() is working? - guilherme - 02-02-2020

I was using the constructor in the model. This caused a break in the initialization of the model's default settings.


RE: Why the $this->find( $id ) is generating error but get()->getRow() is working? - antonftn - 03-01-2021

(02-02-2020, 01:05 PM)guilherme Wrote: I was using the constructor in the model. This caused a break in the initialization of the model's default settings.
Thank you!!!!
I spent couple hours trying to figure it out Big Grin


RE: Why the $this->find( $id ) is generating error but get()->getRow() is working? - zenarter - 01-04-2022

I know this might be too late.  But you can just add parent::construct() at your model's constructor pap


RE: Why the $this->find( $id ) is generating error but get()->getRow() is working? - kidando - 06-27-2022

(01-04-2022, 10:44 AM)zenarter Wrote: I know this might be too late.  But you can just add parent::construct() at your model's constructor pap

Never too late. I was about to just forget about the constructor all together. Thanks