C
C
ColdSpirit2015-04-09 21:01:28
MySQL
ColdSpirit, 2015-04-09 21:01:28

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:

  1. Is this approach correct in 3 tables?
  2. How can I organize the management of posts and categories, in terms of the models and controllers needed for this? Will one model and controller be enough for posts?
  3. What is the Feng Shui name for link tables? I myself did not think for a long time and called "bind_post_category"
  4. Should I use crud-generated controllers and views to display news, or is it better to write them myself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander N++, 2015-04-09
@ColdSpirit

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']);
  }
}

4 Usually I use only model generation from CRUD, since manually creating model code from scratch wastes my chakra)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question