• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Model return Undefined variable: table

#1
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!
Reply

#2
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.