- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个场景
class Foo
{
/**
* @ORM\Id
* @ORM\Column(type="date")
*/
protected $date;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity"Bar", inversedBy="foo")
*/
protected $bar;
[...]
}
class FooRepository extends EntityRepository
{
public function dummyNameFunction()
{
$q = $this->createQueryBuilder('foo')
->//some dummy "components" added to builder
->getQuery()
;
//I need an ArrayCollection as I want to take advantage of
//Doctrine's ArrayCollection filter() function
return new ArrayCollection($q->getResult());
}
}
只要我尝试从数据库中获取数据,我就会得到这个错误
ContextErrorException: Catchable Fatal Error: Object of class DateTime could not be converted to string in /path/to/project/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 2504
对我来说这是“非常清楚”:从对象的Array
到ArrayCollection
的转换,Doctrine 想索引全新的ArrayCollection
具有实体的 id,但由于键是日期时间,PHP 本身没有提供 __toString()
方法(或等效方法)。
我想到的可能的解决方案:
$date
和 $bar
中删除 id 并设置约束以使其唯一。缺点:我有一个完全没用的文件。Date
。缺点:我现在不知道这是否有效,因为我还没有尝试过。首先,您需要定义一个 Doctrine2 服装类型。你怎么做到的?
我在我的包中创建了一个文件夹(以保持 Doctrine2 原始包结构的安全)在 Doctrine\DBAL\Types
。然后,在里面,我创建了这个类
<?php
namespace Company\BundleName\Doctrine\DBAL\Types;
use Doctrine\DBAL\Types\DateType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class CustomDate extends DateType
{
private $date_value;
public function getName()
{
return 'custom_date';
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
$date = parent::convertToPHPValue($value, $platform);
$this->date_value = $date;
return $date->format('Y-m-d');
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return ($value !== null)
? $value : null;
}
}
然后我添加了一个 boot() 方法来捆绑 Bootstrap 文件(您需要在 AppKernel.php 中注册捆绑的文件)
public function boot()
{
$em = $this->container->get('doctrine.orm.entity_manager');
Type::addType('custom_date', 'Company\BundleName\Doctrine\DBAL\Types\CustomDate');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('custom_date','custom_date');
}
之后我进行了修改。 Foo 的 $date
变量的映射
/**
* @ORM\Id
* @ORM\Column(type="custom_date")
*/
protected $date;
最佳答案
似乎唯一的解决方案是创建一个全新的学说类型(我在这里称之为“肮脏的解决方案”),因为 UnitOfWork 需要字符串或整数才能正常工作。
关于symfony - 从数组到 Doctrine/ArrayCollection : entity id (date) issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26888635/
我扩展了 ArrayCollection 类以添加推送方法 package com.cargo.collections { import mx.collections.ArrayCollect
我使用 Symfony2 制作了一个 Web 应用程序,其中用户与实体 Mission 具有数组关联 ManytoMany。用户可以通过表单上传实体$product,表单传递的数据之一是与用户关联的任
我用 Symfony2 制作了一个 Web 应用程序,其中一个 User 有一个数组关联 ManytoMany 与实体 Mission。用户可以通过表单上传实体 $product,表单传递的数据之一是
我在使用 tagsinput 的表单上使用标签: 此插件以单个文本字段结束,其中包含以逗号分隔的标签(例如:tag1,tag2,...) 这些标签目前在非映射表单字段上进行管理: $build
我正在寻找一种为 ODM ArrayCollection 关联数组键的方法。 实体具有以下映射: /** * $fields * * The entities fields * * @ODM
我正试图在我的包中实现一些可重用的翻译。这些是我的代码的相关部分: TranslatorKeys.php: ... /** * @var \ArrayCollection * * @ORM\On
好的,我有一个 User 实体如下 urls = new \Doctrine\Common\Collections\ArrayCollection(); } public functi
我想在 Flex 中迭代 ArrayCollection,同时可以添加和删除项目。 因为我没有找到一种方法来使用像 Java 中那样的“经典”迭代器来完成这项工作。我试过游标。但它并没有真正按照我想要
如果我有一个 ArrayCollection 类型的变量,如何检查集合中是否存在特定名称的键(包括嵌套)。如果确实如此,我如何获取和更改该值? 最佳答案 我猜你正在谈论 Doctrines Array
如果我有一个 ArrayCollection 类型的变量,如何检查集合中是否存在特定名称的键(包括嵌套)。如果确实如此,我如何获取和更改该值? 最佳答案 我猜你正在谈论 Doctrines Array
在对我的数据提供者(数组集合)应用数字排序后,我无法通过 tilelist 重新排序项目。我需要从 arrayCollection 中删除排序吗?如果是这样,是否只是设置 collection.sor
我想创建一个表单来编辑我的用户。与 ManyToMany 关联的用户和角色。在 UserUsers 实体中,我有一个 $roles 变量,它是 ArrayCollection: public func
通常您可以使用以下方法检查变量是否是类的实例: $foo instanceof bar 但是对于 ArrayObjects(属于 Symfony 2),这似乎不起作用 get_class($foo)
即使对象被正确更新,数据似乎也没有持久化,我不明白为什么。 这是我的实体: Article.php /** * @var AttributeInArticle * * @ORM\OneToMan
public function getInvoiceItemsByType($type) { return $this->invoiceItems->filter(function ($inv
考虑以下问题: 我在 Article 和 Author 之间有一个多对一 关联。 class Author{ /** * ... *@var ArrayCollection_of_Ar
我正在尝试按特定字段对 ArrayCollection 进行排序。 ArrayCollection 是一个类(class)数组。在 Course 实体中有一个名为 isLive 的方法,它返回一个 b
我有一些这样的 XML 结构: var struct:XML = ; 我知道我可以通过“id”访问子节点,方式如下: var stuff:Object = struct.(hasO
我正在使用 JMS 序列化程序。当 JsonSerializer 与 Doctrine ArrayCollection 类型一起工作时,它给了我不正确的数组格式。指定的结果应遵循格式 [ {}, {}
在设置了 filterFunction 的 ArrayCollection 上调用 getItemIndex 时遇到了一些问题。 我做了类似 myAC.removeItemAt(myAC.getIte
我是一名优秀的程序员,十分优秀!