10.04.2018 Views

Doctrine_manual-1-2-en

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

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

Chapter 16: Behaviors 260<br />

Listing<br />

16-64<br />

SELECT<br />

z.id AS z__id,<br />

z.zipcode AS z__zipcode,<br />

z.city AS z__city,<br />

z.state AS z__state,<br />

z.county AS z__county,<br />

z.zip_class AS z__zip_class,<br />

z.latitude AS z__latitude,<br />

z.longitude AS z__longitude,<br />

((ACOS(SIN(* PI() / 180) * SIN(z.latitude * PI() / 180) + COS(* PI() /<br />

180) * COS(z.latitude * PI() / 180) * COS((- z.longitude) * PI() / 180)) *<br />

180 / PI()) * 60 * 1.1515) AS z__0,<br />

((ACOS(SIN(* PI() / 180) * SIN(z.latitude * PI() / 180) + COS(* PI() /<br />

180) * COS(z.latitude * PI() / 180) * COS((- z.longitude) * PI() / 180)) *<br />

180 / PI()) * 60 * 1.1515 * 1.609344) AS z__1<br />

FROM zipcode z<br />

WHERE z.city != ?<br />

ORDER BY z__0 asc<br />

LIMIT 50<br />

Notice how the above SQL query includes a bunch of SQL that we did not write. This was<br />

automatically added by the behavior to calculate the number of miles betwe<strong>en</strong> records.<br />

Now we can execute the query and use the calculated number of miles values:<br />

Listing<br />

16-65<br />

// test.php<br />

// ...<br />

$result = $q->execute();<br />

foreach ($result as $zipcode) {<br />

echo $zipcode->city . " - " . $zipcode->miles . "";<br />

// You could also access $zipcode->kilometers<br />

}<br />

Get some sample zip code data to test this<br />

http://www.populardata.com/zip_codes.zip 28<br />

Download and import the csv file with the following function:<br />

Listing<br />

16-66<br />

// test.php<br />

// ...<br />

function parseCsvFile($file, $columnheadings = false, $delimiter = ',',<br />

$<strong>en</strong>closure = "\"")<br />

{<br />

$row = 1;<br />

$rows = array();<br />

$handle = fop<strong>en</strong>($file, 'r');<br />

while (($data = fgetcsv($handle, 1000, $delimiter, $<strong>en</strong>closure)) !==<br />

FALSE) {<br />

if (!($columnheadings == false) && ($row == 1)) {<br />

$headingTexts = $data;<br />

28. http://www.populardata.com/zip_codes.zip<br />

----------------- Brought to you by

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

Saved successfully!

Ooh no, something went wrong!