'Why the name of trucks are same in 2nd and 3rd record of company data retrieval?

My data in database:

enter image description here

My objective is to retrieve all data of company collection (MongoDB) data from the database. The company collection holds one or many trucks. I have implemented one-to-many reference between company and trucks collection. That is working fine. I am using query builder to get all the data. But, my code is not giving me all the Trucks of the specific company. It is retrieving only one truck name for specific documents. My API code is checking the length of the truck's array and storing the name for the truck on ta[] array. Code is not written nicely as I have tried so many methods and I am frustrated now. How can I retrieve my data?

My API:

 /**
     * @Route("/koco/get/company/query", name="queryToGetCompany")
     * @Template()
     */
    public function queryForCompanyAction()
    {

        $checkarray = array();
        $dm = $this->get('doctrine_mongodb')->getManager();
        $qb = $dm->createQueryBuilder(Company::class);
        $qb->find(Company::class);
        $query = $qb->getQuery();
        $comapnies = $query->execute();
        
        foreach ($comapnies as $company)
        {
            $objectId =  $company->getId();
            $objectName = $company->getName();
            $objectAddress = $company->getAddress();

            // for length
            $len = count($company->getTrucks());
            echo $len;
            // For trucks
            $Trucks = $company->getTrucks();
            foreach ($Trucks as $truckname)
            {

                $ta = array();
                for($i=0;$i< $len;$i++){
                    $object = new Truck();
                    $object =  $truckname->getName();
                    $ta[] = $object;
                 }
            } 


            $checkarray[] = array(
            'Id' => $objectId, 
            'Name' =>$objectName, 
            'Address' =>$objectAddress, 
            'Trucks' => $ta,

        );
        }

        $data = [
            'Comapnies' => $checkarray,
        ]; 
        return new JsonResponse($data);

    }

My results from the API:

enter image description here

The 2nd and third companies are giving me the same records for the name of trucks, but in my database the truck names are different.



Solution 1:[1]

Your foreach and your for loop are colliding, in conjunction with your array being emptied inside the foreach loop. If you reset your array before the foreach loop, not inside, and also just use a foreach without the for, I think this is what you want.

What is happening in your code as presented in the question is that the array is wiped out between trucks, so you only get the last truck. Additionally, because of the manually counted for loop, the number of copies of the last truck is equal to the total number of trucks associated with each company.

This is my suggestion based on what you have shown, replacing the entire foreach loop with this code.

$ta = array();

foreach ($Trucks as $truckname)
{
    $object = new Truck();
    $object =  $truckname->getName();
    $ta[] = $object;
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 colonelclick