本文整理汇总了PHP中common\models\Post::findOne方法的典型用法代码示例。如果您正苦于以下问题:PHP Post::findOne方法的具体用法?PHP Post::findOne怎么用?PHP Post::findOne使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common\models\Post
的用法示例。
在下文中一共展示了Post::findOne方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testSetTags
public function testSetTags()
{
$sourceTags = [1, 3];
$post = $this->postModel->findOne(2);
$post->setTags($sourceTags);
$this->assertInstanceOf('common\\models\\Post', $post);
$this->assertTrue($post->save(false));
$this->assertEquals($sourceTags, $post->getTags());
}
示例2: findModel
/**
* Finds the Post model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Post the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Post::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('您所请求的页面不存在');
}
}
示例3: findModel
protected function findModel($id)
{
if (($model = Post::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
示例4: actionDeletePost
public function actionDeletePost($id)
{
$post = Post::findOne(['id' => $id, 'user_id' => Yii::$app->user->id]);
if (!empty($post->id)) {
return $post->delete();
}
return 'Not found post';
}
示例5: getModel
/**
* Finds the Post model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $slug
* @return Post the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function getModel($slug)
{
if (($model = Post::findOne(['slug' => $slug])) !== null) {
return $model;
} else {
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
}
}
示例6: getPost
public function getPost($id)
{
if (($model = Post::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('Стать не найдена.');
}
}
示例7: actionView
public function actionView($id)
{
$post = Post::findOne($id);
if (!$post) {
throw new \yii\web\HttpException(400, 'Wrong post id', 405);
}
return $this->render('view', ['post' => $post]);
}
示例8: getCategory
public function getCategory()
{
$post = Post::findOne($this->id);
foreach (explode(',', $post->category_id) as $value) {
$category = Category::findOne($value);
$data[] = ['id' => $category->id, 'parent_id' => $category->parent_id, 'title' => $category->title, 'indent' => $this->getIndent($category->indent)];
}
return $data;
}
示例9: actionCreate
/**
* Creates a new Comment model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
* @throws NotFoundHttpException
*/
public function actionCreate()
{
$model = new Comment();
if (Yii::$app->request->isGet) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$model->author_id = Yii::$app->user->id;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['../post/view', 'id' => $model->post_id]);
} else {
return $this->render('../post/view', ['model' => Post::findOne($model->post_id)]);
}
}
示例10: actionView
public function actionView($slug)
{
$post = Post::findOne(['slug' => $slug]);
$comment = new Comment();
$comment->name = '';
$comment->email = '';
$comment->body = '';
if ($comment->load(Yii::$app->request->post())) {
$comment->status = 0;
if ($comment->save()) {
Yii::$app->session->setFlash('success', ['type' => 'success', 'duration' => 12000, 'icon' => 'fa fa-chat', 'message' => 'You\'re comment successfully stored and will shown after approval.', 'title' => 'Saving Comment']);
} else {
Yii::$app->session->setFlash('error', ['type' => 'danger', 'duration' => 12000, 'icon' => 'fa fa-chat', 'message' => 'Sorry but we couldn\'t store your comment.', 'title' => 'Saving Comment']);
}
}
return $this->render('view', ['post' => $post, 'comment' => $comment]);
}
示例11: actionCreate
public function actionCreate($id = null)
{
if ($id === null) {
$post = new Post();
} else {
if (($post = Post::findOne(['id' => $id, 'author_id' => Yii::$app->getUser()->getId()])) === null) {
throw new HttpException(404, 'Cannot find the requested post.');
}
}
if (Yii::$app->getRequest()->getIsAjax() && $post->load(Yii::$app->getRequest()->post())) {
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
return ActiveForm::validate($post);
}
if ($post->load(Yii::$app->getRequest()->post()) && $post->save()) {
return $this->redirect(Post::findOne($id)->getUrl());
}
$rootSections = Section::find()->roots()->orderBy('title')->all();
$mainSections = $post->rootSectionId === null ? [] : Section::find()->where(['section_id' => $post->rootSectionId])->orderBy('title')->all();
return $this->render('create', ['post' => $post, 'rootSections' => $rootSections, 'mainSections' => $mainSections]);
}
示例12: testComment
/**
* @param AcceptanceTester $I
*/
public function testComment(AcceptanceTester $I)
{
$I->wantTo('ensure that post comment works');
$postView = PostViewPage::openBy($I);
// $I->see('Sample Post', 'h1');
$I->see('Sample Post');
$I->amGoingTo('submit post comment form with no data');
$postView->submitComment([]);
$I->expectTo('see validations error');
$I->see('Name cannot be blank.', '.help-block');
$I->see('Email cannot be blank.', '.help-block');
$I->see('Content cannot be blank.', '.help-block');
$I->amGoingTo('submit post comment form with no correct email');
$postView->submitComment(['comment_author' => 'tester', 'comment_author_email' => 'tester.email', 'comment_content' => 'New comment']);
$I->expectTo('see that email is not correct');
$I->see('Email is not a valid email address.');
$I->dontSee('Name cannot be blank.', '.help-block');
$I->dontSee('Content cannot be blank.', '.help-block');
$I->amGoingTo('submit post comment form with correct data');
$postView->submitComment(['comment_author' => 'tester', 'comment_author_email' => 'tester@writesdown.dev', 'comment_content' => 'New comment']);
$I->expect('new comment saved');
$I->dontSee('Name cannot be blank.', '.help-block');
$I->dontSee('Email cannot be blank.', '.help-block');
$I->dontSee('Content cannot be blank.', '.help-block');
PostComment::deleteAll(['comment_author' => 'tester']);
Post::findOne(1)->updateAttributes(['post_comment_count' => '1']);
}
示例13: outCommentsTree
/**
* Output tree of comments
* @param array $comments Array of Comment
* @param int $parent_id
* @param array $options
*/
public static function outCommentsTree($comments, $parent_id, $options)
{
if (!isset($options) || !is_object($options)) {
$showReplies = isset($options['showReplies']) ? $options['showReplies'] : true;
$showReplyButton = isset($options['showReplyButton']) ? $options['showReplyButton'] : true;
$postID = isset($options['postID']) ? $options['postID'] : false;
$options = (object) compact('showReplies', 'showReplyButton', 'postID');
}
if (isset($comments[$parent_id])) {
foreach ($comments[$parent_id] as $comment) {
if (is_null($comment->user)) {
$username = 'Аноним';
$avatar = new Asset();
$avatar->assetable_type = Asset::ASSETABLE_USER;
$imageUrl = $avatar->getDefaultFileUrl();
$userUrl = false;
} else {
$username = $comment->user->getDisplayName();
$avatar = $comment->user->getAsset();
$imageUrl = $avatar->getFileUrl();
$userUrl = $comment->user->getUrl();
}
$commentDate = Yii::$app->formatter->asDate(strtotime($comment->created_at), 'd MMMM Y HH:mm');
$repliesCommentsCount = isset($comments[$comment->id]) ? count($comments[$comment->id]) : 0;
$classRepliesCount = $repliesCommentsCount == 0 ? 'no-replies' : '';
$textRepliesCount = $repliesCommentsCount == 0 ? '' : $repliesCommentsCount;
$isReply = $parent_id == 0 ? false : true;
$own = isset(Yii::$app->user->id) && Yii::$app->user->id == $comment->user_id ? 'yes' : 'no';
$rating = $comment->getRating();
$ratingUpClass = '';
$ratingDownClass = '';
if (!Yii::$app->user->isGuest && Yii::$app->user->id != $comment->user_id) {
$userRating = $comment->getUserVote();
if ($userRating == 1) {
$ratingUpClass = 'voted';
} elseif ($userRating == -1) {
$ratingDownClass = 'voted';
}
} else {
$ratingUpClass = 'disable';
$ratingDownClass = 'disable';
}
$commentLevelClass = $parent_id == 0 ? 'lvl-one' : '';
if ($parent_id == 0 && $options->postID && $comment->getCommentableType() == Comment::COMMENTABLE_POST) {
$post = Post::findOne($comment->commentable_id);
if (isset($post->id) && $post->id !== $options->postID) {
$options->postID = $post->id;
?>
<div class="comment-theme">
<div class="theme-label">Комментарии по теме:</div>
<div class="theme-link">
<a href="<?php
echo $post->getUrl();
?>
" data-pjax="0"><?php
echo $post->title;
?>
</a>
</div>
</div>
<?php
}
}
$adminLink = '';
if (Yii::$app->user->can('admin')) {
$adminLink = '<a class="admin-view-link" target="_blank" href="/admin/comment/' . $comment->id . '" data-pjax="0"></a>';
}
?>
<div id="comment-<?php
echo $comment->id;
?>
" class="comment <?php
echo $commentLevelClass;
?>
"
data-own="<?php
echo $own;
?>
"
data-comment-id="<?php
echo $comment->id;
?>
"
data-commentable-type="<?php
echo $comment->getCommentableType();
?>
"
data-commentable-id="<?php
echo $comment->commentable_id;
?>
" >
<div class="comment-user">
<div class="user-photo">
<?php
//.........这里部分代码省略.........
示例14: actionEditPost
public function actionEditPost($id)
{
if (!YiiForum::checkAuth('post_edit')) {
return $this->noPermission();
}
YiiForum::checkIsGuest();
$boardId = YiiForum::getGetValue('boardid');
$model = Post::findOne(['id' => $id]);
$data = YiiForum::getPostValue('Post');
if ($data == null) {
$thread = Thread::findOne(['id' => $model['thread_id']]);
$locals = [];
$locals['thread'] = $thread;
$locals['currentBoard'] = $this->getBoard($boardId);
$locals['model'] = $model;
return $this->render('edit-post', $locals);
} else {
$model->load(Yii::$app->request->post());
$model->modify_time = TTimeHelper::getCurrentTime();
$model->save();
return $this->redirect(['view', 'id' => $model->thread_id]);
}
}
示例15: actionPopup
/**
* Render file browser for editor and file input
*
* @param int|null $post_id
* @param bool $editor
*
* @throws \yii\web\NotFoundHttpException
* @return string
*/
public function actionPopup($post_id = null, $editor = false)
{
$this->layout = "blank";
$model = new Media(['scenario' => 'upload']);
if ($post_id) {
if ($post = Post::findOne($post_id)) {
return $this->render('popup', ['post' => $post, 'model' => $model, 'editor' => $editor]);
} else {
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
}
}
return $this->render('popup', ['model' => $model, 'editor' => $editor]);
}