温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

FOSCommentBundle功能包:设置Doctrine ODM映射(投票)

发布时间:2020-07-09 10:26:29 来源:网络 阅读:772 作者:firehare 栏目:MongoDB数据库
  • 原文出处:12b-mapping_mongodb.md

  • 原文作者:FriendsOfSymfony

  • 授权许可:创作共用协议

  • 翻译人员:FireHare

  • 校对人员:

  • 适用版本:FOSCommentBundle 2.0.5

  • 文章状态:草译阶段

Step 12b: Setup MongoDB mapping

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

ROM实现并没有提供一个具体的Vote类给您使用,您需要创建一个:

<?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类中实现 VotableCommentInterface 接口,并添加一个字段到您的映射中:

<?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(配置您的应用程序)

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步:启用投票。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI