relations() no affect on query

Here is my schema of 3 tables:


create table tbl_doc (

    doc_id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,

    doc_type integer NOT NULL

);


create table tbl_programmer (

  programmer varchar(100) not null,

  primary key (programmer)

);


create table tbl_lookup_programmer (

  doc_id integer not null,

  programmer varchar(100) not null,

  primary key (doc_id, programmer),

  foreign key (doc_id) references tbl_doc (doc_id)

    on delete cascade,

  foreign key (programmer) references tbl_programmer (programmer)

    on update cascade on delete cascade

);

The programmer and lookup_programmer tables provide a means to associate one or many programmers to every doc_id. There is no programmer column in the doc table.

Here is the relations for the doc model:




'programmer' => array(self::HAS_MANY, 'LookupProgrammer', 'tbl_lookup_programmer(doc_id, programmer)')

And the resultant query after


print_r($this->loadModel(1)); die;

:


SELECT * FROM `tbl_doc` `t` WHERE `t`.`doc_id`=1 LIMIT 1

just returns doc_id and doc_type from the table, doc.

I would like for the doc model to return the same row with a comma delimited list of programmers. Not sure how to do this nicely within Yii without writing up a complex query.

Below is the corrected relations


return array(

			'programmer' => array(self::HAS_MANY, 'LookupProgrammer', 'doc_id',

                            'select' => array('GROUP_CONCAT(programmer.programmer) as programmers'),

                            'group' => 'doc_id'

                            )

		);

I see that Yii’s query is correct and I get the right data in PHPMYADMIN, but I am unable to see the “programmers” attribute being populated. Not sure where Yii put it.


$data = Doc::model()->with('programmer')->findByPk(1);

print_r($data->programmer);

there is no "programmers" attribute anywhere!!

I can still get the expected output if I delete the ‘select’ and ‘group’ above and just manually iterate through them…I guess this is the only way then.

Add ‘programmers’ attribute to Doc model and Yii will fill it for you.

Thanks, but no dice. I added


public $programmers = ''

under the Doc model and attempted to find the "programmers" result string in both:


$data = Doc::model()->with('programmer')->findByPk(1);

print_r($data->programmer); 

print_r($data->programmer->programmers); 

Dear bulldozer

Kindly check whether following is helpful.




$data = Doc::model()->with(array('programmer'=>array('together'=>true)))->findByPk(1);

print_r($data->programmer);