CodeIgniter Forums

Full Version: Undefined property: CodeIgniter\View\View::$db
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, need help to solve this problem: 
ErrorException
PHP Code:
Undefined propertyCodeIgniter\View\View::$db 
PHP Code:
$this->db->limit 
Need create query to present here:
PHP Code:
90             $this->db->limit(9);
91             $categories $this->db->get_where('category', array('parent' => 0))->result_array(); 
My proporsal to query is:
PHP Code:
      public function getCategoriesNine($category_id 0) {
        $getTable $this->db->table('category')->get();
        if ($category_id 0) {
          $query $this->db->table('category', array('parent' => 0))
          ->where(['id' => $category_id ])
          ->limit(9)
          ->get()
          ->getFirstRow();
          return $query $query null;
        }
        return $getTable $getTable  null;
      

Please need help if valir of query parse
PHP Code:
->limit(9

present in the view is this proporsal
PHP Code:
                            $queryCatNine = (new \App\Models\CommonQueries())->getCategoriesNine();
                            
$categories $queryCatNine->getResultArray(); 
or I'd like to know how to define this property.

Update example need know is valid in the view:
PHP Code:
                            $queryCat = (new \App\Models\CommonQueries())->getCategoriesNine();
                            
$categories $queryCat->getResultArray();
                            
$categories->limit(10); 
Why don't you pass the query result to your view instead of creating the model in the view?
(01-16-2021, 07:54 PM)kenjis Wrote: [ -> ]Why don't you pass the query result to your view instead of creating the model in the view?
incomplete documentThans for the answer, you are right, I understand, for this reason it was the doubt, I simply wanted to know how to apply it correctly.
How about this?
Code:
      public function getCategoriesNine($category_id = 0) {
        $getTable = $this->db->table('category')->get();
        if ($category_id > 0) {
          $query = $this->db->table('category', array('parent' => 0))
          ->where(['id' => $category_id ])
          ->limit(10)
          ->get()
          ->getFirstRow();
          return $query ? $query : null;
        }
        return $getTable ? $getTable  : null;
      }
This function not its optimal, always get  all rows in category tables though the $category_id is set.

I would trade it for this:

PHP Code:
public function getCategoriesNine($category_id 0
{
  $categories $this->db->table('category');

  if ($category_id 0
  {
        // Limit 10?  getFirtsRow??? i dont understand this...
        return $categories->where(['id' => $category_id ])->limit(10)->get()->getFirstRow();
  }

  return $categories->get();

What do you want to do?

First of all, I recommend you read https://codeigniter4.github.io/CodeIgnit...ilder.html