4

Right now, I'd 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;
    // -------------------------------------------------------- //
}

Which is working perfectly as I needed. And it is returning me an array that contains the tables row objects(App_Model_TableName Objects), which will be used later for further operations like save and delete etc.

What I really want is to remove the code that loop through the rows I got from the result set and converting each row to an object of App_Model_TableName that I'd wrote inside the comments // --- //.

Thanks in advance.

Rick James
  • 135,179
  • 13
  • 127
  • 222
Pushpendra
  • 4,344
  • 5
  • 36
  • 64
  • You are using some kind of 3rd party package that has getAdapter, etc. What package is it? The question is really directed at that, not php or mysql. Please add `zend` (or whatever) to the title. – Rick James Jul 31 '15 at 22:06
  • Rick, I am not using any third-party API at all. – Pushpendra Aug 03 '15 at 06:36
  • 1
    What is `Zend`? What is `setFromArray`? – Rick James Aug 03 '15 at 18:57
  • Zend is an object oriented framework implemented in PHP. And I am using Zend Framework 1.x in my project. – Pushpendra Aug 05 '15 at 11:06
  • 1
    I believe he joked :) – Pascut Aug 06 '15 at 12:56
  • I don't quite understand your question. You say the function returns an array of objects, but then you say you want to convert each row into an object. Are they different objects? I also don't see the name written in the comments. – Katrina Aug 06 '15 at 19:29

2 Answers2

2

Firstly, I am assuming you are using PDO.

Try the following

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. You can then access them like:

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

All these awesome functions I found on:

Disclaimer: this code was not tested :(

Be cool but stay warm

  • Sorry, but the project is implemented using Zend framework 1.12. And it doesn't support "FETCH_CLASS" fetch mode. – Pushpendra Aug 05 '15 at 11:16
0

Finally, I had found the 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 had removed the code that do the fetchAll() and loop through each row in the resultset. Now, I am taking each row from the resultset and creating an row of App_Model_TableName object using the data we got from the resultset.

Working perfectly for me.

Pushpendra
  • 4,344
  • 5
  • 36
  • 64