Querying Records from Tables Using from

You need to give the SQL statement you're using a bit more power and intelligence. The statement needs to know from which table you will fetch data. Using the same Zend_Db_Select object, you'll use the from()

method to identify a table and the_toString() method to convert the SELECT statement you're currently building to a POSQL string. Doing this enables you to compare the object-oriented statement with the intended POSQL.

Listing 5-25 builds on Listing 5-24 by using the from() method to distinguish which table the SQL statement should retrieve data from.

The from() method accepts three parameters:

The initial parameter is the name of the table you want to reference as a string.

• The second parameter is a list of columns. To retrieve all the columns, use the * wildcard symbol; if you want to specify specific columns, use an array. The default value is *.

• The third parameter (optional) is the schema name you want to reference.

Listing5-25. Using Object-Oriented from(): Updated testoostatementAction()

* Test - Object Oriented Select Statement

public function testoostatementAction() {

//Create DB object require_once "Db/Db.php"; $db = Db_Db::conn();

//Create the statement //Select * FROM "artists"; $select = new Zend_Db_Select($db); $statement = $select->from('artists');

//Compare Statement echo $statement->_toString();

//Supress the View


The table is identified by using the from() method and passing in the artists string value. At this point, the full POSQL is the following:

SELECT "artists".* FROM "artists"

You can use the_toString() method to view the generated statement. Load the URL

http://localhost/artist/testoostatement and view the statement.

Was this article helpful?

0 0

Post a comment