It looks like you're new here. If you want to get involved, click one of these buttons!
function form_fields($values = array()) {
$fields = parent::form_fields();
$CI =& get_instance();
$CI->load->module_model(CATALOGUE_FOLDER, 'catalogue_attributes_model');
$CI->load->module_model(CATALOGUE_FOLDER, 'catalogue_attributes_to_sets_model');
/* ATTRIBUTES MULTI */
$attribute_options = $CI->catalogue_attributes_model->options_list();
$attribute_values = (!empty($values['id'])) ? array_keys($CI->catalogue_attributes_to_sets_model->find_all_array_assoc('attribute_id', array('set_id' => $values['id']))) : array();
$fields['attributes'] = array('label' => 'Attributes', 'type' => 'array', 'options' => $attribute_options, 'value' => $attribute_values, 'mode' => 'multi');
return $fields;
}
function _common_query() {
$this->db->select($this->_tables['catalogue_attributes_to_sets'].'.*, '.
$this->_tables['catalogue_sets'].'.id AS set_id, '.
$this->_tables['catalogue_sets'].'.titolo AS set_titolo, '.
$this->_tables['catalogue_attributes'].'.id AS attr_id, '.
$this->_tables['catalogue_attributes'].'.nome AS attr_nome, '.
$this->_tables['catalogue_attributes'].'.tipo AS attr_tipo'
, FALSE);
$this->db->join($this->_tables['catalogue_attributes'], $this->_tables['catalogue_attributes_to_sets'].'.attribute_id = '.$this->_tables['catalogue_attributes'].'.id', 'left');
$this->db->join($this->_tables['catalogue_sets'], $this->_tables['catalogue_attributes_to_sets'].'.set_id = '.$this->_tables['catalogue_sets'].'.id', 'left');
}
function on_after_save($values) {
$CI =& get_instance();
$CI->load->module_model(CATALOGUE_FOLDER, 'catalogue_attributes_to_sets_model');
$saved_data = $this->normalized_save_data;
$set_id = $values['id'];
$attributes = (!empty($saved_data['attributes'])) ? $saved_data['attributes'] : array();
$CI->catalogue_attributes_to_sets_model->delete(array('set_id' => $set_id));
foreach ($attributes as $attr) {
$attribute_set = $CI->catalogue_attributes_to_sets_model->create();
$attribute_set->set_id = $set_id;
$attribute_set->attribute_id = $attr;
$attribute_set->save();
}
}
Comments
As an FYI, the 1.0 beta provides a much more simplified way of creating model
relationships without the need for extra models and tables by using a belongs_to and has_many syntax:
https://github.com/daylightstudio/FUEL-CMS/tree/1.0
https://github.com/daylightstudio/FUEL-CMS-User-Guide-Module
in my Products model, I've set
public $foreign_keys = array('manufacturer_id' => array(CATALOGUE_FOLDER => 'catalogue_manufacturers_model'));
and correctly the form shows a select with the list of manufacturers, with key = id and value = name.
But if I do the same thing on the Images model (one-to-many relationship, a product has one or more images), the select is not formed well. I write:
public $foreign_keys = array('product_id' => array(CATALOGUE_FOLDER => 'catalogue_products_model'));
And the select has key = id and value = [some_unknown_id]. I've set the options_list() method on the products_model like this:
function options_list($key = 'id', $val = 'nome', $where = array(), $order = 'nome') { $return = parent::options_list($key, $val, $where, $order); return $return; }
but is not working.. $val is empty, so the default value is not applied. I've had to modify the method like this:
function options_list($key = 'id', $val = 'nome', $where = array(), $order = 'nome') { if (empty($key)) $key = 'id'; if (empty($val)) $val = 'nome'; if (empty($order)) $order = 'nome'; $return = parent::options_list($key, $val, $where, $order); return $return; }
to overwrite the values in case of emptiness. Strange, uh? Is this some sort of bug?
----------------------------------------------------
Just a little advisory on the blog module:
the tree() method doesn't work (loading loop on screen) because of wrong model loading. The code
$CI->load->module_model(FUEL_FOLDER, 'relationships_model');
should be
$CI->load->module_model(FUEL_FOLDER, 'fuel_relationships_model');
according to the name of the model in fuel/modules/fuel/models :-)
If an image can belong to many products, do you want to use a belongs_to property in your model instead which will give you a multi select to choose the products that belong to your image?
Thanks for the heads up on the blog_posts_model tree method not working correctly.