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


Messages In This Thread
Struckle with my model and the builder - "Call to a member function get() on null" - by sprhld - 05-15-2021, 05:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB