Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How would I code a Codeigniter query in FuelPHP?
  • Hello, Can I build an array and pass it to orm?
    I looked at the docs, but couldn't find an example. Thanks My CI code.
    // Controller
    // Build query 
       $options = array();  
       $options['sortBy'] = $product_sort;
       $options['sortDirection'] = 'ASC';
       $options['PriceLevel'] = $PriceLevel;
       if (!empty($searchField)) $options[$searchField] = $search;
       if (!empty($category)) $options['category'] = $category;
       if (!empty($onsale)) $options['onsale'] = 'Y';
       
       $data['products'] = $this->product_model->getRecords($options);
    // Model
    function getRecords($options = array())
     {  
    // Qualifications
      $this->db->select('cat_imagePath');
      $this->db->join('categories', 'categories.Category_id = inventory.Category_id');
      $this->db->where('categories.active', 'Yes');
      $this->db->where('inventory.Istatus', 'A');
      
      if (isset($options['Item']))
       $this->db->where('Item', $options['Item']);
    
                    if (isset($options['searchField']))
       $this->db->like($options['searchField'], $options['searchData']);
      $query = $this->db->get('inventory');
     return $query->result();
      }
    
  • I don't see an array being passed in this example (other then $options to your own model method). So what do you exactly mean? And the CI example is AR code, which is equivalent to our Database class. This is not an ORM, which works different. You would do something like
    $result = Model_Inventory::find()
        ->related('categories')
        ->where('categories.active', '=', 'Yes')
        ->where('Istatus', '=', 'A')
        ->get();
    
    and add extra clauses to your query if needed.
  • Hi WanWizard, I pass an options array to my product_model->getRecords($options) In CI I can just keep issuing wheres if the option[x] is set. In your example it looks like each where is part of the find. I need to issue a where if the options[x] is set.
    there can be multiple wheres, depending on the options array. I can post the entire product_model->getRecords function of that would help clarify my logic. Thanks
  • Read up on method chaining. CI does the same as FuelPHP does. You can easily write:
    $result = Model_Inventory::find();
    $result->related('categories');
    $result->where('categories.active', '=', 'Yes');
    $result->where('Istatus', '=', 'A');
    $result->get();
    
    But not very clean...
  • That's great. Thank you. For some reason, I was thinking the find returned a result set. Now I see the final get.
  • Find() has a few shortcuts that do return a result set, like find('first'), find('last') and find('all') or find($id). If you don't pass anything find() will return a query object upon which you can chain your selection methods.
  • Thanks for explaining this. Most of my CI was easy to convert. Right now, I am having trouble renaming a field in the select clause. 'List_price as price', 'description'.....
  • ORM doesn't support selects. It always selects the full record.
  • I need to do this, would I just use the database class then? DB::select()
  • If you really need to select a subset of the columns, yes. The ORM is about objects representing data, and allows you to perform manipulations on that data through the objects. Limiting the select would cause the objects to be incomplete, and as selected objects are cached because only one object may exist of any given record, once you selected a subset you would never be able to access the missing columns again. Also, due to the way an ORM works, you should not use it for any type of "batch" operations, like mass selects or updates, or complex joins. In those cases DB calls work a lot better (and faster).
  • WanWizard, Thanks for taking the time to explain this.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion