I have one course table which has field subjec_id with comma separate value ex(1,2) the subject id and subject name are stored in another table called subject
1 —>c
2 —>c++
3 —>PHP
now my case is, i want to show c,c++ instead of showing 1,2 please help me , thanks in advance
zaccaria
(Matteo Falsitta)
November 22, 2010, 2:42pm
2
That is not the standard way for do this job.
Usually for this many-many relations is better to create a third table in wich you save the relations, like:
subjects:
1 --->c
2 --->c++
3 --->PHP
courses:
1 => system programming
2 => web designer
course_has_subject:
1 1
1 2
If you follow this standard schema, Gii will authomathically generate the many_many relation for get.
Otherway if you want to proceed on your approach, you can use a condition:
Subject::model()->findAll("id IN ($model->subjec_id)");
But this is a deprecated approach.
zaccaria:
That is not the standard way for do this job.
Usually for this many-many relations is better to create a third table in wich you save the relations, like:
subjects:
1 --->c
2 --->c++
3 --->PHP
courses:
1 => system programming
2 => web designer
course_has_subject:
1 1
1 2
If you follow this standard schema, Gii will authomathically generate the many_many relation for get.
Otherway if you want to proceed on your approach, you can use a condition:
Subject::model()->findAll("id IN ($model->subjec_id)");
But this is a deprecated approach.
could u tell me where should i use this code because im new to yii
Subject::model()->findAll("id IN ($model->subjec_id)");
zaccaria
(Matteo Falsitta)
November 23, 2010, 10:58am
5
You should have a model Subject.
When you have the need to find all subject wich are in a comma separated list, you can write:
$subjects=Subject::model()->findAll("id IN ($commaSeparatedList)");
Where in $commaSeparatedList you have something like 1,2.
Anyway I recomand you, if you are new to Yii, to follow the standard path at the beginning, this will result in a better comprehension.
Create a db structure with 3 tables, and you will have the possibility to use all powers of AR