Answer the question
In order to leave comments, you need to log in
How to use a table link correctly?
Good day to all. I am making a lightweight blog in Yii, and I have a few questions.
By the principle of encapsulation, I created 3 tables - a post, a category, and an intermediate one containing the id of both. After I generated a model for posts.
The questions are:
Answer the question
In order to leave comments, you need to log in
1 I name tables in the singular, like models.
example
blog_post
blog_category
blog_category_many or feng shui blog_category_pivot in general, any many_to_many table ends with pivot
And yet, if this table belongs to a blog, blog_ will always go first, so it's easier to navigate the database later.
Relationships as I was taught manahs, which are often used in models, I put in the trait
UserRelationshipUserId
User what I get in this connection Relationship - tells me what kind of trait UserId for which field I get the User model
A big part of the role, as sensei told me, is phpdoc support in the comments for IDE
<?php
namespace app\models\traits;
use app\models\User;
/**
* Class UserRelationUserId
*
* @package app\models\traits
* @property \app\models\User $user
*/
trait UserRelationUserId
{
/**
* Relationship get user
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
/**@var \yii\db\ActiveRecord $this */
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question