如何在 symfony 中制作循环或更短的代码
How to make a loop or a shorter code in symfony
我有以下代码
$cp = new Criteria();
$cp->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$cp->add(ProductBrandServicePeer::TYPE_ID, 1);
$cp_count = ProductBrandServicePeer::doCount($cp);
$cs = new Criteria();
$cs->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$cs->add(ProductBrandServicePeer::TYPE_ID, 3);
$cs_count = ProductBrandServicePeer::doCount($cs);
$cb = new Criteria();
$cb->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$cb->add(ProductBrandServicePeer::TYPE_ID, 2);
$cb_count = ProductBrandServicePeer::doCount($cb);
我想缩短它或制作一个循环。
我在想这样的事情:
$cb->add(ProductBrandServicePeer::TYPE_ID, array(1,2,3);
但是我如何获得 cp_count、cs_count 和 cb_count 变量?
$array = array(); // to store the counts
for($i = 1; i <= 3; $i++) {
$c = new Criteria();
$c->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$c->add(ProductBrandServicePeer::TYPE_ID, $i);
$array[$i - 1] = ProductBrandServicePeer::doCount($c); // let's store the count in the array
}
输出:
array(3) {
[0]=>
**cpcount**
[1]=>
**cscount**
[2]=>
**cbcount**
}
要稍后在代码中使用相同的变量,请使用可变变量。 :D
foreach (['cp' => 1, 'cs' => 3, 'cb' => 2] as $var => $typeId) {
$$var->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$$var->add(ProductBrandServicePeer::TYPE_ID, $typeId);
${$var.'_count'} = ProductBrandServicePeer::doCount($$var);
}
我有以下代码
$cp = new Criteria();
$cp->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$cp->add(ProductBrandServicePeer::TYPE_ID, 1);
$cp_count = ProductBrandServicePeer::doCount($cp);
$cs = new Criteria();
$cs->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$cs->add(ProductBrandServicePeer::TYPE_ID, 3);
$cs_count = ProductBrandServicePeer::doCount($cs);
$cb = new Criteria();
$cb->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$cb->add(ProductBrandServicePeer::TYPE_ID, 2);
$cb_count = ProductBrandServicePeer::doCount($cb);
我想缩短它或制作一个循环。 我在想这样的事情:
$cb->add(ProductBrandServicePeer::TYPE_ID, array(1,2,3);
但是我如何获得 cp_count、cs_count 和 cb_count 变量?
$array = array(); // to store the counts
for($i = 1; i <= 3; $i++) {
$c = new Criteria();
$c->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$c->add(ProductBrandServicePeer::TYPE_ID, $i);
$array[$i - 1] = ProductBrandServicePeer::doCount($c); // let's store the count in the array
}
输出:
array(3) {
[0]=>
**cpcount**
[1]=>
**cscount**
[2]=>
**cbcount**
}
要稍后在代码中使用相同的变量,请使用可变变量。 :D
foreach (['cp' => 1, 'cs' => 3, 'cb' => 2] as $var => $typeId) {
$$var->add(ProductBrandServicePeer::BIZORG_ID, $bizorg->getBizorgId());
$$var->add(ProductBrandServicePeer::TYPE_ID, $typeId);
${$var.'_count'} = ProductBrandServicePeer::doCount($$var);
}