Welcome Guest, Not a member yet? Register   Sign In
Why the $this->find( $id ) is generating error but get()->getRow() is working?
#1
Question 

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.
"Who thinks in code"
Perfectly describes who I am
mbparvez.me
Reply
#2

(This post was last modified: 02-01-2020, 10:20 AM by zahhar.)

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';
?>
Reply
#3

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
Reply
#4

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

(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
Reply
#6

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

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB