溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

FOSCommentBundle功能包:設(shè)置Doctrine ODM映射(投票)

發(fā)布時(shí)間:2020-07-09 10:26:29 來(lái)源:網(wǎng)絡(luò) 閱讀:772 作者:firehare 欄目:MongoDB數(shù)據(jù)庫(kù)
  • 原文出處:12b-mapping_mongodb.md

  • 原文作者:FriendsOfSymfony

  • 授權(quán)許可:創(chuàng)作共用協(xié)議

  • 翻譯人員:FireHare

  • 校對(duì)人員:

  • 適用版本:FOSCommentBundle 2.0.5

  • 文章?tīng)顟B(tài):草譯階段

Step 12b: Setup MongoDB mapping

The MongoDB implementation does not provide a concrete Vote class for your use,you must create one:

ROM實(shí)現(xiàn)并沒(méi)有提供一個(gè)具體的Vote類(lèi)給您使用,您需要?jiǎng)?chuàng)建一個(gè):

<?php
// src/MyProject/MyBundle/Document/Vote.php
namespace MyProject\MyBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use FOS\CommentBundle\Document\Vote as BaseVote;
/**
 * @MongoDB\Document
 * @MongoDB\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 */
class Vote extends BaseVote
{
    /**
     * @MongoDB\Id
     */
    protected $id;
    /**
     * Comment of this vote
     *
     * @var Comment
     * @MongoDB\ReferenceOne(targetDocument="MyProject\MyBundle\Document\Comment")
     */
    protected $comment;
}

And you should implement VotableCommentInterface in your Comment class and add a field to your mapping:

并且您需要在您的Comment類(lèi)中實(shí)現(xiàn) VotableCommentInterface 接口,并添加一個(gè)字段到您的映射中:

<?php
// src/MyProject/MyBundle/Document/Comment.php
namespace MyProject\MyBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use FOS\CommentBundle\Document\Comment as BaseComment;
use FOS\CommentBundle\Model\VotableCommentInterface;
/**
 * @MongoDB\Document
 * @MongoDB\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 */
class Comment extends BaseComment implements VotableCommentInterface
{
    // .. fields
    /**
     * @MongoDB\Int
     * @var int
     */
    protected $score = 0;
    /**
     * Sets the score of the comment.
     *
     * @param integer $score
     */
    public function setScore($score)
    {
        $this->score = $score;
    }
    /**
     * Returns the current score of the comment.
     *
     * @return integer
     */
    public function getScore()
    {
        return $this->score;
    }
    /**
     * Increments the comment score by the provided
     * value.
     *
     * @param integer value
     *
     * @return integer The new comment score
     */
    public function incrementScore($by = 1)
    {
        $this->score += $by;
    }

Configure your application(配置您的應(yīng)用程序)

In YAML:

YAML格式:

# app/config/config.yml
fos_comment:
    db_driver: mongodb
    class:
        model:
            vote: MyProject\MyBundle\Document\Vote

Or if you prefer XML:

如果您偏好XML:

# app/config/config.xml
<fos_comment:config db-driver="mongodb">
    <fos_comment:class>
        <fos_comment:model
            vote="MyProject\MyBundle\Document\Vote"
        />
    </fos_comment:class>
</fos_comment:config>

Back to the main step(返回主步驟)

Step 12: Enable voting.

第12步:?jiǎn)⒂猛镀薄?p>

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI