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 17: Searching 272<br />

Listing<br />

17-14<br />

// test.php<br />

Listing<br />

17-15<br />

Listing<br />

17-16<br />

// ...<br />

$newsItemTable = <strong>Doctrine</strong>_Core::getTable('NewsItem');<br />

$results = $newsItemTable->search('test');<br />

print_r($results);<br />

The above code executes the following query:<br />

SELECT<br />

COUNT(keyword) AS relevance,<br />

id<br />

FROM article_index<br />

WHERE id IN (SELECT<br />

id<br />

FROM article_index<br />

WHERE keyword = ?)<br />

AND id IN (SELECT<br />

id<br />

FROM article_index<br />

WHERE keyword = ?)<br />

GROUP BY id<br />

ORDER BY relevance DESC<br />

The output of the code above would be the following:<br />

$ php test.php<br />

Array<br />

(<br />

[0] => Array<br />

(<br />

[relevance] => 1<br />

[id] => 1<br />

)<br />

)<br />

Now you can use those results in another query to retrieve the actual NewsItem objects:<br />

Listing<br />

17-17<br />

// test.php<br />

// ...<br />

$ids = array();<br />

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

$ids[] = $result['id'];<br />

}<br />

$q = <strong>Doctrine</strong>_Query::create()<br />

->from('NewsItem i')<br />

->whereIn('i.id', $ids);<br />

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

print_r($newsItems->toArray());<br />

The above example would produce the following output:<br />

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

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

Saved successfully!

Ooh no, something went wrong!