0
votes

I am doing a update on my framework. Previously i am using zf1 zend db select for my data access object class. Now, i would like to make a shift and upgrade to zf2. I have problems translating for the insert, update, select, and delete queries and wonder if someone can shed some light to assist me.

  • What are the new classes that i should use?
  • Does it involve alot of re-coding?
  • Any references will helps alot ( Been looking through stackoverflow, but haven found a comprehensive guide)

Below is my code for insert/update/delete/select for zf1

Insert

$userdata = array('email' => $email,
    'name' => $name,
    'gender' => $gender,
    'location' => $location,
    'fbid' => $fbid,
    'ipaddress' => $ipaddress,
    'jointimestamp'=>new Zend_Db_Expr('NOW()'));      
$this->dbo->insert('users', $userdata);
return $this->dbo->lastInsertId();

Select

if($this->dbo->fetchOne('SELECT id FROM users WHERE username = ?',$username)){
    return true;
}else{
    return false;
}

Update

$userdata = array($field => $value);
$this->dbo->update('user', $userdata, $this->dbo->quoteInto('useremail = ?', $this->user));

Also, does zf2 has fetchall, fetchone, fetchrow methods etc?

Appreciate any advices.

1
Try starting with the Documentation before you ask others to do the work for you ;)Sam
@Sam Thanks figured that out!Slay

1 Answers

0
votes

After reading the documentation, i have come out with the insert/select/update queries for zf2. Below is the code snippet for benefit of those who may need it in future. I am using Zend\Db\Select as a standalone classes for my custom mvc framework.

Adapter

$adapter = new Zend\Db\Adapter\Adapter(array(
            'driver'   => 'pdo_mysql',
            'host' => DB_HOST,
            'database' => DB_PREFIX.DB_NAME,
            'username' => DB_USER,
            'password' => DB_PW
));

Select

$select = $this->sql->select()
            ->from('table')
            ->join('users', 'users.id = table.userid')
            ->order("table.createdtimestamp DESC");
$statement = $this->sql->prepareStatementForSqlObject($select);
$result =  $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($result);
return $resultSet->toArray();

Insert

$insert = $this->sql->insert()
        ->into("messages");
$userdata = array(
         'message' => $message,
         'createdtimestamp'=>new Zend\Db\Sql\Expression('NOW()'),
         'userid' => $userid);    
$insert->values($userdata );    
$statement = $this->sql->prepareStatementForSqlObject($insert);
$result =  $statement->execute();
//return last insert id
return $this->dbo->getDriver()->getLastGeneratedValue();

Update

$update = $this->sql->update()
        ->table('posts')
        ->where(array('pid'=>$pid));
$numbercountarr = array('numbercount' =>  new Zend\Db\Sql\Expression('numbercount+ 1'));
$update->set($numbercountarr );
$statement = $this->sql->prepareStatementForSqlObject($update);
result =  $statement->execute();

To count rows

$statement = $this->sql->prepareStatementForSqlObject($query);
$result =  $statement->execute();
return $result->count();

Hope this can help those who need it save some time.