<repo_name>olegsemi9i/yii2-test.loc<file_sep>/frontend/models/Message.php
<?php

namespace frontend\models;

use Yii;

/**
 * This is the model class for table "message".
 *
 * @property int $id
 * @property int $author_id
 * @property string $text
 * @property string|null $date_add
 *
 * @property Author $author
 */
class Message extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'message';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['author_id', 'text'], 'required'],
            [['author_id'], 'integer'],
            [['text'], 'string'],
            [['date_add'], 'safe'],
            [['author_id'], 'exist', 'skipOnError' => true, 'targetClass' => Author::className(), 'targetAttribute' => ['author_id' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'author_id' => 'Author ID',
            'text' => 'Text',
            'date_add' => 'Date Add',
        ];
    }

    /**
     * Gets query for [[Author]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getAuthor()
    {
        return $this->hasOne(Author::className(), ['id' => 'author_id']);
    }
    
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
     
            if ($this->isNewRecord) 
                $this->date_add = date("Y-m-d H:i:s");

            return true;
        }
        return false;
    }
    
   
    
    
}<file_sep>/frontend/models/Author.php
<?php

namespace frontend\models;

use Yii;
use yii\web\UploadedFile;

/**
 * This is the model class for table "author".
 *
 * @property int $id
 * @property string|null $heading
 * @property string|null $name
 * @property string|null $text
 * @property string|null $date_add
 * @property string|null $img
 *
 * @property Message[] $messages
 */
class Author extends \yii\db\ActiveRecord
{
    public $file;
    public $path = 'uploads';
   
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'author';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['text'], 'string'],
            [['date_add'], 'safe'],
            [['heading', 'img'], 'string', 'max' => 255],
            [['name'], 'string', 'max' => 100],
            
            [['heading', 'name', 'text'], 'required', 'message' => 'Не может быть пустым'],
            [['file'], 'file', 'extensions' => 'png, jpg'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'heading' => 'Заголовок',
            'name' => 'Имя',
            'text' => 'Текст',
            'date_add' => 'Дата создания',
            'img' => 'Картинка',
        ];
    }

    /**
     * Gets query for [[Messages]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMessages()
    {
        return $this->hasMany(Message::className(), ['author_id' => 'id'])->orderBy('id DESC');;
    }
    
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
     
            if ($this->isNewRecord) 
                $this->date_add = date("Y-m-d H:i:s");

            return true;
        }
        return false;
    }
    
    public function beforeDelete()
    {
        foreach ($this->messages as $child) {
            $child->delete();
        }
        return parent::beforeDelete();
    }
    
    
    public function upload()
    {
        if($this->file){
            $name = $this->file->baseName.'.'.$this->file->extension; 
            $this->file->saveAs($this->path.'/'.$name, false);
            $this->img = $name;
        } 
    }
    
    public function getImageurl()
    {
        return \Yii::$app->request->BaseUrl.'/'.$this->path.'/'.$this->img;
    }
    
}<file_sep>/frontend/views/site/_messages.php
<?
    if($messages){
        foreach($messages as $message){
            echo $message->text.'</br>';
        }
    }
?>