21.10.2015 Views

1-33

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Symfony2 – Franz Jordán 2011<br />

function close_database_connection($link)<br />

{<br />

mysql_close($link);<br />

}<br />

function get_all_posts()<br />

{<br />

$link = open_database_connection();<br />

$result = mysql_query('SELECT id, title FROM post', $link);<br />

$posts = array();<br />

while ($row = mysql_fetch_assoc($result)) {<br />

$posts[] = $row;<br />

}<br />

close_database_connection($link);<br />

return $posts;<br />

}<br />

The filename model.php is used because the logic and data access of an application is<br />

traditionally known as the "model" layer. In a well-organized application, the majority of the<br />

code representing your "business logic" should live in the model (as opposed to living in a<br />

controller). And unlike in this example, only a portion (or none) of the model is actually<br />

concerned with accessing a database.<br />

The controller (index.php) is now very simple:<br />

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

Saved successfully!

Ooh no, something went wrong!