fuel_model, has_many, no object data?

edited December 2013 in Modules
Hello again,

I've created a simple module (garages) with a has_many relationship like so:
public $has_many = array('stocks' => array('model' => array('app' => 'products_model')));

... so a garage might stock a number of products.

From the admin point of view, everything is fine, the products show, I can assign, save and remove.

I wanted to fetch the related products in a controller, and noticed in the docs that the fuel_model() method is used. So I tried
$result = fuel_model('garages', 'key', $id);
which fetches the garage's data array OK, but with no "stocks" object or array present?

Comments

  • edited December 2013
    So the following doesn't work correct?
    $result = fuel_model('garages', 'key', $id); $stocks = $result->stocks; // should return an array of product record objects $products_model = $result->get_stocks(TRUE); // should return the products_model object with the "within" query already applied to it so you can do further processing (e.g. find_all_assoc... etc)
  • edited December 2013
    Sadly.. yes. The result var is an array, but with no "stocks" object/array.
  • edited 3:59PM
    Can you add this line after 5416 in MY_Model and see the SQL being run says?
    $this->_CI->$foreign_model->debug_query();
  • edited 3:59PM
    As far as I can tell (from adding log_message() to that line and elsewhere) _get_relationship() isn't being called at all, so no query to report.
  • edited 3:59PM
    It sounds like the next place to look would be in the __get magic method on line 5278 where "_is_relationship_property" is. That __get magic method has a bunch of if else conditionals ahead of that "_is_relationship_property" call that may be intercepting that "stock" property.
  • edited 3:59PM
    Tchoh! The problem was... not declaring
    class Garage_model extends Base_module_record { }
  • edited 3:59PM
    Oh yeah... that can be a common mistake. The model will still work without a record model, but it will use a simple array syntax and may cause problems if you aren't intending to use it that way... including this problem.
  • edited 3:59PM
    My fault for not auto-generating the model, which mostly, I'm doing now!
  • edited December 2013
    Is there an example of saving 'has_many' relationships, from say a checkbox array set up as a 'multi' type. I have the HTML in place via Form Builder, I'm not clear of whether Fuel has an in-built method? Is it the save_related()? This would be outside of Fuel's admin, but using Fuel_relationships. I've written my own method, but expect there is a Fuel way.
  • edited 3:59PM
    I'm assuming that you are passing the post to the model's save method and the relationships aren't saving correct? Is the form using the model's form_fields method to generate the fields?
  • edited December 2013
    Actually it's more basic! - I'm not sure of the method to use: the save_related() example in the docs is:

    $this->examples_model->save_related( 'examples_to_categories', array('example_id' => $obj->id), array('categories_id' => $_POST['categories']));

    but I would be using the fuel_relationships look-up table to store the POST data in.

    I wrote my own method in the end, which deletes all existing rows for a given candidate_key, then inserts new (if any). I was just wondering what the 'Fuel way' to save has_many data sets was.
  • edited 3:59PM
    The save method should take care of it:
    $this->examples_model->save($_POST)
    $_POST would need to have the has_many key name you are wanting to save in it (e.g. "categories")
  • edited 3:59PM
    Aha - I thought it would be simple, maybe not that simple! I might give that a go, but thanks for the heads-up on the POST name - even if in this case it would be an array of checkboxes.
Sign In or Register to comment.