任何人都可以提供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;
但createdBy和updatedBy数据库列始终为NULL。
该文档提供了配置其他侦听器的示例(例如我工作的时间戳)但我找不到可责备侦听器的示例或文档。
感谢您的帮助!!
=============================================== ================
是的我添加了以下用途:
use Gedmo\Mapping\Annotation as Gedmo;
我还使用Timestampable和提供的特征:
use Gedmo\Timestampable\Traits\TimestampableEntity;
// doctrine comments removed
class Document
{
use TimestampableEntity;
...
}
,时间戳配置为:
services:
gedmo.listener.timestampable:
class: Gedmo\Timestampable\TimestampableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
Timespambable工作正常。
我为可责备的监听器尝试了类似的配置,因为它有一个setUserValue
方法:
gedmo.listener.blameable:
class: Gedmo\Blameable\BlameableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
- [ setUserValue, [ @security.token_storage ] ]
但它不起作用,我收到此错误(4个捆绑包是我项目中使用的捆绑包):
类' Symfony \ Component \ Security \ Core \ Authentication \ Token \ Storage \ TokenStorage'在链配置的命名空间中找不到Cf \ UserBundle \ Entity,Cf \ DocumentBundle \ Entity,Cf \ SouffleBundle \ Entity,FOS \ UserBundle \ Model
我知道它以某种方式将用户ID或安全令牌作为参数丢失,但我无法在任何地方找到示例。 这就是我被困的地方。有什么想法吗?
答案 0 :(得分:2)
为打开symfony 3.0 +的人们做出的额外贡献:
在“doctrine_extensions.yml”中添加/修改:
gedmo.listener.blameable:
class: Gedmo\Blameable\BlameableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ "@annotation_reader" ] ]
答案 1 :(得分:0)
如果要更新updated_by字段,则必须指定该字段,以便在更新时在updated_by中进行指定。 例如:
/**
* @var \DateTime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime", nullable=true)
*/
protected $updated;
/**
* @var string $updatedBy
*
* @Gedmo\Blameable(on="update", field="updated")
* @ORM\Column(type="string", nullable=true)
*/
protected $updatedBy;
请注意 field =“ updated”
答案 2 :(得分:-1)
如果有人仍然有这个问题,我终于可以按照以下步骤让Gedmo Blameable工作了:
将其添加到doctrine_extensions.yml
gedmo.listener.blameable:
class: Gedmo\Blameable\BlameableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ "@annotation_reader" ] ]
修改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());
}
}
}