<?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; cakephp</title>
	<atom:link href="http://code.hexflux.com/category/cakephp/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>Coding in an illusion of choice</title>
		<link>http://code.hexflux.com/2009/07/coding-in-an-illusion-of-choice/</link>
		<comments>http://code.hexflux.com/2009/07/coding-in-an-illusion-of-choice/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 08:02:09 +0000</pubDate>
		<dc:creator>Hizam Mohd</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://code.hexflux.com/?p=94</guid>
		<description><![CDATA[How to code the illusion of choice in cakephp]]></description>
			<content:encoded><![CDATA[<p>I found this in a code written by a newbie. I thought it would be a good example about giving people false choices.</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$complaint</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Complaint'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'accepted'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
        by <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$complaint</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AssignedTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$complaint</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Complaint'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'decline'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
        by <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$complaint</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AssignedTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$complaint</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Complaint'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'updated'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
        by <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$complaint</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AssignedTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$complaint</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Complaint'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'closed'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
        by <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$complaint</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AssignedTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Funny but nearly gave me a heart attack.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.hexflux.com/2009/07/coding-in-an-illusion-of-choice/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
		<item>
		<title>menggunakan cakephp&#8217;s beforeFind</title>
		<link>http://code.hexflux.com/2008/09/menggunakan-cakephps-beforefind/</link>
		<comments>http://code.hexflux.com/2008/09/menggunakan-cakephps-beforefind/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 03:33:08 +0000</pubDate>
		<dc:creator>Hizam Mohd</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[beforefind]]></category>
		<category><![CDATA[callback]]></category>

		<guid isPermaLink="false">http://code.hexflux.com/?p=39</guid>
		<description><![CDATA[I found out that on cakephp 1.2 RC3 you must use the new $Model->find() style or the query will break. 1 2 3 4 5 6 7 8 9 10 11 //in the controller use this... $this-&#62;Post-&#62;find&#40;'first', $params&#41;; &#160; //instead of the 'old' style... $this-&#62;Post-&#62;find&#40;$conditions&#41;; &#160; //so that in the model, you can do something [...]]]></description>
			<content:encoded><![CDATA[<p><i>I found out that on cakephp 1.2 RC3 you must use the new $Model->find() style or the query will break.</i> </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//in the controller use this...</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//instead of the 'old' style...</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//so that in the model, you can do something like this...</span>
<span style="color: #000000; font-weight: bold;">function</span> beforeFind<span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryData</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$queryData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conditions'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;`Post`.`post_type`&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;personal&quot;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p>Ada masanya kita perlu mengubah condition carian-carian kita pada satu-satu model. Selain meletakkan condition query itu setiap kali kita melakukan find, kita boleh menggunakan function callback pada model iaitu function beforeFind(). Ia menerima satu parameter iaitu $queryData, variable array yang menyimpan maklumat untuk find seperti &#8216;conditions&#8217;, &#8216;limit&#8217;, &#8216;joins&#8217; dan sebagainya.</p>
<p><del>Tidak</del> seperti tertera pada dokumentasi cakePHP, sebenarnya untuk beforeFind  kita dapat memulangkan samada boolean (true/false) ataupun satu array . Jika kita memulangkan boolean, ia samada menjalankan query tersebut atau tidak: true untuk teruskan query, false untuk tidak membuat query.</p>
<p>Apa yang <del>tidak</del> disebut ialah jika kita mahu menukarkan apa2 pada syarat query itu, kita perlu buat perubahan pada $queryData dan pulangkan $queryData itu (return ($queryData)). Contoh&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* ini contoh untuk menambahkan syarat pada find kita. Ini pada model AccomodationTravelBooking
**/</span>
<span style="color: #000000; font-weight: bold;">function</span> beforeFind<span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryData</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$queryData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conditions'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;`AccomodationTravelBooking.booking_type` = 'accomodation'&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Contoh diberi pada dokumentasi cakephp ialah jika kita tidak membenarkan query dilakukan mengikut syarat2 tertentu:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* ini contoh untuk menambahkan syarat pada find kita. Ini pada model AccomodationTravelBooking
**/</span>
<span style="color: #000000; font-weight: bold;">function</span> beforeFind<span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryData</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//kononnya id 44 tu satu ID yang kita tak benarkan untuk diquery</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conditions'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AccomodationTravelBooking.id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">44</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>nota update: tak baca betul2, camtu la resultnya.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.hexflux.com/2008/09/menggunakan-cakephps-beforefind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cakephp Talk di veryimportantprogrammer.com</title>
		<link>http://code.hexflux.com/2008/08/cakephp-talk-di-veryimportantprogrammercom/</link>
		<comments>http://code.hexflux.com/2008/08/cakephp-talk-di-veryimportantprogrammercom/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 22:23:02 +0000</pubDate>
		<dc:creator>Hizam Mohd</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[veryimportantprogrammer.com]]></category>

		<guid isPermaLink="false">http://code.hexflux.com/?p=31</guid>
		<description><![CDATA[Saya akan bagi talk mengenai cakephp di event veryimportantprogrammer.com hari ini. Ini akan menumpu kepada cakephp secara am dengan harapan pendengar dapat melihat macam mana membina aplikasi cakephp. Kebetulan terjumpa coding lama yang boleh saya guna untuk demo. Files dan slides akan saya letakkan di sini selepas talk. Coding lama tu perlu kepada sedikit comments [...]]]></description>
			<content:encoded><![CDATA[<p>Saya akan bagi talk mengenai cakephp di event veryimportantprogrammer.com hari ini. Ini akan menumpu kepada cakephp secara am dengan harapan pendengar dapat melihat macam mana membina aplikasi cakephp.</p>
<p>Kebetulan terjumpa coding lama yang boleh saya guna untuk demo.</p>
<p>Files dan slides akan saya letakkan di sini selepas talk. Coding lama tu perlu kepada sedikit comments untuk memudahkan orang faham apa yang ada di situ.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.hexflux.com/2008/08/cakephp-talk-di-veryimportantprogrammercom/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Contoh penggunaan find dalam cakephp 1.2</title>
		<link>http://code.hexflux.com/2008/08/contoh-penggunaan-find-dalam-cakephp-12/</link>
		<comments>http://code.hexflux.com/2008/08/contoh-penggunaan-find-dalam-cakephp-12/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 07:04:49 +0000</pubDate>
		<dc:creator>Hizam Mohd</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[model]]></category>

		<guid isPermaLink="false">http://code.hexflux.com/?p=21</guid>
		<description><![CDATA[contoh menggunakan find mengikut cakephp 1.2]]></description>
			<content:encoded><![CDATA[<p>Cakephp v1.2 memperkenalkan lebih banyak pilihan untuk model::find() iaitu samada mahu mendapatkan satu rekod sahaja (&#8216;first&#8217;), mendapatkan kesemua rekod (&#8216;all&#8217;), kiraan jumlah rekod yang dicapai (&#8216;count&#8217;) ataupun dalam bentuk list untuk dimasukkan digunakan dalam dropdown list contohnya.</p>
<p>Berikut beberapa contoh penggunaannya.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//first. mendapatkan satu rekod sahaja (yang pertama dicapai oleh database)</span>
<span style="color: #000088;">$condition</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Post.user_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//capai blog-post yang dibuat oleh user 3</span>
<span style="color: #000088;">$posts_by_user</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$condition</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$posts_by_user</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//memaparkan title post berkenaan.</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//all. mendapatkan semua rekod mengikut syarat yang kita tetapkan dalam $condition</span>
<span style="color: #000088;">$condition</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Post.user_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//capai blog-post yang dibuat oleh user 3</span>
<span style="color: #000088;">$posts_by_user</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</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: #000088;">$condition</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
pr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts_by_user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//ini akan memaparkan kandungan array iaitu kesemua rekod kepunyaan user 3</span></pre></td></tr></table></div>

<p>model::findAll() akan dibuang (deprecated) dalam versi akhir 1.2 jadi kita patut menggunakan model::find untuk mencapai rekod-rekod dalam cakephp.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//count. mengira berapa rekod yang dicapai</span>
<span style="color: #000088;">$condition</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Post.user_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//capai blog-post yang dibuat oleh user 3</span>
<span style="color: #000088;">$count_posts_by_user</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'count'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$condition</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//ini memulangkan integer</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$posts_by_user</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//akan terpapar jumlah rekod ie 6</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//list. mendapatkan senarai rekod</span>
<span style="color: #666666; font-style: italic;">//dalam bentuk yang sesuai untuk diguna dalam $form-&gt;input jenis 'select' (contohnya)</span>
<span style="color: #666666; font-style: italic;">//+++++++++++++++ controller +++++++++++++++++++++++++++//</span>
&nbsp;
<span style="color: #000088;">$states</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">State</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;">'states'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$states</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//hantarkan kepada view...</span>
<span style="color: #666666; font-style: italic;">//+++++++++++++++ view +++++++++++++++++++++++++++//</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//di view pula...</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;">'negeri'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'options'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$states</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//ini akan paparkan dropdown senarai negeri.</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://code.hexflux.com/2008/08/contoh-penggunaan-find-dalam-cakephp-12/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
