displaying Moduals with has_many() relationship in views
I'm very familiar with CodeIgniter, but this is my first time using fuelcms. So far, I love it. I created a meetings_model.php file and a childcare_options_model.php file. The meetings model has a $has_many relationship with the childcare_options model. Importing them both into the admin area as modules was easy enough. The modules work, I can add, edit, and delete Meetings and Childcare Options as well as associate Childcare Options with Meetings. However, I'm having trouble displaying the meetings with their childcare options in a view file.
In a view file, I can display all of the meeting data, but don't know how to access the meeting's childcare data. I checked the mysql logs and indeed, the childcare_options table is not joined in fuel's mysql query.
I've read through
http://docs.getfuelcms.com/modules/tutorial and know about the fuel_relationships table. It seems like I could join the tables manually in the meetings_model->list_items() method, but I get the impression that fuel is supposed to do this automatically. Right?
In the view, i'm trying:
<?php $meetings = fuel_model('meetings', array('find' => 'all', 'date asc')); ?>
...
<?php foreach($meetings as $meeting) : ?>
...
<?php foreach($meeting->childcare_options as $childcare_option):?>
Any suggestions or examples? I can post more of my code if that would be helpful too. Thanks!
Comments
1. I'm assuming the has_many key is also "childcare_options" correct?
2. Is the $childcare_option variable in the loop empty (does $childcare_option->values() return an array of its property values)?
3. Are you able to get to line 5370 of the MY_Model class (perhaps just add an exit there to see if it stops the script)? This is the record objects __get magic method which handles those dynamic bindings to the object.
2. The loop that has the $childcare_option variable is never run (no childcare options are returned).
3. No, line 5370 is never reached. This seems like the biggest problem.
Here's the contents of my meetings_model.php file. I tried to follow the tutorials and examples available in the fuel documentation.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(FUEL_PATH.'models/base_module_model.php');
class Meetings_model extends Base_module_model {
public $has_many = array('childcare_options' => array('childcare_options_model'));
public $required = array('title','date','description');
function __construct()
{
parent::__construct('meetings'); // table name
}
function list_items($limit = NULL, $offset = NULL, $col = 'date', $order = 'asc')
{
$data = parent::list_items($limit, $offset, $col, $order);
return $data;
}
function _common_query()
{
parent::_common_query(); // to do active and published
}
function on_before_validate($values)
{
// if slug is left blank, then we generate it based on the name
$values['date'] = date('Y-m-d',strtotime($values['date']));
return $values;
}
}
class Meeting_model extends Base_module_record {
}
?>
Thanks!
I was making it harder than it needed to be. I had the childcare_options code inside an "if(isset($meeting->childcare_options))" statement. I took that away and it works! Thanks again!