CodeIgniter Forums
Model return Undefined variable: table - 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: Model return Undefined variable: table (/showthread.php?tid=74356)



Model return Undefined variable: table - bustersg - 09-15-2019

I get ErrorException
Undefined variable: table
APPPATH/Models/OptionsModel.php at line 22

Code:
22: $option_value = $table->find($item);

app/Models/OptionsModel.php
Code:
<?php namespace App\Models;

use CodeIgniter\Model;

class OptionsModel extends Model
{
    protected $DBGroup = 'default';
    protected $table = 'options';
    protected $primaryKey = 'option_name';
    protected $returnType = 'array';

    public function get_options($item, $display = 0)
    {
        $option_value = $table->find($item);

        if (empty($option_value)) {
            if (!$display) {
                return $option_value;
            } else {
                echo $option_value;
            }
        }
    }

}

Any help will be appreciated. Thanks!


RE: Model return Undefined variable: table - ciadmin - 09-15-2019

Two problems immediately apparent with your code:
1) $table is a class property, yet you access it as $table, when perhaps it should be $this->table
2) $table is a string, and will not have any object access ($table->anything.....)

If using CI's model, you would use the Model class' find method, eg. $this->find($item).
The CI model treats the $table property as the name of a database table the model is bound to.