我有两个名为“成分”和“金额”的表。每种成分都有一个称为数量的关系列,该列可以包含一个或多个数量的对象。
我想做的是循环获取所有成分,然后也获取所有这些成分的量。但是,我注意到,当我使用查询来获取金额时,这会大大减慢该过程,并且我无法获取所有成分(900多种)的金额,服务器只会超时。
有没有一种方法可以获取某种成分的所有金额而无需执行第二次for循环? 最终,我要返回所有成分及其所含的量。
这是代码
try {
$query = new ParseQuery("Ingredient");
$query->descending("name");
$query->limit(1000);
$results = $query->find();
for ($i = 0; $i < count($results); $i++) {
$object = $results[$i];
$ingredient = new Ingredient($object->getObjectId(), $object->get('name'), $object->get('category'), $object->get('KH'), $object->get('EW'),
$object->get('FE'), $object->get('ALK'), $object->get('amounts')->_encode());
array_push($old_values_array, $ingredient);
$relation = $object->get('amounts');
$query1 = $relation->getQuery();
$results1 = $query1->find();
for ($j = 0; $j < count($results1); $j++) {
//echo "Got here in loop 2";
$object2 = $results1[$j];
$id = $object2->getObjectId();
$name = $object2->get('name');
$grams = $object2->get('grams');
$amount = new Amount($id, $name, $grams);
array_push($amounts_array, $amount);
}
}
答案 0 :(得分:0)
one workaround would be to add a pointer to ingredients
in your Amount
parse class. Hence, you can query all 'Amounts' related to that ingredients at once and with one query.
-- If you don't know the max number of amounts for an ingredients, you can first count and the make another query to find them.
$ingredient = ...;
$query= new ParseQuery("Ingredient")->equalsTo('ingredient',$ingredient);
$amountCount=$query->count();
$amounts=$query->limit($amountCount)->find();
apologies if my PHP syntax might be wrong.