获取表对象(App_Model_TableName)作为获取结果(Zend Framework)

时间:2015-07-29 11:08:38

标签: php mysql zend-framework zend-db zend-db-table

现在,我在我的模型中写了一个函数:

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

根据我的需要,这是完美的工作。它返回一个包含表行对象( App_Model_TableName 对象)的数组, 稍后将用于进一步操作,如保存和删除 等等。

我真正想要的是删除循环遍历结果集中的行并将每行转换为 App_Model_TableName 的对象的代码,我在评论中写了 // --- //

提前致谢。

2 个答案:

答案 0 :(得分:2)

首先,我假设您正在使用PDO。

尝试以下

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

调用YourClass::getRowsByZipCode()将返回一组App_Model_TableName个对象。然后,您可以访问它们:

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

我发现的所有这些令人敬畏的功能:

免责声明:此代码未经过测试:(

保持凉爽但保持温暖

答案 1 :(得分:0)

最后,我找到了解决方案:

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

我删除了执行fetchAll()的代码并循环遍历结果集中的每一行。现在,我从结果集中获取每一行,并使用从结果集中获取的数据创建一行App_Model_TableName对象。

完美地为我工作。