Creating an array of objects within a loop

oop

#1

I've been struggling with this for too long now. It's not making much sense, which usually means it will be a silly obvious mistake that I don't see. Though I have not been using opp for that long yet, so could be missing something.
The problem is in creating an array of objects within a foreach loop using data fetched from a database. The obvious and simpler way would be to just fetch the data straight into a custom class. But I have to treat the objects differently and assign different properties depending on how the database finds the row (whether it is found in the look up table or not).
I'm guessing the problem has to do with how php treats objects as references, but I have not thought up a solution (that works) yet and I'm sure I've been able to do similar before without any issue. So need another to take a look.

Here is a bit of code from a function I tried to begin with.
The var_dumps and echos are just thrown in for debugging.

			$query = "SELECT product_id, quantity, price, total, name, ep_id
				FROM oc_order_product
				LEFT JOIN product_lut ON product_id = oc_id
				WHERE order_id = ?" ;
			$sql = $read->prepare($query) ;
			if(!$sql->execute([$this->order_id])){ return false ;}
			if(!$prods = $sql->fetchAll(\PDO::FETCH_CLASS, 'epos\product')){ return false ;}
			//var_dump($prods); //exit;
			echo 'Count: '.count($prods);
			$tprods = array() ;
			$mprods = array();
			foreach($prods as $k => $prod){
				//echo "<p>Key: $k</p>\n" ; // For Debugging
				$obj = new \StdClass ;
				$tprods = array();
				$mprods = array();
				if($prod->ep_id){	// Is found in the Look up table
					echo "<p>Key: $k</p>\n" ; // For Debugging
					$obj->ProductId = (int)$prod->ep_id ;
					$obj->UnitPrice = (float)$prod->price ;
					$obj->Quantity = (int)$prod->quantity ;
					$tprods[] = $obj ;
					//var_dump($obj);
				}
				else{		// Products not found on Epos go in Misc Items
					$obj->Name = $prod->name ;
					$obj->UnitPrice = (float)$prod->price ;
					$obj->PrintOnOrder = false ;
					for($i = (int)$prod->quantity; $i > 0; $i--){ // One entry for each of quantity
						$mprods[] = $obj ;
					}
				}
			}
			var_dump($tprods); //exit;
			var_dump($mprods); exit;

What I see is $prods is an array with a count of 2, both keys (0 and 1) get echoed. But the array $tprods has only one object, the last one, $mprods is still empty.
So I'm guessing the first object is getting ovewritten by the second instead of appending to the array.

I then tried taking the object creation to separate functions to see if that would break the reference, but no.

			$query = "SELECT product_id, quantity, price, total, name, ep_id
				FROM oc_order_product
				LEFT JOIN product_lut ON product_id = oc_id
				WHERE order_id = ?" ;
			$sql = $read->prepare($query) ;
			if(!$sql->execute([$this->order_id])){ return false ;}
			if(!$prods = $sql->fetchAll(\PDO::FETCH_CLASS, 'epos\product')){ return false ;}
			//var_dump($prods); //exit;
			echo 'Count: '.count($prods);
			$tprods = array() ;
			$mprods = array();
			foreach($prods as $k => $prod){
				//echo "<p>Key: $k</p>\n" ; // For Debugging
				$tprods = array();
				$mprods = array();
				if($prod->ep_id){	// Is found in the Look up table
					echo "<p>Key: $k</p>\n" ; // For Debugging
					$tprods[$k] = $this->prodep($prod) ; // Returns an object
				}
				else{		// Products not found on Epos go in Misc Items
					for($i = (int)$prod->quantity; $i > 0; $i--){ // One entry for each of quantity
						$mprods[] = $this->mprodep($prod) ;	 // Returns an object
					}
				}
			}
			var_dump($tprods); //exit;
			var_dump($mprods); exit;

The results are the same.

Count: 2

Key: 0

Key: 1

...\incl\class\epos\transaction.php:166:
array (size=1)
  1 => 
    object(stdClass)[81]
      public 'ProductId' => int 11219933
      public 'UnitPrice' => float 29.95
      public 'Quantity' => int 1

...\incl\class\epos\transaction.php:167:
array (size=0)
  empty

#2

you reset your result arrays in each iteration cycle, so you can't have more than one object as a result.


#3

Doh!
I knew it had to be something that stupid .:blush:

Been looking at this too long to see it.