12.07.2015 Views

A Gentle Introduction to symfony (pdf) - Bad Request

A Gentle Introduction to symfony (pdf) - Bad Request

A Gentle Introduction to symfony (pdf) - Bad Request

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix A: Inside The Model Layer (Propel) 367Even after calling the delete() method, an object remains available until the end of therequest. To determine if an object is deleted in the database, call the isDeleted()method.Retrieving Records by Primary KeyIf you know the primary key of a particular record, use the retrieveByPk() class method ofthe peer class <strong>to</strong> get the related object.$article = ArticlePeer::retrieveByPk(7);ListingA-14The schema.yml file defines the id field as the primary key of the blog_article table, sothis statement will actually return the article that has id 7. As you used the primary key, youknow that only one record will be returned; the $article variable contains an object of classArticle.In some cases, a primary key may consist of more than one column. In those cases, theretrieveByPK() method accepts multiple parameters, one for each primary key column.You can also select multiple objects based on their primary keys, by calling the generatedretrieveByPKs() method, which expects an array of primary keys as a parameter.Retrieving Records with CriteriaWhen you want <strong>to</strong> retrieve more than one record, you need <strong>to</strong> call the doSelect() method ofthe peer class corresponding <strong>to</strong> the objects you want <strong>to</strong> retrieve. For instance, <strong>to</strong> retrieveobjects of class Article, call ArticlePeer::doSelect().The first parameter of the doSelect() method is an object of class Criteria, which is asimple query definition class defined without SQL for the sake of database abstraction.An empty Criteria returns all the objects of the class. For instance, the code shown inListing 8-12 retrieves all the articles.Listing 8-12 - Retrieving Records by Criteria with doSelect()—Empty Criteria$c = new Criteria();$articles = ArticlePeer::doSelect($c);ListingA-15// Will result in the following SQL querySELECT blog_article.ID, blog_article.TITLE, blog_article.CONTENT,blog_article.CREATED_ATFROM blog_article;HydratingThe call <strong>to</strong> ::doSelect() is actually much more powerful than a simple SQL query. First,the SQL is optimized for the DBMS you choose. Second, any value passed <strong>to</strong> the Criteriais escaped before being integrated in<strong>to</strong> the SQL code, which prevents SQL injection risks.Third, the method returns an array of objects, rather than a result set. The ORMau<strong>to</strong>matically creates and populates objects based on the database result set. This processis called hydrating.For a more complex object selection, you need an equivalent of the WHERE, ORDER BY,GROUP BY, and other SQL statements. The Criteria object has methods and parametersfor all these conditions. For example, <strong>to</strong> get all comments written by Steve, ordered by date,build a Criteria as shown in Listing 8-13.----------------- Brought <strong>to</strong> you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!