我已将此行添加到我的composer.json中:
"gedmo/doctrine-extensions": "dev-master"
Run Code Online (Sandbox Code Playgroud)
这是在我的模块的module.config.php中:
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
然后我想在我的实体中使用带时间戳的注释,例如:
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime",nullable=true)
*/
private $created;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime",nullable=true)
*/
private $updated;
Run Code Online (Sandbox Code Playgroud)
但这不起作用.当我使用上面的注释持久化实体时,创建和更新的列为NULL.
我创建了一个具有Sortable行为的实体,并且有一个关于使用它的问题.
设置和获取位置的方法对我来说还不够,所以我想用以下代码做简单的
moveUp和moveDown方法:
public function moveUp() {
++$this->position;
}
public function moveDown() {
if($this->position != 0)
--$this->position;
}
Run Code Online (Sandbox Code Playgroud)
通过此实现,moveUp方法没有限制使用已经最大位置递增项目.禁止递增此类物品的最佳方法是什么?我听说直接在实体中进行自定义查询不是一个好习惯,那么如何检查项目是否已经具有最大值?
对于开发,我们有一个Symfony控制台命令,用于执行其他控制台命令以重建db,运行fixture等.
作为该过程的一部分,我需要运行一些樱桃挑选的学说迁移命令,但由于某种原因,我无法在同一进程中运行多个执行命令.
为了确认,我可以手动运行这些任务,并且可以在控制台执行中运行其中一个命令,然后手动运行另一个没有问题.
$this->getApplication()->run(new ArrayInput(array(
'command' => 'doctrine:migrations:execute',
'version' => '20140310162336',
'--no-interaction' => true
)), $output);
$this->getApplication()->run(new ArrayInput(array(
'command' => 'doctrine:migrations:execute',
'version' => '20140310170437',
'--no-interaction' => true
)), $output);
Run Code Online (Sandbox Code Playgroud)
返回的错误是:
[Doctrine\DBAL\Migrations\MigrationException]
Migration version 20140310162334 already registered with class Doctrine\DBAL\Migrations\Version
Run Code Online (Sandbox Code Playgroud)
作为第一个存在的版本文件的版本可以确认一个不在migration_versions表中,也不是在这种情况下需要.建议将其加载到迁移对象中.
任何人都可以提供输入,如果我做错了,如果这可能是某个地方的错误.
使用dev-master运行Symfony 2.2.*和迁移包.
我可以覆盖标记对象序列化的方式吗?目前返回所有内容,我想排除id,created_at,updated_at和标记.我正在使用JMS Serializer软件包,Doctrine Extensions Taggable和FPN Tag Bundle.
这是我的设置,我想在实体的命名空间实际上是DoctrineExtensions可能是问题时将Tag Bundle的父设置为FPN.
大多数实体参数都在DoctrineExtensions\Taggable\Entity\Tag(id,name,created_at等)中.我正在覆盖扩展DoctrineExtensions的FPN包.DoctrineExtensions是一个库而不是一个包.
我怎样才能做到这一点?
# app/config/config.yml
# ...
jms_serializer:
metadata:
auto_detection: true
directories:
TagBundle:
namespace_prefix: "FPN\\TagBundle"
path: "@MYTagBundle/Resources/config/serializer/fpn"
# MY\TagBundle\Resources\config\serializer\fpn\Entity.Tag.yml
FPN\TagBundle\Entity\Tag:
exclusion_policy: ALL
properties:
id:
expose: false
name:
expose: true
created_at:
expose: false
updated_at:
expose: false
tagging:
expose: false
# src/MY/TagBundle/Entity/Tag.php
<?php
namespace MY\TagBundle\Entity;
use FPN\TagBundle\Entity\Tag as BaseTag;
class Tag extends BaseTag
{
}
# vendor/fpn/tag-bundle/FPN/TagBundle/Entity/Tag.php
<?php
namespace FPN\TagBundle\Entity;
use DoctrineExtensions\Taggable\Entity\Tag as BaseTag;
class Tag extends BaseTag
{
....
}
# src/MY/TagBundle/MYTagBundle.php
<?php
namespace …Run Code Online (Sandbox Code Playgroud) 我正在使用" knp Doctrine Translatable "来翻译实体.到目前为止工作得很好.现在,我想要一个通用的解决方案,适用于任何数量的语言.所以我想使用一个嵌入式表单(Collections)来处理实体的Translatables.现在一切正常,除了为了添加新的翻译而没有设置translatable_id.任何人都试图实现这一目标吗?我只是想知道是否有更简单的方法来做到这一点,以避免过于复杂的事情.
到目前为止,非常好,这里是我的类型,这样你就可以更好地理解架构了.
// Main type that has a linkTranslationType with the translations
class linkType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' => 'Name'
))
->add('translations', 'collection', array(
'type' => new linkTranslationType(),
'label' => false,
'allow_add' => true,
'allow_delete' => true
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Link'
));
}
}
Run Code Online (Sandbox Code Playgroud)
这是LinkTranslationType,每种语言呈现为"一行":en_EN Anchor http // url/en
class linkTranslationType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, …Run Code Online (Sandbox Code Playgroud) 我正在使用stof/StofDoctrineExtensionsBundle(Atlantic18/DoctrineExtensions 的捆绑包装器)来实现嵌套集(树)实体。该实体已配置并正常工作,但我无法弄清楚如何在单个查询中检索所有子项(完整树)的所有根音符。我目前有完整的集合返回,但它延迟加载所有子项,这意味着执行了大量查询。
谢谢你的帮助。
nested-sets doctrine-orm doctrine-extensions stofdoctrineextensions
在Gedmo Doctrine Extensions的可翻译行为中,有关于“个人翻译”的说明。有人可以澄清什么是个人翻译吗?
当使用StofDoctrineExtensions(这是Gedmo Doctrine Extensions的Symfony2端口)可排序行为时,我一直收到此错误:
此存储库只能附加到ORM可排序侦听器
由于我无法在官方文档中轻易找到答案,因此我将在此留下答案以供将来参考.
在Symfony2项目中,我正在使用Loggable Doctrine Extension.
我看到有一个LoggableListener.
当可记录实体中的(可记录)字段发生变化时,确实会触发事件吗?如果是这样,有没有办法获得触发它的字段列表?
我想象一个实体的情况,比方说10个字段,其中3个可记录.对于3中的每一个,如果它们改变了值,我想执行一些操作,因此如果它们中的3个改变,将执行3个动作.
任何的想法?
谢谢!
编辑
阅读下面的评论并阅读关于学说的事件的文档,我理解有3个选项:
1)如果我使用的是doctrine> 2.4,那么即使使用参数,也可以直接在实体级使用生命周期回调
2)我可以收听并订阅Lifecycle Events,但在这种情况下,文档会说"Lifecycle events are triggered for all entities. It is the responsibility of the listeners and subscribers to check if the entity is of a type it wants to handle."
3)做你建议的,使用实体监听器,你可以在实体级别定义哪个是将"附加"到类的监听器.
即使第一个解决方案看起来更容易,我也会读到"你也可以使用这个监听器来实现已经改变的所有字段的验证.当使用昂贵的验证来调用时,这比使用生命周期回调更有效".什么被认为是"昂贵的验证?".
在我的情况下,我必须执行的是"如果实体Y的字段X发生了变化,而不是在通知表上添加通知"用户Z将X(Y)的值从A更改为B"
哪个是最合适的方法,考虑到我有大约1000个这样的领域?
EDIT2
为了解决我的问题,我正在尝试在监听器中注入service_container服务,这样我就可以访问调度程序来调度一个新事件,该事件可以执行我需要的新实体的持久化.但是我该怎么做呢?
我尝试了通常的方法,我将以下内容添加到service.yml
app_bundle.project_tolereances_listener:
class: AppBundle\EventListener\ProjectTolerancesListener
arguments: [@service_container]
Run Code Online (Sandbox Code Playgroud)
当然我向听众添加了以下内容:
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
Run Code Online (Sandbox Code Playgroud)
但我得到以下内容:
Catchable …Run Code Online (Sandbox Code Playgroud) 我有一个 Symfony 2.8 应用程序,它使用Gedmo\Timestampable自动created_at和操作的注释,但它似乎在首次创建实体/行时updated_at将相同的时间戳放入列中。updated_atAnUPDATE确实会导致该updated_at列显示较新的时间戳,但我希望它一开始就为空白。
我的理解是,在 上INSERT,只有created_at列应该填充时间戳,并且updated_at应该保持空白,因为它尚未更新。只有在该列第一次更新后才可以获得更新时间的值。
我的预期正确吗?我的代码/配置有问题吗?或者这个注释实际上是否按设计设置了两列的值?
我的config.yml:
...
stof_doctrine_extensions:
default_locale: "%locale%"
translation_fallback: true
orm:
default:
translatable: true
timestampable: true
...
Run Code Online (Sandbox Code Playgroud)
我的实体:
...
/**
* An automatic timestamp of the creation.
*
* @var \DateTime $createdAt
* @Gedmo\Timestampable(on="create")
* @ORM\Column(name="created_at", type="datetime")
*/
public $createdAt;
/**
* An automatic timestamp of the updation.
*
* @var \DateTime $updatedAt
* @Gedmo\Timestampable(on="update")
* …Run Code Online (Sandbox Code Playgroud) 在本地主机中,一切正常,但在部署项目后,我收到此错误
[Semantical Error] The annotation "@Gedmo\Mapping\Annotation\slug" in property
AppBundle\Entity\Product::$slug does not exist, or could not be auto-loaded.
Run Code Online (Sandbox Code Playgroud)
这是产品类
use Gedmo\Mapping\Annotation as Gedmo;
abstract class Prodcut
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
* @Gedmo\slug(fields={"name"})
* @ORM\Column(name="slug", type="string", length=255, unique=true)
*/
private $slug;
Run Code Online (Sandbox Code Playgroud)