relationship - Simple relation in YII, can't get it done -
this first time use relations in yii, question simple.
module table name - pk status - fk status_id status table id pk name so, each module has 1 status.
but can not seem working.
module.php (model)
public function relations() { return array( 'status'=>array(self::belongs_to, 'modulestatus', 'status'), ); } i access them way:
$modulesar = module::model()->with('status')->findall(); if( $modulesar ) { foreach( $modulesar $modulear ) { $this->modules[ $modulear->name ] = array( 'sessionlimit' => isset($modulear->sessionlimit) ? $modulear->sessionlimit : 0, 'status' => isset($modulear->status) ? $modulear->status : 'disabled', ); } } var_dump(yii::app()->module->modules;
array(3) { ["digidoc"]=> array(2) { ["sessionlimit"]=> int(0) ["status"]=> string(1) "2" // should "disabled" } ["docusearch"]=> array(2) { ["sessionlimit"]=> int(0) ["status"]=> string(1) "1" // should "enabled" } ["printbox"]=> array(2) { ["sessionlimit"]=> int(0) ["status"]=> string(1) "2" // should "disabled" } } i appreciate help.
thanks!
edit:
query executed yii:
select `t`.`name` `t0_c0`, `t`.`status_id` `t0_c1`, `t`.`session_limit` `t0_c2`, `status`.`id` `t1_c0`, `status`.`name` `t1_c1` `ss_module` `t` left outer join `ss_module_status` `status` on (`status`.`id`=`t`.`name`) +------------+-------+-------+-------+-------+ | t0_c0 | t0_c1 | t0_c2 | t1_c0 | t1_c1 | +------------+-------+-------+-------+-------+ | digidoc | 2 | 0 | null | null | | docusearch | 1 | 2 | null | null | | printbox | 2 | 0 | null | null | +------------+-------+-------+-------+-------+ edit 2:
changing last
on (`status`.`id`=`t`.`name`); to
on (`status`.`id`=`t`.`status_id`); works expected, dont know how fix in yii.
when peek $modulear->status, whole status model instance, not it's id. go on , peek instance:
$modulear->status->name.
that's activerecord magic intended ;)
and inversely, if have status model, should have relation there saying this:
'modules' => array(self::has_many, 'module', 'status_id')
which make following possible:
$status = status::model()->find(/*somehow*/); /* $status status instance */ foreach ($status->modules $module) { // here are! looping on modules connected status // each $module full-fledged module instance }
Comments
Post a Comment