It looks like you're new here. If you want to get involved, click one of these buttons!
CREATE TABLE `w_brew_goods` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`description` varchar(5000) NOT NULL,
`cat_id` int(11) NOT NULL,
`active` enum('yes','no') NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
CREATE TABLE `w_brew_size_price` (
`brew_id` int(11) NOT NULL,
`size` enum('12','16','20') NOT NULL,
`price` decimal (5,2),
UNIQUE KEY `brew_size` (`brew_id`,`size`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
CREATE TABLE `w_product_categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`relevant` enum('Baked','Brew') NOT NULL DEFAULT 'Baked' COMMENT 'Which area is this category relvant to?',
`order` int(2) NOT NULL COMMENT 'Sort Order within relevant category',
`active` enum('yes','no') NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`),
UNIQUE KEY `sort_order` (`relevant`,`order`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
class Brew_goods_model extends Base_module_model {
public $required = array('title','description');
public $foreign_keys = array('cat_id' => array('app' => 'product_categories_model', 'where' => array('relevant' => 'Brew'))
);
function __construct()
{
parent::__construct('w_brew_goods'); // provide table name
}
function form_fields($values = array(),$related=array())
{
$fields = parent::form_fields($values,$related);
$sizes = array('12','16','20');
$fields['size'] = array('type' => 'enum', 'mode' => 'select', 'options' => $sizes, );
$fields['price'] = array();
return $fields;
}
...
class Brew_sizes_model extends Base_module_model {
public $foreign_keys = array('brew_id' => array('app' => 'brew_goods_model'));
function __construct()
{
parent::__construct('w_brew_size_price'); // provide table name
}
...
$related = array('size' => 'brew_sizes_model', 'price' => 'brew_sizes_model');
function on_after_save($values)
{
$size = $this->normalized_save_data['size'];
$price = $this->normalized_save_data['price'];
$this->save_related('brew_sizes_model',
array('brew_id' => $values['id']),
array(
'size'=> array($size),
'price'=> array($price)
)
);
}
Comments
function on_after_save($values) { $save['brew_id' = $values['id']; $save['size'] = $this->normalized_save_data['size']; $save['price'] = $this->normalized_save_data['price']; $CI =& get_instance(); $CI->load->model('brew_sizes_model'); $CI->brew_sizes_model->save($save); }
I can certainly do a standard write to the DB, I just thought there would be a relationship function I should use.
As this is in the on_after_save function, and I'm calling ->save on the model, does this address both INSERT and UPDATE? Or should I use on_after_insert and on_after_update?