i have 3 modeles interrelated.All relations has been created in database of mysql.
first model subscriber,has_many relation, code snippet as
return array( 'subscriberrel' => array(self::HAS_MANY, 'Assignment', 'id'), ); 2nd modele assignment,BELONG_TO relation,code snippet as
return array( ‘group’ => array(self::BELONGS_TO, ‘Groups’, ‘group_id’), ‘assignmentrel’ => array(self::BELONGS_TO, ‘Subscriber’, ‘phone_number_id’), ); 3rd model Groups,BELONGS_TO relation code snippet as
return array( ‘phoneNumberGroupAssignments’ => array(self::HAS_MANY, ‘Assignment’, ‘group_id’), ); Now i have a cgridview in subscriber/admin.php .i want to display phone_number_id from assignment on subscriber/admin.php using cgrid view.code snippet is here. update Error in this code snippet.
array( ‘header’=>assignment::model()->getAttributeLabel(‘phone_number_id’), ‘value’=>’($data->getRelated(\‘subscriberrel\’)=== null)?“No related assignment registry”:$data->getRelated(\‘subscriberrel\’)->phone_number_id’, ‘type’=>‘raw’, ), but when i run the page following error message. Trying to get property of non-object
C:\wamp\www\yii\framework\base\CComponent.php(607) : eval()'d code(1)
konapaz
(Konapaz)
November 28, 2012, 10:20am
2
Hi hameedhamdani
check then condition of
array( ‘header’=>assignment::model()->getAttributeLabel(‘phone_number_id’), ‘value’=>’($data->getRelated(\‘subscriberrel\’)=== null)?“No related assignment registry”:$data->getRelated(\‘subscriberrel\’)->phone_number_id’, ‘type’=>‘raw’, ),
step by step
fist check only ‘header’=>assignment::model()->getAttributeLabel(‘phone_number_id’) without ‘value’=>’’
then check
‘value’=>‘with a simple code’
and so on
KonApaz:
Hi hameedhamdani
check then condition of
array( ‘header’=>assignment::model()->getAttributeLabel(‘phone_number_id’), ‘value’=>’($data->getRelated(\‘subscriberrel\’)=== null)?“No related assignment registry”:$data->getRelated(\‘subscriberrel\’)->phone_number_id’, ‘type’=>‘raw’, ),
step by step
fist check only ‘header’=>assignment::model()->getAttributeLabel(‘phone_number_id’) without ‘value’=>’’
then check
‘value’=>‘with a simple code’
and so on
yes i did same way…header’=>assignment::model()->getAttributeLabel(‘phone_number_id’), ‘value’=’ ', so it is ok but columns have empty only.
Error is only in value on Relation-name at this line
‘value’=>’$data->getRelated(\‘assignmentrel\’)->phone_number_id’,
so what to next?
konapaz
(Konapaz)
November 28, 2012, 10:51pm
4
In relation of subscriber model class add this (you have ‘subscriberrel’ => array(self::HAS_MANY, ‘Assignment’, ‘id’) is not the correct related AR)
this is the correct (but check if the primary-foreign key are correct)
'assignmentrel' => array(self::HAS_MANY, 'Assignment', 'phone_number_id'),
In your view subscriber file (where your CGridView widget is) replace the
'value'=>'($data->getRelated(\'assignmentrel\')=== null)?"No related registry":$data->getRelated(\'assignmentrel\')->group_id',
with that
'value'=>'($data->getRelated(\'assignmentrel\')=== null)?"No related registry": array_pop($data->getRelated(\'assignmentrel\'))->group_id',
softark
(Softark)
November 29, 2012, 10:00am
5
When the relation is HAS_MANY or MANY_MANY, CActiveRecord::getRelated() will return an array of objects or an enmpty array .
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getRelated-detail
So, the code should be:
'value' => 'count($data->getRelated(\'assignmentrel\')) == 0 ?
"No related registry" :
array_pop($data->getRelated(\'assignmentrel\'))->group_id',
Or, I would write like this:
'value' => 'count($data->assignmentrel) == 0 ?
"No related registry" :
$data->assignmentrel[0]->group_id',
Note that it shows only the first related data. If you want to show all the related data, then you have to do something extra.
KonApaz:
In relation of subscriber model class add this (you have ‘subscriberrel’ => array(self::HAS_MANY, ‘Assignment’, ‘id’) is not the correct related AR)
this is the correct (but check if the primary-foreign key are correct)
'assignmentrel' => array(self::HAS_MANY, 'Assignment', 'phone_number_id'),
In your view subscriber file (where your CGridView widget is) replace the
'value'=>'($data->getRelated(\'assignmentrel\')=== null)?"No related registry":$data->getRelated(\'assignmentrel\')->group_id',
is my code wrong ,since i m usng relation of subscriber model…but fetching data from assignment model???
with that
'value'=>'($data->getRelated(\'assignmentrel\')=== null)?"No related registry": array_pop($data->getRelated(\'assignmentrel\'))->group_id',
yes i replaced
but error is
subscriber does not have relation "assignmentrel".
then i replaced the relation name with subscriberrel like
'value'=>'($data->getRelated(\'subscriberrel\')=== null)?"No related registry": array_pop($data->getRelated(\'subscriberrel\'))->group_id',
Data display but not related:
All records has been displayong one time.group_id=1,2,3,4,5,6,7,8,9,10 has been displaying only.
it is working well and displaying data but data of group_id with id of subcriber is not related according to relations of pk and foreign key.
?i:e subscriber has id=1 and in assignment model group_id=2 has been assigned to it.
But
in assignment(phone_number_group_assignment) table id=1 and group_id=1 .
it does not shows all related data???
Next Step?
softark:
When the relation is HAS_MANY or MANY_MANY, CActiveRecord::getRelated() will return an array of objects or an enmpty array .
http://www.yiiframew …tRelated-detail
So, the code should be:
'value' => 'count($data->getRelated(\'assignmentrel\')) == 0 ?
"No related registry" :
array_pop($data->getRelated(\'assignmentrel\'))->group_id',
Or, I would write like this:
'value' => 'count($data->assignmentrel) == 0 ?
"No related registry" :
$data->assignmentrel[0]->group_id',
Note that it shows only the first related data. If you want to show all the related data, then you have to do something extra.
thanks you are right…2nd code is working but not showing all related data…what to do extra?
softark
(Softark)
November 29, 2012, 10:44am
8
I’m sorry, but I don’t understand what relations you have.
Would you please write them in a format that is easy to understand? Why don’t you quote the relations() methods of your models?
// class ModelA
public function relations()
{
return array(
'model_b' => array(self::HAS_MANY, 'ModelB', 'id'),
);
}
And, please use the code tag (<> button ) in your post.
softark
(Softark)
November 29, 2012, 11:16am
9
It depends on your design.
The simplest solution might be creating a model function to return a joined data.
public function GetGroupIds()
{
$group_ids = array();
foreach($this->assignmentrel as $a)
{
$group_ids[] = $a->group_id;
}
return implode(',', $group_ids);
}
And in the view
'value' => '$data->GetGroupIds()',
softark:
I’m sorry, but I don’t understand what relations you have.
Would you please write them in a format that is easy to understand? Why don’t you quote the relations() methods of your models?
// class ModelA
public function relations()
{
return array(
'model_b' => array(self::HAS_MANY, 'ModelB', 'id'),
);
}
And, please use the code tag (<> button ) in your post.
I have two models
Related Tables:
id of subscriber is primary key.
phone_number_is is foreign key related to subscriber model’ s id.
Model No 1: subscriber model relation.
return array(
'subscriberrel' => array(self::HAS_MANY, 'Assignment', 'phone_number_id'),
);
Model No2 : assignment model relation
return array(
'assignmentrel' => array(self::BELONGS_TO, 'Subscriber', 'id'),
);
cgridview of subscriber/admin.php code for displaying column of group_id or phone_number_id from assignment model.
array(
'header'=>Assignment::model()->getAttributeLabel('group_id'), //column header
'value' => 'count($data->subscriberrel) == 0 ?
"No related registry" :
$data->subscriberrel[0]->group_id',
'type'=>'raw',
),
plz see my code and reply am i right for creating relations? but related data is not correctly displaying?
softark
(Softark)
November 29, 2012, 12:07pm
11
Subscriber HAS MANY Assignments and Assignment BELONGS TO Subscriber … is it correct?
If it is the case, then the relations should be something like the following:
// Subscriber
'assignments' => array(self::HAS_MANY, 'Assignment', phone_number_id),
// Assignment
'subscriber' => array(self::BELONG_TO, 'Subscriber', phone_number_id),
You could have used Gii to generate the models. Usually it will create the proper relations for you.
softark:
Subscriber HAS MANY Assignments and Assignment BELONGS TO Subscriber … is it correct?
If it is the case, then the relations should be something like the following:
// Subscriber
'assignments' => array(self::HAS_MANY, 'Assignment', phone_number_id),
// Assignment
'subscriber' => array(self::BELONG_TO, 'Subscriber', phone_number_id),
You could have used Gii to generate the models. Usually it will create the proper relations for you.
First thanks for quick Reply.
2ndly i am confused about this sentence.
subscriber HAS MANY Assignments and Assignments BELONGS TO Subscriber…yes it is right
But
is it necessary to write same column_name in both relations?? In Actual relations among id and phone_number_id fields.
so why we write only one column name in both relations???plz reply thanks
softark
(Softark)
November 29, 2012, 2:10pm
13
softark:
// Subscriber
'assignments' => array(self::HAS_MANY, 'Assignment', phone_number_id),
// Assignment
'subscriber' => array(self::BELONG_TO, 'Subscriber', phone_number_id),
The example above is for standard type of 1:N relation where FK "phone_number_id" of Assignment refers to PK "id" of Subscriber.
If your relation is not constructed in this way, you have to use the syntax like:
// Subscriber
'assignments' => array(self::HAS_MANY, 'Assignment', array('FK' => 'PK')),
// Assignment
'subscriber' => array(self::BELONG_TO, 'Subscriber', array('FK' => 'PK')),
http://www.yiiframework.com/doc/guide/1.1/en/database.arr
See the 2nd "info" column.
konapaz
(Konapaz)
November 29, 2012, 8:58pm
14
softark:
When the relation is HAS_MANY or MANY_MANY, CActiveRecord::getRelated() will return an array of objects or an enmpty array .
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getRelated-detail
So, the code should be:
'value' => 'count($data->getRelated(\'assignmentrel\')) == 0 ?
"No related registry" :
array_pop($data->getRelated(\'assignmentrel\'))->group_id',
Or, I would write like this:
'value' => 'count($data->assignmentrel) == 0 ?
"No related registry" :
$data->assignmentrel[0]->group_id',
Note that it shows only the first related data. If you want to show all the related data, then you have to do something extra.
I checked the first approach in my localhost before I post this code but with error, so array_pop($data->getRelated(\‘assignmentrel\’))->group_id’ runs without error, May be Inmy php setting or version
konapaz
(Konapaz)
November 29, 2012, 9:10pm
15
hameedhamdani:
First thanks for quick Reply.
2ndly i am confused about this sentence.
subscriber HAS MANY Assignments and Assignments BELONGS TO Subscriber…yes it is right
But
is it necessary to write same column_name in both relations?? In Actual relations among id and phone_number_id fields.
so why we write only one column name in both relations???plz reply thanks
Hi again hameedhamdani
The
// Subscriber
'assignments' => array(self::HAS_MANY, 'Assignment', phone_number_id),
// Assignment
'subscriber' => array(self::BELONG_TO, 'Subscriber', phone_number_id),
have sense only if an subscriber has a parent subscriber or assignments too,
I think in your case you needn’t it
To make your relations AR sense-clear check in an action (like view or index) with var_dump the primary model content (like related models and id’s)
Do not try solve the problem relations and cgridview at once, but step by step
KonApaz:
Hi again hameedhamdani
The
// Subscriber
'assignments' => array(self::HAS_MANY, 'Assignment', phone_number_id),
// Assignment
'subscriber' => array(self::BELONG_TO, 'Subscriber', phone_number_id),
have sense only if an subscriber has a parent subscriber or assignments too,
I think in your case you needn’t it
To make your relations AR sense-clear check in an action (like view or index) with var_dump the primary model content (like related models and id’s)
Do not try solve the problem relations and cgridview at once, but step by step
Thanks Sir,
i used same relations as u described but same error
Trying to get property of non-object
C:\wamp\www\yii\framework\base\CComponent.php(607) : eval()'d code(1)
in cgridview code is
array(
'header'=>Assignment::model()->getAttributeLabel('group_id'), //column header
'value'=>'($data->getRelated(\'assignments\')=== null)?"No related assignment Exist":$data->getRelated(\'assignments\')->group_id', //column name, php expression
'type'=>'raw',
),
What is the next step>
softark:
The example above is for standard type of 1:N relation where FK "phone_number_id" of Assignment refers to PK "id" of Subscriber.
If your relation is not constructed in this way, you have to use the syntax like:
// Subscriber
'assignments' => array(self::HAS_MANY, 'Assignment', array('FK' => 'PK')),
// Assignment
'subscriber' => array(self::BELONG_TO, 'Subscriber', array('FK' => 'PK')),
http://www.yiiframew …en/database.arr
See the 2nd "info" column.
yes i used 2nd approach for relations
// Subscriber
'assignments' => array(self::HAS_MANY, 'Assignment', array('FK' => 'PK')),
// Assignment
'subscriber' => array(self::BELONG_TO, 'Subscriber', array('FK' => 'PK')),
But with error
Property "subscriber.FK" is not defined.
i toggle the FK with PK and PK with FK then error
Property "subscriber.PK" is not defined.
softark
(Softark)
November 30, 2012, 6:26am
18
‘PK’ and ‘FK’ in the example are just the place holders.
You have to replace them with your PK and FK.
@softark
Thanks i have achieved my target of Relations using your adivse.Thanks
My Next problem is now how to search or filter the related column in cgridview.
softark
(Softark)
November 30, 2012, 12:27pm
20