使用Symfony2.8with Doctrine 2.5,我想在Doctrine ORM查询中过滤所有数据集,其中arraycollection恰好包含3个元素.
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository("AppBundle:EduStructItem")
->createQueryBuilder('e')
->addSelect('COUNT(e.preconditions) AS HIDDEN numberpre')
->having('numberpre = 3')
->getQuery();
$res = $query->getResult();
dump($res);
foreach ($res as $entity){
print "title:".$entity->getTitle()."<br>";
dump($entity->getPreconditions()->toArray());
}
Run Code Online (Sandbox Code Playgroud)
preconditions 是一个包含前提条件集合的arraycollection.
最后,我希望得到所有结果正好有3个前提条件.另外,也可以通过arraycollection中的值的数量来排序(类似order by Count(e.preconditions)).
由于使用了另一个捆绑包,我将原则从2.5.2降级到2.5.0.我不认为这是我的问题的原因,但为了完整起见,这是我的作曲家秀的教义部分:
data-dog/pager-bundle v0.2.4 Paginator bundle for symfony2 and doctrine orm, allows customization with filters and sorters
doctrine/annotations v1.2.7 Docblock Annotations Parser
doctrine/cache v1.5.2 Caching library offering an object-oriented API for many cache backends
doctrine/collections v1.3.0 Collections Abstraction library
doctrine/common …Run Code Online (Sandbox Code Playgroud) 在Flex中使用.contains()时ArrayCollection,它将始终查看内存引用.它似乎没有看到.equals()方法或.toString()方法或任何可覆盖的东西.相反,我需要ArrayCollection每次循环并检查每个项目,直到找到我正在寻找的东西.
有谁知道为什么Flex/ActionScript是这样制作的?为什么不提供一种让人们contains()按照自己想要的方式使用方法的方法?

此插件最终包含一个文本字段,其中包含以逗号分隔的标记(例如:tag1,tag2,...)
这些标记目前在非映射表单字段上进行管理:
$builder
// ...
->add('tags', 'text', array(
'mapped' => false,
'required' => false,
))
;
Run Code Online (Sandbox Code Playgroud)
最后,它们存储在ArrayCollection中,因为在数据库字段中存储多个值是一种不好的做法:
/**
* @var ArrayCollection[FiddleTag]
*
* @ORM\OneToMany(targetEntity="FiddleTag", mappedBy="fiddle", cascade={"all"}, orphanRemoval=true)
*/
protected $tags;
Run Code Online (Sandbox Code Playgroud)
要将我的表单映射到我的实体,我可以在我的控制器中执行一些代码,如下所示:
$data->clearTags();
foreach (explode(',', $form->get('tags')->getData()) as $tag)
{
$fiddleTag = new FiddleTag();
$fiddleTag->setTag($tag);
$data->addTag($fiddleTag);
}
Run Code Online (Sandbox Code Playgroud)
但这看起来是错误的方式.
我想知道将我的实体映射到我的表单以及将表单映射到我的实体的最佳做法是什么.
因为我对 Symfony 和 Doctrine 还很陌生,所以我有一个可能很愚蠢的问题;-)
有人可以用简单的话向我解释集合(尤其是实体中的 ArrayCollections)吗?它是什么以及何时以及如何使用它们?(也许在一个简单的例子中)
在文档中无法很好地弄清楚...
提前致谢。
我在标签和文章实体之间存在多对多关系,插入效果很好,但编辑表单(editAction 函数)的创建不起作用。所有代码都在那里:
Article.php
<?php
namespace Diapalema\DiapalemaBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
class Article
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetimetz")
*/
private $created;
/**
* @var \DateTime
*
* @ORM\Column(name="updated", type="datetimetz")
*/
private $updated;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="article", cascade=
{"persist"})
*/
private $auteur;
/**
* BlogArticle …Run Code Online (Sandbox Code Playgroud) 我有一些使用ArrayCollection的代码,它导入:
import mx.collections.ArrayCollection;
Run Code Online (Sandbox Code Playgroud)
然后:
static protected var myAC:ArrayCollection = new ArrayCollection();
Run Code Online (Sandbox Code Playgroud)
看起来很简单,但在我的项目中,找不到/定义了导入(以及ArrayCollection)!
我使用的是Flash Builder 4(Flex 4) - 它是一个ActionScript项目,项目属性设置为Flex SDK 4.0.一切都应该是默认设置.
当我输入"import mx"时.并按控制空间(自动完成),我看到一个选项列表,如核心,几何和其他几个,但没有'集合'.
我肯定错过了什么?
apache-flex actionscript-3 arraycollection flash-builder flex4
Doctrine2何时加载ArrayCollection?
直到我调用一个方法,比如count或getValues,我没有数据
这是我的情况.我有一个与促销实体的OneToMany(双向)关系的委托实体,如下所示:
Promotion.php
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Promotion
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions", cascade={"persist"})
* @ORM\JoinColumn(name="delegation_id", referencedColumnName="id")
*/
protected $delegation;
}
Run Code Online (Sandbox Code Playgroud)
Delegation.php
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Delegation
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Promotion", mappedBy="delegation", cascade={"all"}, orphanRemoval=true)
*/
public $promotions;
public function __construct() {
$this->promotions = new \Doctrine\Common\Collections\ArrayCollection();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我做类似以下的事情(使用给定的委托)
$promotion …Run Code Online (Sandbox Code Playgroud) collections load lazy-evaluation arraycollection doctrine-orm
我使用 Symfony2 制作了一个 Web 应用程序,其中用户具有数组关联 ManytoMany 与实体 Mission。用户可以通过表单上传实体$product,表单传递的数据之一就是与用户关联的任务。
当我尝试上传数据时,出现错误:
ContextErrorException: Catchable Fatal Error: Object of class
Doctrine\ORM\PersistentCollection could not be converted to string in
C:\BitNami\wampstack-5.4.23-
0\frameworks\symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 103
Run Code Online (Sandbox Code Playgroud)
很明显,Doctrine 不知道如何保存任务的价值。
我该如何管理?
我也不知道如何在我的产品实体中声明任务对象。现在就是这样:
/**
* @var string
*
* @ORM\Column(name="mission", type="string", length=255)
*/
protected $mission;
Run Code Online (Sandbox Code Playgroud)
更新 - -
我的控制器现在是:
$form = $this->createFormBuilder($product)
->add('name', 'text')
->add('mission', 'entity', array('required' => true, 'multiple' => false, 'class' => 'AcmeManagementBundle:Mission', 'query_builder' => function($repository) { return $repository->createQueryBuilder('c')->orderBy('c.id', 'ASC'); },))
//...
->add('save', 'submit')
->getForm();
Run Code Online (Sandbox Code Playgroud)
更新 - -
现在工作,但我有一个问题。当出现上传 …
我试图使用方法ArrayCollection :: contains来查找对象是否已经在我的Collection中,但是当我在做什么时:
//My ArrayCollection
$lesRoles = $drt->getDrtApplication()->getRoles();
$leRole = $lesRoles->first();
echo "Property appNom : ".$leRole->getRleApplication()->getAppNom()."// Property appRole : ".$leRole->getRleId()." <br>";
$role = new \Casgaya\Role(2,$drt->getDrtApplication());
echo "Property appNom : ".$role->getRleApplication()->getAppNom()."// Property appRole : ".$role->getRleId()." <br>";
var_dump($lesRoles->contains($role));
Run Code Online (Sandbox Code Playgroud)
结果是:
属性appNom:CORA //属性appRole:2
属性appNom:CORA //属性appRole:2
bool(false)
由于appNom和rleId是实体Role拥有的我跳过的唯一两个属性,它将返回true.
编辑新测试案例:
echo "Test object role : <br>";
var_dump($lesRoles==$role);
echo"<br>";
echo "Test integer property rleID from object role : <br>";
var_dump($role->getRleId() == $leRole->getRleId());
echo"<br>";
echo "Test Application object property RleApplication from object role : <br> ";
var_dump($role->getRleApplication() == …Run Code Online (Sandbox Code Playgroud) public function getInvoiceItemsByType($type)
{
return $this->invoiceItems->filter(function ($invoice) use ($type) {
/** @var InvoiceItem $invoice */
return $invoice->getType() == $type;
}
);
}
public function getInvoiceItemsByType($type) {
foreach ($this->invoiceItems as $invoice) {
if ($invoice->getType() == $type) {
return $invoice;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
这两个功能有区别吗?有人告诉我有一个,但我无法找到它是什么,以及一个函数而不是另一个函数将如何影响我的代码
arraycollection ×10
symfony ×6
doctrine-orm ×4
php ×4
apache-flex ×2
doctrine ×2
collections ×1
contains ×1
edit ×1
equality ×1
flex4 ×1
forms ×1
load ×1
orm ×1