public function create() { $isValid = false; $this->imageFile = UploadedFile::getInstance($this, 'imageFile'); $user = User::findOne(Yii::$app->user->getId()); $bulletin = new Bulletin(); if (isset($this->imageFile)) { $imagePath = '/uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension; $this->imageFile->saveAs(Yii::getAlias('@webroot') . $imagePath); $m_image = new Image(); $m_image->url = $imagePath; if ($m_image->save()) { $image = Image::findOne($m_image->getPrimaryKey()); $bulletin->link('image', $image); $bulletin->link('user', $user); $isValid = true; } else { $isValid = false; } } $bulletin->title = $this->title; $bulletin->description = $this->description; if ($bulletin->save()) { $isValid = true; } else { $isValid = false; } return $isValid; }
/** * Displays homepage. * * @return mixed */ public function actionIndex() { $query = Bulletin::find(); $pagination = new Pagination(['defaultPageSize' => 10, 'totalCount' => $query->count()]); $bulletins = $query->orderBy(['created_at' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('index', ['bulletins' => $bulletins, 'pagination' => $pagination]); }
public function actionBulletin() { $bulletinForm = new BulletinForm(); $query = Bulletin::find()->where(['user_id' => YII::$app->user->getId()]); $pagination = new Pagination(['defaultPageSize' => 5, 'totalCount' => $query->count()]); $bulletins = $query->orderBy('created_at')->orderBy(['created_at' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('bulletin', ['model' => $bulletinForm, 'bulletins' => $bulletins, 'pagination' => $pagination]); }
public function getBulletins() { return $this->hasMany(Bulletin::className(), ['user_id' => 'id']); }