CodeIgniter Forums
Model error with builder first() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Model error with builder first() (/showthread.php?tid=85935)



Model error with builder first() - DocHojo - 12-21-2022

Hi,

I'm trying to do a simple model query but it doesn't seem to work as defined in the official documentation.

Here's my code below:
PHP Code:
<?php 
namespace App\Models;  

use CodeIgniter\Model;

class 
UtilisateurModel extends Model {

    protected $db;

    protected $table           'utilisateur';
    protected $primaryKey      'id_utilisateur';
    protected $useAutoIncrement true;
    protected $returnType      'array';
    
    
protected $allowedFields = [
        'nom',
        'email',
    ];

    public function __construct() {
        $this->db db_connect();
    }

    public function get_from_email ($email) {
        return $this->where('email'$email)->first();
    }

When I call (new UtilisateurModel)->get_from_email($email);

I got the error:
Argument 1 passed to CodeIgniter\Database\BaseResult::getFirstRow() must be of the type string, null given

How can I solve that?
Regards


RE: Model error with builder first() - kenjis - 12-21-2022

Remove your constructor.


RE: Model error with builder first() - DocHojo - 12-21-2022

Yes that does the charm!

It was there when I took the code and I didn't challenged it.

Cheers


RE: Model error with builder first() - ramonpuig - 01-24-2024

(12-21-2022, 10:47 PM)kenjis Wrote: Remove your constructor.

thank you for this! i was not conscious to see that i too was having the same problem because i was overriding the parent constructor !


RE: Model error with builder first() - tassman - 05-31-2024

(12-21-2022, 10:47 PM)kenjis Wrote: Remove your constructor.

Got the same problem and this helped me. May I know why?


RE: Model error with builder first() - tassman - 05-31-2024

(05-31-2024, 05:09 AM)tassman Wrote:
(12-21-2022, 10:47 PM)kenjis Wrote: Remove your constructor.

Got the same problem and this helped me. May I know why?

Got answer Smile (RTFM!!!)
https://codeigniter.com/user_guide/models/model.html#connecting-to-the-database


RE: Model error with builder first() - InsiteFX - 07-06-2024

Because CodeIgniter's Model automatically connects to the database.