Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework: Proper way to interact with database?

I'm fairly new to the Zend Framework and MVC and I'm a bit confused by Zend_DB and the proper way to interact with the database.

I'm using the PDO MySQL adapter and have created some classes to extend the abstract classes:

class Users extends Zend_Db_Table_Abstract {
    protected $_name = 'users';
    protected $_primary = 'user_id';
    protected $_rowClass = 'User';

    public function getUserbyID($id) { /* code */ }
    // More code here
}
class User extends Zend_Db_Table_Row_Abstract {
    // Code here
}
class Widgets extends Zend_Db_Table_Abstract {
    protected $_name = 'widgets';
    protected $_rowClass = 'Widget';

    public function getWidgetsfromUser($userid) { /* code */ }
    // More code here
}
class User extends Zend_Db_Table_Row_Abstract {
    public function doSomethingWithWidget() { /* code */ }
    // More code here
}

There seems to be so many ways to access the DB (fetchAll(), find(), fetchAll() through adapter, insert(), createRow() and save(), select() object) that I always find myself going back to the docs to figure out what I should be doing.

SO has taught me prepared statements are the way to go, and I've been trying to use rowsets and row (should I be?), but I'm still confused as to what's the best way to interact with the database?

(apologies for the terribly open ended question)

like image 276
Gilean Avatar asked Jan 15 '09 02:01

Gilean


People also ask

How do I connect to a database in Zend Framework 2?

$adapter = $this->getServiceLocator()->get('adapter'); Create a sql statetment and put in variable $sql. Now do this: $statement = $adapter->createStatement($sql); $result = $statement->execute();

Is Zend Framework Good?

When it comes to PHP frameworks, Zend is counted among the best. Zend Framework offers lots of benefits for creating feature-rich and dynamic web solutions. MVC features and a strong component library have made Zend a popular PHP framework for creating a myriad of web solutions.

What is the use of Zend framework in PHP?

Zend Framework is a collection of professional PHP packages with more than 570 million installations. It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.

Is Zend a PHP Framework?

Zend Framework is a collection of 60+ packages for professional PHP development. Each package is available on GitHub and can be installed via Composer.


1 Answers

In general, people prefer to access the database through the Table and Row objects, to match their habits of object-oriented programming.

The OO approach is useful if you need to write code to transform or validate query inputs or outputs. You can also write custom methods in a Table or Row class to encapsulate frequently-needed queries.

But the object-oriented interface is simplified, unable to perform all the types of database operations you might need to do. So you can delve deeper and run a SQL query against the Zend_Db_Adapter methods like query() and fetchAll() when you require finer control over your SQL.

This is pretty common for object-oriented interfaces to databases. An OO layer that could duplicate every SQL feature would be insanely complex. So to compromise, an OO layer usually tries to provide easy ways to do the most common tasks, while giving you the ability to go under the covers when necessary.

That's a very general answer to your very general question.

like image 76
Bill Karwin Avatar answered Sep 30 '22 23:09

Bill Karwin