ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-02 19:03:15  阅读:271  来源: 互联网

标签: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对象的代码.

提前致谢.

解决方法:

首先,我假设您正在使用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();
}

我找到的所有这些很棒的功能:

> http://php.net/manual/en/pdostatement.fetchall.php
> http://php.net/manual/en/pdostatement.execute.php

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

保持凉爽,但要保持温暖

标签:php,mysql,zend-framework,zend-db,zend-db-table
来源: https://codeday.me/bug/20190702/1358769.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有