Reputation: 77
I have two tables, dpd
and cabang
. One cabang
can only have one dpd
and each cabang
should have a dpd
.
This is the relation in my "Cabang" model :
public function getIdDpd()
{
return $this->hasOne(Dpd::className(), ['id_dpd' => 'id_dpd']);
}
I try to access "dpd" attribute in "dpd" table in my view, I tried to var_dump it but still got the same error :
$model = Cabang::find()
->joinWith('idDpd')
->all();
var_dump($model->dpd);
Note : 'dpd'
in var_dump($model->dpd)
refered to the column name in 'Dpd'
table. I have a column name 'dpd'
in my 'dpd'
table. I also tried $model->idDpd->dpd
but it return the same error.
What am I doing wrong? Thanks
Upvotes: 0
Views: 132
Reputation: 23738
Your mistake is
$model->dpd
where you are trying to access it assuming that $model
has the Dpd
object, but it is holding the Cabang
object and you should use the relation name to get the Dpd
object and then call the field name like below.
$model->idDpd->dpd
EDIT: You are using ->all()
in your query not ->one()
and hence you cannot simply do $model->idDpd->dpd
right after the query, you need to loop through the records and then call the relation, i thought you might be doing the same way but anyway see below how to use it.
$cabangs = Cabang::find()
->joinWith('idDpd')
->all();
foreach($cabangs as $index=>$model){
$model->idDpd->dpd;
}
Upvotes: 1