Zend Framework 2 Db\Adapter\Adapter query resultset like ZF1

前端 未结 5 1444
广开言路
广开言路 2021-02-08 18:40

Just need a hand understanding some simple database queries in ZF2. In ZF1 I have simple methods like this:

public function recordset()
{
// listing of all reco         


        
5条回答
  •  有刺的猬
    2021-02-08 19:21

    Ok, I think I've got it. At least this will do the job for the time being. Basically, you have to add one extra step and feed the result object into a ResultSet object which has a toArray convenience method. I suppose this could be done a million other ways, but... this works.

    Keep in mind, I wouldn't do this in a controller, or even in this exact way, but its only a test at this point. There's times when I want this available, and this is how ZF2 can do it, if one desired. (never minding good/bad habits)

    In the top of the Controller add/use the ResultSet:

    use Zend\Db\ResultSet\ResultSet;
    

    Here's the working test action:

    public function blaAction()
    {
        $db = new DbAdapter(
            array(
                'driver'        => 'Pdo',
                'dsn'            => 'mysql:dbname=mydb;host=localhost',
                'username'       => 'root',
                'password'       => '',
                )
        );
        $sql = 'select * from customer 
            where cust_nbr > ? and cust_nbr < ?';
        $sql_result = $db->createStatement($sql, array(125000, 125200))->execute();
        if($sql_result->count() > 0){
            $results = new ResultSet();
            $this->view->data = $results->initialize($sql_result)->toArray();
        }
        return $this->view;
    }
    

    toArray is just doing a foreach loop for you, so, I guess its still adding extra array loops I wanted to avoid, but not having looked at ZF1 version of their code, maybe its doing the same anyway.

    What I will probably do is create a simple db wrapper class for Zend\Db that replaces my Zend_Registry statement from ZF1 and adds a fetchAll and fetchOne method, that way I can quickly port over a bunch of ZF1 code to ZF2 much easier.

    Thanks for your input in the comments, I appreciate it. :)

    Oh, I also wanted to mention. I ran into this bridge class someone created, which might also be helpful: https://github.com/fballiano/zfbridge

    EDIT: So the adapter results returned are iterable it turns out. I am not sure what steps I took that led to my confusion, but the results in $db->query are returned as a Pdo\Result object and that can be looped in foreach easy enough. What messed me up was the fact that if you var_dump it,it doesn't show the array data, just the object. That led me down a totally confusing path.

    Now, even though the above works, this is better IMO, because we can take that object, send it where we want for iteration later. (rather than loop over the whole thing to create an array first, only to iterate another loop, waste of time that way)

    Here's a working example I like better. you just loop the object, and there's your data! duh! Not sure how I miss the simple things sometimes. :)

    public function blaAction()
    {
        $db = new DbAdapter(
            array(
                'driver'        => 'Pdo',
                'dsn'            => 'mysql:dbname=gwdb;host=localhost',
                'username'       => 'root',
                'password'       => '',
                )
        );
        $sql = 'select * from customer 
            where cust_nbr > ? and cust_nbr < ?';
    
        $rs = $db->query($sql)->execute(array(125000, 125200));
        // Source of confusion: this doesn't dump the array!!!
        // It dumps the object properties for Pdo\Result
        Debug::dump($rs);
        // but it is still able to iterate records directly
        // without toArray
        foreach ($rs as $row){
            Debug::dump($row);
        }
    
        return $this->view;
    } 
    

提交回复
热议问题