使用Symfony2配置可归咎于学说扩展



任何人都可以提供一个怪异的Gedmo扩展程序的完整示例,尤其是可任的侦听器的配置?

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

 * @var User $createdBy
 *
 * @GedmoBlameable(on="create")
 * @ORMManyToOne(targetEntity="CfUserBundleEntityUser")
 * @ORMJoinColumn(name="createdBy", referencedColumnName="id")
 */
private $createdBy;
/**
 * @var User $updatedBy
 *
 * @GedmoBlameable(on="update")
 * @ORMManyToOne(targetEntity="CfUserBundleEntityUser")
 * @ORMJoinColumn(name="updatedBy", referencedColumnName="id")
 */
private $updatedBy;

但是创建的数据库列和更新始终为空。

该文档提供了配置其他侦听器的示例(例如,我可以使用的时间戳),但我没有找到可归咎于侦听器的示例或文档。

感谢您的任何帮助!

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

编辑以回答让:

是的,我添加了使用:

use GedmoMappingAnnotation as Gedmo;

我还将时间戳与提供的特征:

use GedmoTimestampableTraitsTimestampableEntity;
// doctrine comments removed
class Document
{
    use TimestampableEntity;
...
}

,可时间通知的配置为:

services:
    gedmo.listener.timestampable:
        class: GedmoTimestampableTimestampableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]

TimePambable的作品很好。我尝试了可归咎于侦听器的类似配置,因为它具有setUserValue方法:

gedmo.listener.blameable:
    class: GedmoBlameableBlameableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]
        - [ setUserValue, [ @security.token_storage ] ]

但是它不起作用,我会遇到此错误(4个捆绑包是我项目中使用的错误):

类'Symfony Component Component Security core authentication token storage tokenStorage'在链条配置的命名空间CF userBundle Entity,cf documentBundle Entity,cf soufflebundle entity,fos entity,fos entity,cf userbundle entity,cf soufflebundle entity,fos,fos,fos,fos,fos,UserBundle Model

我知道它以一种或另一种方式丢失了用户ID或安全令牌作为参数,但在任何地方都找不到示例。那就是我卡住的地方。有什么想法吗?

我还发现很难用stofdoctrineextensionsbundle启用可疑的行为(让我们假设您正在使用它)。

该捆绑包中没有提及一件配置:

# Add in your app/config/config.yml
stof_doctrine_extensions:
    orm:
        default:
            blameable: true

除此之外,我创建了一个命运的特征:

<?php
namespace AppBundleEntityTraits;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
use AppBundleEntityUser;
/**
 * Add Blameable behavior to an entity.
 */
trait BlameableEntity {
    /**
     * @var User
     *
     * @GedmoBlameable(on="create")
     * @ORMManyToOne(targetEntity="AppBundleEntityUser")
     * @ORMJoinColumn(name="created_by", referencedColumnName="id")
     */
    protected $createdBy;
    /**
     * @var User
     *
     * @GedmoBlameable(on="update")
     * @ORMManyToOne(targetEntity="AppBundleEntityUser")
     * @ORMJoinColumn(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;
    }
}

在您的实体中,只需添加一个use语句:

<?php
namespace AppBundleEntity;
use AppBundleEntityTraitsBlameableEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
 * Your precious Foo entity
 *
 * @ORMTable(name="foo")
 * @ORMEntity(repositoryClass="AppBundleEntityFooRepository")
 */
class Foo
{
    use BlameableEntity;
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    // ...

我希望这会有所帮助!

对人打开Symfony 3.0 :

的小额外贡献

在" doctrine_extensions.yml"中添加/修改:

gedmo.listener.blameable:
    class: GedmoBlameableBlameableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ "@annotation_reader" ] ]

如果要更新更新的_by字段,则必须指定字段,以便在更新时,请在updateed_by中执行此操作。例如:

/**
 * @var DateTime $updated
 *
 * @GedmoTimestampable(on="update")
 * @ORMColumn(type="datetime", nullable=true)
 */
protected $updated;
/**
 * @var string $updatedBy
 *
 * @GedmoBlameable(on="update", field="updated")
 * @ORMColumn(type="string", nullable=true)
 */
protected $updatedBy;

请注意 field ="更新"

如果某人仍然遇到这个问题,我终于可以按照以下步骤获得gedmo可做的工作:

  1. 遵循本指南https://github.com/atlantic18/doctrineextensions/blob/master/doc/symfony2.md
  2. 将此添加到Doctrine_extensions.yml

    gedmo.listener.blameable:
      class: GedmoBlameableBlameableListener
      tags:
        - { name: doctrine.event_subscriber, connection: default }
      calls:
        - [ setAnnotationReader, [ "@annotation_reader" ] ]
    
  3. 修改OnkernelRequest方法如下:

    public function onKernelRequest(GetResponseEvent $event) {
        if (Kernel::MAJOR_VERSION == 2 && Kernel::MINOR_VERSION < 6) {
            $securityContext = $this->container->get('security.context', ContainerInterface::NULL_ON_INVALID_REFERENCE);
            if (null !== $securityContext && null !== $securityContext->getToken() && $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                $loggable = $this->container->get('gedmo.listener.loggable');
                $loggable->setUsername($securityContext->getToken()->getUsername());
            }
        }
        else {
            $tokenStorage = $this->container->get('security.token_storage')->getToken();
            $authorizationChecker = $this->container->get('security.authorization_checker');
            if (null !== $tokenStorage && $authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                $loggable = $this->container->get('gedmo.listener.loggable');
                $loggable->setUsername($tokenStorage->getUser());
                $blameable = $this->container->get('gedmo.listener.blameable');
                $blameable->setUserValue($tokenStorage->getUser());
            }
        }
    }
    

相关内容

  • 没有找到相关文章

最新更新