使用symfony2配置可责备的学说扩展

JML*_*JML 8 php symfony doctrine-orm

任何人都可以提供Blameable Gedmo扩展的完整示例,尤其是Blameable Listener的配置吗?

我正在使用文档提供的代码:

 * @var User $createdBy
 *
 * @Gedmo\Blameable(on="create")
 * @ORM\ManyToOne(targetEntity="Cf\UserBundle\Entity\User")
 * @ORM\JoinColumn(name="createdBy", referencedColumnName="id")
 */
private $createdBy;

/**
 * @var User $updatedBy
 *
 * @Gedmo\Blameable(on="update")
 * @ORM\ManyToOne(targetEntity="Cf\UserBundle\Entity\User")
 * @ORM\JoinColumn(name="updatedBy", referencedColumnName="id")
 */
private $updatedBy;
Run Code Online (Sandbox Code Playgroud)

但createdBy和updatedBy数据库列始终为NULL.

该文档提供了配置其他侦听器的示例(例如我工作的时间戳)但我找不到可责备侦听器的示例或文档.

谢谢你的帮助!!

================================================== =============

编辑回答让:

是的我添加了以下用途:

use Gedmo\Mapping\Annotation as Gedmo;
Run Code Online (Sandbox Code Playgroud)

我还使用Timestampable提供的特性:

use Gedmo\Timestampable\Traits\TimestampableEntity;

// doctrine comments removed
class Document
{
    use TimestampableEntity;
...
}
Run Code Online (Sandbox Code Playgroud)

和时间戳配置是:

services:
    gedmo.listener.timestampable:
        class: Gedmo\Timestampable\TimestampableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]
Run Code Online (Sandbox Code Playgroud)

Timespambable工作得很好.我为可责备的监听器尝试了类似的配置,因为它有一个setUserValue方法:

gedmo.listener.blameable:
    class: Gedmo\Blameable\BlameableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]
        - [ setUserValue, [ @security.token_storage ] ]
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我得到这个错误(4个包是在我的项目中使用的):

在链配置的命名空间中找不到类"Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage"Cf\UserBundle\Entity,Cf\DocumentBundle\Entity,Cf\SouffleBundle\Entity,FOS\UserBundle\Model

我知道它以某种方式将用户ID或安全令牌作为参数丢失,但我无法在任何地方找到示例.那就是我被困住的地方.任何的想法 ?

ChM*_*Mat 12

我还发现很难用StofDoctrineExtensionsBundle启用Blameable行为(让我们假设您正在使用它).

该捆绑包中没有提到一个配置:

# Add in your app/config/config.yml
stof_doctrine_extensions:
    orm:
        default:
            blameable: true
Run Code Online (Sandbox Code Playgroud)

除此之外,我创建了一个BlameableEntity特征:

<?php

namespace AppBundle\Entity\Traits;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use AppBundle\Entity\User;

/**
 * Add Blameable behavior to an entity.
 */
trait BlameableEntity {

    /**
     * @var User
     *
     * @Gedmo\Blameable(on="create")
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
     * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
     */
    protected $createdBy;

    /**
     * @var User
     *
     * @Gedmo\Blameable(on="update")
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
     * @ORM\JoinColumn(name="updated_by", referencedColumnName="id")
     */
    protected $updatedBy;

    /**
     * Set createdBy
     *
     * @param User $createdBy
     * @return Object
     */
    public function setCreatedBy(User $createdBy)
    {
        $this->createdBy = $createdBy;

        return $this;
    }

    /**
     * Get createdBy
     *
     * @return User
     */
    public function getCreatedBy()
    {
        return $this->createdBy;
    }

    /**
     * Set updatedBy
     *
     * @param User $updatedBy
     * @return Object
     */
    public function setUpdatedBy(User $updatedBy)
    {
        $this->updatedBy = $updatedBy;

        return $this;
    }

    /**
     * Get updatedBy
     *
     * @return User
     */
    public function getUpdatedBy()
    {
        return $this->updatedBy;
    }

}
Run Code Online (Sandbox Code Playgroud)

在您的实体中,只需添加如下use语句:

<?php

namespace AppBundle\Entity;

use AppBundle\Entity\Traits\BlameableEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Your precious Foo entity
 *
 * @ORM\Table(name="foo")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\FooRepository")
 */
class Foo
{
    use BlameableEntity;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    // ...
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!