public function create(NewsForm $form): News
{
$form->publish_at = strtotime($form->publish_at);
$news = News::create(
$form->title,
$form->short_desc,
$form->body,
$form->publish_at,
$form->is_public,
new Meta(
$form->meta->title,
$form->meta->description,
$form->meta->keywords
)
);
* @property int $id
* @property integer $created_at
* @property integer $publish_at
* @property string $title
* @property string $short_desc
* @property string $body
* @property boolean $is_public
* @property integer $photo_id
* @property integer $user_id
*
* @property Meta $meta
* @property Photo $photo
* @property User $author
*/
class News extends ActiveRecord
{
public $meta;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'shop_news';
}
public static function create(
$title,
$short_desc,
$body,
$publish_at,
$is_public,
Meta $meta): News
{
$news = new static();
$news->title = $title;
$news->short_desc = $short_desc;
$news->meta = $meta;
$news->body = $body;
$news->publish_at = $publish_at;
$news->created_at = time();
$news->is_public = $is_public;
$news->user_id = Yii::$app->user->identity->getId();
return $news;
}
public function edit(
$title,
$short_desc,
$body,
$publish_at,
$is_public,
Meta $meta)
{
$this->title = $title;
$this->short_desc = $short_desc;
$this->meta = $meta;
$this->body = $body;
$this->publish_at = $publish_at;
$this->is_public = $is_public;
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['title', 'short_desc', 'body'], 'required'],
[['title'], 'string', 'max' => 65],
[['created_at',], 'integer'],
//[['publish_at'], 'datetime', 'format' => 'php:d.m.Y H:i', 'timestampAttribute' => 'publish_at'],
[['short_desc'], 'string', 'max' => 100],
[['publish_at'], 'integer'],
[['body'], 'string'],
];
}
namespace app\models;
use app\models\news\News;
/**
* @property string $title
* @property string $short_desc
* @property string $body
* @property boolean $is_public
* @property string $publish_at
*
* @property MetaForm $meta
* @property Photo $photo
*/
class NewsForm extends CompositeForm
{
public $title;
public $short_desc;
public $body;
public $is_public;
public $publish_at;
public function __construct(News $news = null, $config = [])
{
if ($news) {
$this->title = $news->title;
$this->short_desc = $news->short_desc;
$this->body = $news->body;
$this->is_public = $news->is_public;
$this->publish_at = $news->publish_at;
$this->meta = new MetaForm($news->meta);
}
//$this->photo = new PhotoForm();
$this->meta = new MetaForm();
parent::__construct($config);
}
public function attributeLabels()
{
return [
'title' => 'Заголовок',
'short_desc' => 'Краткое описание',
'body' => 'Текст статьи',
//'photo' => 'Изображение',
'is_public' => 'Показывать на основном сайте?',
'publish_at' => 'Время публикации'
];
}
public function rules(): array
{
return [
[['title', 'short_desc', 'body'], 'required'],
[['title', 'short_desc'], 'string', 'max' => 255],
//[['created_at', 'publish_at'], 'safe'],
[['title', 'short_desc'], 'trim'],
[['body'], 'string'],
[['publish_at'], 'string'],
[['is_public'], 'boolean'],
];
}