D
D
Denis Labutin2015-04-14 14:57:00
Yii
Denis Labutin, 2015-04-14 14:57:00

How to add no relations in Yii?

There is a Place model, it belongs to the Metro model.
How to add the absence of the subway?
In order not to suffer too much, I decided to add id 0 name to the database. Missing, and as it turned out later, using the framework, I can’t add an entry with id 0 to the database if this entry is gone.
In some parts of the code, you have to call $model->metro->name (connection), and for many elements metro_id = 0.
How to do everything right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2015-04-14
@lazy_den

Some crutches. Write just at the "place" here is a method

public function getMetroName()
{
     return $this->metro ? $this->metro->name :  'Отсутствует'
}

And do not add any fake records to the metro table. "Places" without a metro must have a metro_id value of NULL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question