<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code@HexFlux &#187; controller</title>
	<atom:link href="http://code.hexflux.com/tag/controller/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.hexflux.com</link>
	<description>yet another web application developer using cakephp &#124;&#124; django</description>
	<lastBuildDate>Thu, 01 Apr 2010 10:43:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Easier Search&#124;&#124;Filtering With Controller::postConditions()</title>
		<link>http://code.hexflux.com/2009/06/easier-searchfiltering-with-controllerpostconditions/</link>
		<comments>http://code.hexflux.com/2009/06/easier-searchfiltering-with-controllerpostconditions/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 04:54:27 +0000</pubDate>
		<dc:creator>Hizam Mohd</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[postCondition]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://code.hexflux.com/?p=80</guid>
		<description><![CDATA[Using postCondition to simplify your search or filtering feature.]]></description>
			<content:encoded><![CDATA[<p>In the old days, I remember being extra lazy when it comes to requests for filter/search functions. I would be lazier if the conditions includes lots of fields.</p>
<p>Stumbling upon postConditions, this flipped what I felt about creating a filter function.</p>
<p>So here&#8217;s what we are going to try, we&#8217;ll create a filter function for messages as example. Let&#8217;s assume you have the table, model, controller and some CRUD done and working for messages.</p>
<p>Let us begin by creating the view first since we&#8217;re getting the filter conditions from the form. It&#8217;s this file: &#8216;app/views/messages/filter_messages.ctp&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
&lt;h2&gt;Filter Messages&lt;/h2&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Message'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'filter_messages'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'from'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Submit Filter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;hr /&gt;
&lt;h2&gt;Filter Results: &lt;/h2&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//display apa kita jumpa kat sini</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$messages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Message'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'message'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;hr /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Then add the filter function to the app/controllers/messages_controller.php.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> filter_messages<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//simple initialization for the variable that'll carry the result</span>
        <span style="color: #000088;">$messages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//checks if we sent something to filter via the form (remember sent items go to $this-&gt;data)</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">/*
             * here comes postConditions
             * first parameter is the posted value,
             * 
             * the second parameter (array('message' =&gt; 'LIKE'))
             * tells postCondition for the fields we're looking at and what type of
             * condition we want to use for that field item. So in this case, we want to use 'LIKE' for field 'message'
             * therefore the sql generated will look similar to &quot;SELECT * FROM messages as Message WHERE Message.message LIKE '%whatever%' &quot;
             * 
             * the third parameter, will tell what is the boolean condition among each
             * condition we set for the find (you know like if we want 'message' is like 'b' AND the message came from 'dude')
             * 
             * last and quite least (heh), if we set true, only the fields we set in the second parameter will be considered. 
             * if false, any field in $this-&gt;data will be used. nifty huh.
             */</span>
            <span style="color: #000088;">$conditions</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">postConditions</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #339933;">,</span>
                                                 <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'LIKE'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                                 <span style="color: #0000ff;">'OR'</span><span style="color: #339933;">,</span>
                                                 <span style="color: #009900; font-weight: bold;">false</span>
                                               <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//the usual find() function.</span>
            <span style="color: #000088;">$messages</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//sends result to the view.</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'messages'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That&#8217;s it!</p>
<p>An additional note: Controller::postConditions() doesn&#8217;t mean you have to use the data posted but you can also ready your own array in the usual $myarray['Model']['field1'] format and use that as parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #000088;">$tobefiltered</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>
                        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                               <span style="color: #0000ff;">'message'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'this is my message'</span><span style="color: #339933;">,</span>
                               <span style="color: #0000ff;">'created'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'2009-09-09 21:09:09'</span><span style="color: #339933;">,</span>
                               <span style="color: #0000ff;">'from'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'013xxxxxxxxxx'</span>
                              <span style="color: #009900;">&#41;</span>
                       <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$condition_generated</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">postConditions</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tobefiltered</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://code.hexflux.com/2009/06/easier-searchfiltering-with-controllerpostconditions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
