V
V
Vladislav2018-10-29 12:32:58
Yii
Vladislav, 2018-10-29 12:32:58

How to make tables linked by key?

There are two tables - comments and post, you need to link these tables by the key 'author_id' => 'id'. I tried to do this, but nothing comes up in response.
post.php

<?php

namespace frontend\models;

use Yii;
use yii\db\Connection;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "post".
 *
 * @property int $id
 * @property int $user_id
 * @property string $filename
 * @property string $description
 * @property int $created_at
 * @property int $complaints
 */
class Post extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'post';
    }

    public function getComments()
    {
        $this->hasMany(Comments::class, ['post_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'filename' => 'Filename',
            'description' => 'Description',
            'created_at' => 'Created At',
        ];
    }
}

Comments.php
<?php

namespace frontend\models;

use Yii;

/**
 * This is the model class for table "comments".
 *
 * @property int $id
 * @property int $author_id
 * @property int $post_id
 * @property string $comment_text
 * @property int $created_at
 */
class Comments extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'comments';
    }

    public function getPost()
    {
        $this->hasOne(Post::class, ['id' => 'post_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'author_id' => 'Author ID',
            'post_id' => 'Post ID',
            'comment_text' => 'Comment Text',
            'created_at' => 'Created At',
        ];
    }
}

Action:
/**
     * View post
     * @param $id
     * @return string
     */
    public function actionView($id)
    {
        /* @var $currentUser User */
        $currentUser = Yii::$app->user->identity;

        $post = Post::findOne($id);

        // именно эта переменная пуста, хотя должна содержать комментарии
        $comments = $post->comments;


        echo '<pre>';print_r($comments);die;

        $commentsForm = new CommentsForm();

        return $this->render('view', [
            'post' => $this->findPost($id),
            'currentUser' => $currentUser,
            'comments' => $comments,
            'commentsForm' => $commentsForm,
        ]);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2018-10-29
@Cetch

public function getPost()
    {
        return $this->hasOne(Post::class, ['id' => 'post_id']);
    }

Oh, and annotations, of course.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question