FOSCommentBundle功能包:設置Doctrine ORM映射(投票)

Step 12a: Setup Doctrine ORM mapping

The ORM implementation does not provide a concrete Vote class for your use,you must create one. This can be done by extending the abstract entities provided by the bundle and creating the appropriate mappings.

ROM實現並沒有提供一個具體的Vote類給您使用,您需要創建一個。這可以通過擴展功能包提供的抽象實體並創建一個相應的映射來實現。

For example:

例如:

<?php
// src/MyProject/MyBundle/Entity/Vote.php
namespace MyProject\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\CommentBundle\Entity\Vote as BaseVote;
/**
 * @ORM\Entity
 * @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 */
class Vote extends BaseVote
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\generatedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * Comment of this vote
     *
     * @var Comment
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\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/Entity/Comment.php
namespace MyProject\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\CommentBundle\Entity\Comment as BaseComment;
use FOS\CommentBundle\Model\VotableCommentInterface;
/**
 * @ORM\Entity
 */
class Comment extends BaseComment implements VotableCommentInterface
{
    // .. fields
    /**
     * @ORM\Column(type="integer")
     * @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

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


Or if you prefer XML:

如果您偏好XML:

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

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

Step 12: Enable voting.

第12步:啓用投票。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章