我知道a ArrayCollection是一个包装器Array,但我想知道的是何时选择一个而不是另一个?过度使用ArrayCollections会导致性能下降吗?
我想创建一个表单来编辑我的用户.用户和角色与ManyToMany连接.在UserUsers实体中,我有一个$ roles变量,它是ArrayCollection:
public function __construct()
{
$this->roles = new ArrayCollection();
}
Run Code Online (Sandbox Code Playgroud)
在我的表单上,我想通过多个select表单元素向我的用户添加角色.在我的用户表格中:
public function buildForm( FormBuilderInterface $builder, array $options ) {
$builder->add( 'username' )
->add( 'password', 'repeated', array(
'type' => 'password',
'mapped' => false,
'required' => false,
'first_options' => array(
'label' => 'Password' ),
'second_options' => array(
'label' => 'Repeat Password' ) ) )
->add( 'roles', 'choice', array(
'mapped' => false,
'multiple' => true ) );
}
Run Code Online (Sandbox Code Playgroud)
现在我的多重选择是空的.
如果我将map映射为true,我收到一条错误消息:
UserRoles无法在...中转换为int
我尝试了很多方法,但我无法正确解决这个问题.
想象一下,我有一个数组集合$items,并removeItem在我的SomeEntity实体类中实现了一个方法,
public function removeItem(Item $item)
{
$this->items->removeElement($item);
return $this;
}
Run Code Online (Sandbox Code Playgroud)
以及接收 PATCH 请求的控制器操作:
public function patchAction(Request $request, SomeEntity $entity) {
$form = ...;
$form->handleRequest($request);
if ($form->isValid()) {
...
$this->get('doctrine.orm.entity_manager')->flush();
return $entity;
}
return $form;
}
Run Code Online (Sandbox Code Playgroud)
想象一下,SomeEntity类的实例将项目保存为[0 => {ITEM}, 1 => {ITEM}, 2 => {ITEM}, 3 => {ITEM}](对象的索引数组)。如果您以 JSON 格式向前端项目中getAction的SomeEntity对象发送请求并接收该对象,则对象中这些项目的类型将是对象的索引数组(您可以使用数组方法对其进行迭代),但是如果您删除一个或其中更多来自具有 PATCH 方法的对象并接收 JSON 响应,您将获得一个包含对象的 Object 类型,因为在 PATCH 请求之后某些键已被删除,并且响应中 SomeEntity 类的对象不再包含索引数组,而是会有一个对象的对象。这是因为,当您将数组转换为 json 对象时,您会得到两种不同的结果
此时我正在通过修改(手动重新创建元素)removeItem实体类中的现有方法来解决这个问题,如下所示: …
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)
这两个功能有区别吗?有人告诉我有一个,但我无法找到它是什么,以及一个函数而不是另一个函数将如何影响我的代码
可以说我有以下数组:
my_array = [1, 5, 8, 11, -6]
Run Code Online (Sandbox Code Playgroud)
我需要迭代这个数组并将当前值之前的值相加.一个例子可能更容易理解.我需要返回一个看起来像这样的数组:
final_array = [1, 6, 14, 25, 19]
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情:
my_array.collect {|value| value + previous_values }
Run Code Online (Sandbox Code Playgroud)
但显然这不起作用,因为我无法弄清楚如何获取数组中的先前值.
我是一个编程菜鸟,所以这可能比我做的更容易.我很确定我需要使用收集或注入,但我似乎无法弄清楚如何做到这一点.
任何帮助,将不胜感激.
交换到Flex Array Collection中元素的最佳方法是什么?
我将ArrayCollection绑定为组合框的数据提供者.选择一行,应将对象移动到组合框列表的顶部,并将顶部对象移动到所选对象的位置.
我面临着同样的问题.有人可以用一个例子来解释我吗?
我的代码是:
var dataList:ArrayCollection = new ArrayCollection([{name:"alauddn"}, {name:"ansari"}]);
private function getItemInd(event:MouseEvent):void{
var item:Object = new Object();
item.name = "ansari";
var ias:int = dataList.getItemIndex(item);
Alert.show(ias.toString() + ": " + item.name);
}
Run Code Online (Sandbox Code Playgroud)
但它返回" -1:
我的表格中有一些隐藏的字段.
<ul id="user_roles">
<li><hidden field value="role1"></li>
<li><hidden field value="role2"></li>
(...)
</ul>
Run Code Online (Sandbox Code Playgroud)
我使用jQuery(和数据原型)来添加新角色.
问题是我想渲染这样的东西:
<ul id="user_roles">
<li>role1 <hidden field value="role1"></li>
<li>role2 <hidden field value="role2"></li>
(...)
</ul>
Run Code Online (Sandbox Code Playgroud)
初始渲染没问题:我只是说:
{% for role in roles %}
<li> {{ role }} {{ form_row(role) }} </li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
但是默认数据原型只会呈现{{form_row(role)}}(隐藏字段).
我应该在哪里更改默认数据原型?
form_div_layout.html中没有{%block prototype%}我可以自定义....
有没有办法区分2 ArrayCollection?(例如array_diff)
今天我循环第一个,检查$ it-> contains()是否匹配,但是我认为它可以重构。
我有一个名为"发票"的实体,其中有许多"订阅",因此"发票"和"订阅"处于多对多的关系中.
class Invoice
{
/**
* @ORM\Column(type="integer")
*/
protected $a;
/**
* @ORM\Column(type="integer")
*/
protected $b;
/**
* @ORM\ManyToMany(targetEntity="Subscription", inversedBy="invoices")
*/
protected $subscriptions;
public function __construct()
{
$this->subscriptions = new ArrayCollection();
}
//typical setters and getters
}
Run Code Online (Sandbox Code Playgroud)
使用querybuilder,如何使用subcription上的where子句获取一组匹配的实体?
$subscription = ;//something pulled from the db and is a "subscription" object
$em->createQueryBuilder()
->select('p')
->from('Invoice', 'p')
->where('p.a = :a')
->andWhere('p.b = :b')
->andWhere('p.subscriptions has :subscription') //this line here I need help
->setParameter('a', 1)
->setParameter('b', 2)
->setParameter('subscription', $subscription)
->getQuery()
->getResult();
Run Code Online (Sandbox Code Playgroud)