[eluser]Unknown[/eluser]
Hi Everyone,
I'm new to codeigniter and datamapper and I'm trying to map a simple one-to-many relationship between 2 tables fr_regions and fr_departements.
Below are my table structures:
Code:
CREATE TABLE IF NOT EXISTS `fr_departements` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`region_id` int(11) NOT NULL,
`code` varchar(3) NOT NULL,
`nom_departement` varchar(250) NOT NULL,
`isfront` enum('0','1') NOT NULL DEFAULT '0',
`zone_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
and
Code:
CREATE TABLE IF NOT EXISTS `fr_regions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nom_region` varchar(100) CHARACTER SET latin1 NOT NULL,
`isfront` enum('0','1') CHARACTER SET latin1 NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
and my models are like this:
Code:
class Departement_model extends DataMapper
{
public $table = "fr_departements";
public $has_one = array(
'region' => array(
'class' => 'Region_model',
'other_field' => 'departement',
),
);
}
/* End of file departement_model.php */
/* Location: ./application/models/departement_model.php */
and
Code:
class Region_model extends DataMapper
{
public $table = "fr_regions";
public $has_many = array(
'departement' => array(
'class' => 'Departement_model',
'other_field' => 'region',
),
);
}
/* End of file fr_region.php */
/* Location: ./application/models/region_model.php */
and here is yhe code in my controller :
Code:
if($this->input->post('region')) {
$data['selected_region'] = (int)$this->input->post('region');
$dept_object = new departement_model();
$list_departements = $dept_object->where_related('Region_model', 'id_region' , $data['selected_region'])->get();
$data['list_departements'] = $list_departements;
}
else $data['selected_region'] = null;
I'm just trying to get a list of departements belonging to one region and i have this error : Unable to relate departement_model with Region_model.
Can anyone help me and tell what's wrong in my code please.
Thanks