0

I have two tables named as Ingredient and Amount. Each ingredient has a relation column called amounts, that can either contain one or more than one amount objects.

What I am trying to do is to fetch all ingredients in a loop and then fetch the amounts for all of those ingredients as well. However I have noticed that when I use the query to fetch amounts then that slows down the process a lot and I am not able to fetch the amounts for all of the ingredients (900+), the server just times out.

Is there a way that I can fetch all the amounts for a certain ingredient without doing a second for loop? Ultimately what I would want is to return all ingredients along with the amounts that they have.

Here's the code

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);  
     }   

}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there any workaround that might work without adding a pointer?
(if relation between ingredients and amounts are many to many, then I don't know any better approach.) OTHERWISE (my comment): I think adding a pointer will be more efficient. Because pointers don't really take too much storage space and since they point to the ID of the pointed object, and in parse, ID is always indexed, it will be pretty fast to fetch the pointed object. BTW, you can have both relation to amounts and a pointer to ingredients.
relation between them is many to many, can you explain how your code helps in this case? I mean with the relational object of amounts (in ingredient), parse already only queries the relevant amounts. so getting the count doesn't improve the search I think.
I was thinking if I just fetch all of the ingredients and all of the amounts and store them in an array, is there any function that can give back the amount IDs stored in the relational object without doing a fetch? That way I can filter the amounts array. Would containedIn help in this case?
yes, If you just want to get the IDs, then change this ($query1 = $relation->getQuery();) to this ($query1 = $relation->getQuery()->select('objectId')). This way, you only get the id of the Amounts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.