Getting a table object (App_Model_TableName) as a selection result (Zend Framework)

Right now, I wrote a function in my model as:

public function getRowsByZipCode($zip) { // SQL to get all the rows with the given zip code $stmt = $this -> getAdapter() -> query( "SELECT * FROM table_name WHERE table_name.status = 1 AND table_name.zip={$zip}"); $resultRows = $stmt->fetchAll(); // -------------------------------------------------------- // // Convert result set to an array of objects $resultObjects = array(); // If there is atleast one row found in DB if(count($resultRows) > 0) { // Loop throguh all the rows in the resultset foreach($resultRows as $resultRow) { // Create table row and fill it with the details got from DB $h = $this->createRow(); $h->setFromArray($resultRow); // Add to the array $resultObjects[] = $h; } } return $resultObjects; // -------------------------------------------------------- // } 

What works, as I needed. And it returns to me an array containing table row objects ( App_Model_TableName ), which will be used later for further operations, such as saving and deleting , etc.

What I really want is to remove the code that goes through the lines received from the result set and convert each line into the App_Model_TableName object, which I wrote in the comments // --- // .

Thanks in advance.

+4
source share
2 answers

Finally, I found a solution:

 public function getRowsByZipCode($zip) { // SQL to get all the rows with the given zip code $stmt = $this -> getAdapter() -> query( "SELECT * FROM table_name WHERE table_name.status = 1 AND table_name.zip={$zip}"); $resultObjects= array(); while($data = $stmt->fetch()) { $h = $this->createRow(); $h->setFromArray($data); // Add to array $resultObjects[] = $h;; } return $resultObjects; } 

I removed the code that fetchAll () does and iterates over each row in the result set. Now I take each row from the result set and create the row of the App_Model_TableName object using the data obtained from the result set.

It works great for me.

0
source

First, I assume that you are using PDO.

Try to execute

 class App_Model_TableName { public $status; public $zip; // public $other_column; } class YourClass { protected function getAdapter() { // Do adapter stuffs } public function query($query, array $param) { // When Using PDO always use prepare and execute when you pass in a variable // This will help prevent SQL injection $stmt = $this->getAdapter()->prepare($query); return $query->execute($param); } /** * @return App_Model_TableName[] */ public function getRowsByZipCode($zip) { // SQL to get all the rows with the given zip code // This way will help prevent SQL injection $query = "SELECT * FROM table_name WHERE table_name.status = 1 AND table_name.zip = :zip"; $qData = array(':zip' => $zip); $results = $this->query($query, $qData); return $results->fetchAll(PDO::FETCH_CLASS, 'App_Model_TableName'); } } 

Calling YourClass::getRowsByZipCode() will then return you an array of App_Model_TableName objects. Then you can access them, for example:

 $data = $instance_of_yourclass->getRowsByZipCode(12345); foreach ($data as $row) { echo $row->zip; echo $row->do_stuff(); } 

All these amazing features that I found on:

Disclaimer: This code has not been tested :(

Be healthy, but stay warm.

+2
source

All Articles