fuel_model, has_many, no object data?
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
$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)
$this->_CI->$foreign_model->debug_query();
class Garage_model extends Base_module_record { }
$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.
$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")