Welcome Guest, Not a member yet? Register   Sign In
Struckle with my model and the builder - "Call to a member function get() on null"
#1

Hello!

I had the idea, to write a new program. I do this for fun, I am not a professional developer. I used CI3 at my last project 4 years ago, so I would like to have fun with CI4 now :-)

But I struckle with my model. It was not easy to write data to the database and now I can't get it out of it Angry  Here is my code to ask you for a review and feedback, how I should do it. I know, there are always different ways to the goal but I would like to have all DB-operations in my model and use the controller just as a "conductor" - as the most how-tos are describe CRUD-operations. And: I do it that way too, also without success.

So this is my latest code where I'm stuck... With this code, I get the message "Call to a member function get() on null" and I didn't found a solution. I tried the builder and without it (as you see)...

The controller

PHP Code:
<?php

namespace App\Controllers;

use 
App\Models\EpocheModel;

class 
Epoche extends \CodeIgniter\Controller {

    private $data;
    private $model;

    public function __construct()
    {
        $this->data['title']   "Epochen";
        $this->data['heading'] = "Epochen";
        $this->data['content'] = "Epoche";

        $this->model = new EpocheModel();
    }

    public function index()
    {
        $this->data['epochen'] = $this->model->getAll();
    }

    public function add()
    {
        $this->model->add($this->request);

        $this->data['epochen'] = $this->model->getAll();
    }

    public function __destruct()
    {
        echo view('BaseLayout'$this->data);
    }


And my model

PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Database\ConnectionInterface;
use 
CodeIgniter\Model;
use 
CodeIgniter\Validation\ValidationInterface;

class 
EpocheModel extends Model {

    protected $table         'epoche';
    protected $primaryKey    'id';
    protected $returnType    'object';
    protected $allowedFields = ['id''name''description''time_begin''time_end''source'];

    public function __construct()
    {
        $db = \Config\Database::connect();
        $builder $db->table('epoche');
    }

    public function add($data)
    {
        $query = [
            'parent_id'     => 0,
            'name'          => $data->getVar('label'),
            'description'   => $data->getVar('description'),
            'time_begin'    => $data->getVar('begin'),
            'time_end'      => $data->getVar('end'),
            'source'        => $data->getVar('source')
        ];

        return $this->insert($query);

    }

    public function getAll()
    {
        return $this->builder->get();

    }




Thank you!
Reply
#2

Your redefining the database db and builder in your models constructor, the model already has the builder.
just use this->builder where needed. These are already set in the model.

You do not have to tell the builder the model name its in your variables.


PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
EpocheModel extends Model {

    protected $table         'epoche'// your table is auto set here and sent to the model
    protected $primaryKey    'id';
    protected $returnType    'object';
    protected $allowedFields = ['id''name''description''time_begin''time_end''source'];

    public function __construct()
    {
    
parent::__construct();
    }

    public function add($data)
    {
        $query = [
            'parent_id'     => 0,
            'name'          => $data->getVar('label'),
            'description'   => $data->getVar('description'),
            'time_begin'    => $data->getVar('begin'),
            'time_end'      => $data->getVar('end'),
            'source'        => $data->getVar('source')
        ];

        return $this->insert($query);

    }

    public function getAll()
    {
        return $this->builder->get();

    }


What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thank you for your answer, what I did is the result of different approaches to solve my problem Big Grin Every how-to did something new...

I changed my code to you suggestion and I still get the message "Call to a member function get() on null" and it points to "APPPATH/Models/EpocheModel.php at line 38" which is the line with "$this->builder"
Reply
#4

Try this to see if your getting ant data back from the database.

PHP Code:
public function getAll()
{
    // if this doenot work then you are not getting any data back.

    $query $this->builder->get();

    foreach ($query->getResult() as $row)
    {
        echo $row->title."<br>";
        echo $row->heading."<br>";
        echo $row->content."<br>";
    }

    // dump $query to the display
    echo var_dump($query);

    exit();

    //return $this->builder->get();


Get back to me and let me know that's just a fast way of testing a method for errors.

I wont be back on until 11:30 pm tonight but I'll check here.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

If you need to work with the query builder, you need to call the builder function, it’s not a variable:
PHP Code:
$builder $this->builder(); 
See: http://codeigniter.com/user_guide/models...ry-builder

But in your case, you don’t need your getAll() function, just call the findAll() function:
PHP Code:
$this->data['epochen'] = $this->model->findAll(); 
See: http://codeigniter.com/user_guide/models...nding-data
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB