Zend Framework - Using SQL directly

Active3 hr before
Viewed126 times

4 Answers


Since these four tasks are so closely related, and generally used together within the same application, Zend\Db\Sql\Sql objects help you create them and produce the result you are attempting to achieve.,Zend\Db\Sql\Sql objects can also be bound to a particular table so that in getting a select, insert, update, or delete object, they are all primarily seeded with the same table when produced.,These are the functions you can call to either produce (a) a prepared statement, or (b) a string to be executed.,Similarly to Select objects, the table can be set at construction time or via into().

load more v

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,I'm currently starting a project from scratch using the Zend Framework. I'm experienced in PHP and SQL, but I've never used a PHP framework before.,Are there any issues with doing it this way? Am I better in the long run going with the Zend way of doing database queries? Is it common for Zend developers to write the SQL themselves rather than using the Zend-style method queries?, Stack Overflow Public questions & answers

I believe your best bet would be to write this in your model:

function myQuery($table, $id) {
   return $this -
      > getDefaultAdapter() -
      > query("SELECT * FROM ? WHERE id = ?",
         array($table, $id)
      ) -
      > fetchAll();

Zend\Db\Sql\Select presents a unified API for building platform-specific SQL SELECT queries. Instances may be created and consumed without Zend\Db\Sql\Sql:,Zend\Db\Sql is a SQL abstraction layer for building platform-specific SQL queries via an object-oriented API. The end result of a Zend\Db\Sql object will be to either produce a Statement and ParameterContainer that represents the target query, or a full string that can be directly executed against the database platform. To achieve this, Zend\Db\Sql objects require a Zend\Db\Adapter\Adapter object in order to produce the desired results.,If you provide a string, this string will be used to create a Zend\Db\Sql\Predicate\Expression instance, and its contents will be applied as-is, with no quoting:,Zend\\Db\\Sql\\Sql objects can also be bound to a particular table so that in obtaining a Select, Insert, Update, or Delete instance, the object will be seeded with the table:

Since these four tasks are so closely related and generally used together within the same application, the Zend\Db\Sql\Sql class helps you create them and produce the result you are attempting to achieve.

use Zend\ Db\ Sql\ Sql;

$sql = new Sql($adapter);
$select = $sql - > select(); // returns a Zend\Db\Sql\Select instance
$insert = $sql - > insert(); // returns a Zend\Db\Sql\Insert instance
$update = $sql - > update(); // returns a Zend\Db\Sql\Update instance
$delete = $sql - > delete(); // returns a Zend\Db\Sql\Delete instance
load more v

Let us now create a database book in the tutorials db using the following SQL command.,Populate the book table with sample data. Use the following SQL command.,This is done by adhering to the following three steps.,Fetch book information using the BookTable's fetchAll() method and register it into the view.

Let us take a look at a simple model – MyModel

< ? php
namespace Tutorial\ Model;
class Book {
   public $id;
   public $author;
   public $title;
load more v

Other "using-undefined" queries related to "Zend Framework - Using SQL directly"