Yii2 - Gii Model Relations - Why is there a 0 after the function name?

I haven't seen this online when looking at other code, tutorials, tutorials, etc.

When I generate the model with Gii, the relationship-relative functions have zero after them.


Example:

class Benefit extends \yii\db\ActiveRecord
{
    // truncated Yii Model code...

    public function getType0()
    {
        return $this->hasOne(BenefitTypes::className(), ['id' => 'type']);
    }
}

      


BenefitTypes is an identifier for name matching:

id | name
---------------
1  =>  Federal
2  =>  Non-Profit

      

In the "Benefit" table, it has a column named "type" which is a relation to the "id" column of the "Benef_types" table.

I though I should be able to (in / views / benefit / index.php) 'type.name' but it doesn't work either. It changes the column name to "Name type" and puts "(unspecified)" in the data table ...

Example:

<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'id',
        'somevalue',
        'type.name',
    ],
]) ?>

      

What is happening, why is it not acting as intended?


UPDATE

I'm starting to think that the 0 suffix for relationship function names, that is: getType0, is due to the fact that "type" is used in the table as a column name to avoid duplication or confusion. I cannot find this documented, but would like a definite answer.

I changed the name of the function to getTypeRelation (). Then the index.php for the detailview widget uses 'typeRelation.name' and it returns the name via a relation just fine.

+3


source to share


2 answers


Your thinking is correct. Relationship names are generated using the generateRelationName () function .

protected function generateRelationName($relations, $table, $key, $multiple)
{
    if (!empty($key) && substr_compare($key, 'id', -2, 2, true) === 0 && strcasecmp($key, 'id')) {
        $key = rtrim(substr($key, 0, -2), '_');
    }
    if ($multiple) {
        $key = Inflector::pluralize($key);
    }
    $name = $rawName = Inflector::id2camel($key, '_');
    $i = 0;
    while (isset($table->columns[lcfirst($name)])) {
        $name = $rawName . ($i++);
    }
    while (isset($relations[$table->fullName][$name])) {
        $name = $rawName . ($i++);
    }
    return $name;
}

      



Yii uses the related table name as the relationship name. If you have a column with the same name as the linked table, a digit will be added to the relation to avoid confusion due to Yii's handling of magic functions. This also happens if you have two or more columns in the same table associated with the same table, for example, columns create_user_id

, update_user_id

and delete_user_id

associated with the table user

will result in a relationship named user

, user0

and user1

.

In your example, it is desirable to specify a foreign key field, such as type_id

or typeId

. Yii will handle them correctly. Another alternative when you have multiple columns associated with the same table is to simply rename the functions.

+2


source


Because the relational column name and relation name are the same. When do you call $benefit->type

what you expect, a column value / property type

or an instance BenefitTypes

? So now you know. $benefit->type

returns the value of the property and $benefit->type0

returns an instance of the relationship.



0


source







All Articles