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

Getting table object(App_Model_TableName) as fetch result (Zend Framework)

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

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 对象的代码倒是在评论里面写了// --- //.

提前致谢。

首先,我假设您使用的是 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() 然后 return 你将得到一个 App_Model_TableName 对象的数组。然后您可以像这样访问它们:

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

我发现的所有这些很棒的功能:

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

保持凉爽但保持温暖

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

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 对象。

非常适合我。