Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Find array problem
  • I'm trying to get array of categories from Model_Category, but it's not working as expected.
    // Works fine, returns array of objects
    $categories = Model_Category::find('all');
    
    // Returns only first object
    $categories = Model_Category::find(array(1, 2));
    

    Any idea what I'm doing wrong here?
  • Not sure what you were expecting but the first param of Orm\Model::find() is either 'all'/'first'/'last' or a primary key (which can be anything, including an array as there might be mulitple PKs). This works as the 2 is ignored and only the 1 is used: there's probably 1 PK which uses the first value in the array, there's no second PK thus the second value is never used.
  • Hi Jelmer, thanks for clearing that up, just starting with FuelPHP so please bear with me :) I have a "website" model and "category" model using many-to-many relation. In website edit screen I have checkbox array of categories that you can assign to a website. Below code works, but I was wandering if there is a better way to do it using FuelPHP ORM?
    $selected = array();
    foreach (Input::post('category') as $category_id)
    {
     $selected[] = Model_Category::find($category_id);
    }
    
    $website->categories = $selected;
    
    $website->save();
    
  • $selected = Model_Category::query()->where('category_id', 'IN', Input::post('category'))->get();
    
  • Ah, perfect, thanks for that one.

Howdy, Stranger!

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

In this Discussion