<?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>Behçet Mutlu &#187; Database Administration</title>
	<atom:link href="http://www.behcetmutlu.com/blog/category/database-administration/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.behcetmutlu.com/blog</link>
	<description>&#34;Think the future that you would want tomorrow and act today to realize it.&#34;</description>
	<lastBuildDate>Wed, 22 Feb 2012 16:27:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CakePHP : Translate Behaviour : Fixing Nested Translation Problem</title>
		<link>http://www.behcetmutlu.com/blog/cakephp-translate-behaviour-translation-problem/</link>
		<comments>http://www.behcetmutlu.com/blog/cakephp-translate-behaviour-translation-problem/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 20:27:51 +0000</pubDate>
		<dc:creator>bekco</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Data Querying]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.behcetmutlu.com/blog/?p=191</guid>
		<description><![CDATA[<div style="padding-top:5px;padding-right:0px;padding-bottom:5px;padding-left:0px;;">
							<iframe
								style="height:25px !important; border:none !important; overflow:hidden !important; width:450px !important;" frameborder="0" scrolling="no" allowTransparency="true"
								src="http://www.linksalpha.com/social?blog=Beh%C3%A7et+Mutlu&link=http%253A%252F%252Fwww.behcetmutlu.com%252Fblog%252Fcakephp-translate-behaviour-translation-problem%252F&title=CakePHP+%3A+Translate+Behaviour+%3A+Fixing+Nested+Translation+Problem&desc=If+you+ever+tried+to+use+Translation+Behaviour+of+CakePHP+you+should+realize+that+Translate+Behavior+does+not+support+nested+translations+for+the+model+relations.+I%27ve+found+one+article+that+shows+how&fc=333333&fs=arial&fblname=like&fblref=facebook&fbllang=en_US&fblshow=1&fbsctr=1&fbslang=en&twlang=en&twmention=bekco&twrelated1=&twrelated2=&twctr=1&lnkdshow=noshow&lnkdctr=1&buzzctr=1&diggctr=1&stblctr=1">
							</iframe>
						</div>If you ever tried to use Translation Behaviour of CakePHP you should realize that Translate Behavior does not support nested translations for the model relations. I&#8217;ve found one article that shows how to fix a similar translation problem on related models at http://groups.google.com/group/cake-php/browse_thread/thread/9b7e60900269643b%29. Unfortunately it was not a complete solution that you can implement easily [...]]]></description>
			<content:encoded><![CDATA[<div style="padding-top:5px;padding-right:0px;padding-bottom:5px;padding-left:0px;;">
							<iframe
								style="height:25px !important; border:none !important; overflow:hidden !important; width:450px !important;" frameborder="0" scrolling="no" allowTransparency="true"
								src="http://www.linksalpha.com/social?blog=Beh%C3%A7et+Mutlu&link=http%253A%252F%252Fwww.behcetmutlu.com%252Fblog%252Fcakephp-translate-behaviour-translation-problem%252F&title=CakePHP+%3A+Translate+Behaviour+%3A+Fixing+Nested+Translation+Problem&desc=If+you+ever+tried+to+use+Translation+Behaviour+of+CakePHP+you+should+realize+that+Translate+Behavior+does+not+support+nested+translations+for+the+model+relations.+I%27ve+found+one+article+that+shows+how&fc=333333&fs=arial&fblname=like&fblref=facebook&fbllang=en_US&fblshow=1&fbsctr=1&fbslang=en&twlang=en&twmention=bekco&twrelated1=&twrelated2=&twctr=1&lnkdshow=noshow&lnkdctr=1&buzzctr=1&diggctr=1&stblctr=1">
							</iframe>
						</div><p>If you ever tried to use Translation Behaviour of CakePHP you should realize that Translate Behavior does not support nested translations for the model relations. I&#8217;ve found one article that shows how to fix a similar translation problem on related models at <a href="http://groups.google.com/group/cake-php/browse_thread/thread/9b7e60900269643b%29">http://groups.google.com/group/cake-php/browse_thread/thread/9b7e60900269643b%29</a>.</p>
<p>Unfortunately it was not a complete solution that you can implement easily but it gave me a good approach to help the model to request nested queries to make Translate Behaviour work.</p>
<p>I need to warn you that this solution is not a really good one but until CakePHP team fix this issue on one of the future releases of CakePHP this would help you a lot.</p>
<p>Here is the code:</p>
<pre class="prettyprint">class AppModel extends Model {

    var $_findMethods = array(&#39;all&#39; =&gt; true, &#39;first&#39; =&gt; true, &#39;count&#39; =&gt; true,
		&#39;neighbors&#39; =&gt; true, &#39;list&#39; =&gt; true, &#39;threaded&#39; =&gt; true, &#39;translated&#39; =&gt; true);

    function _findTranslated($state, $query, $results = array()) {
        if ($state == &#39;before&#39;) {
            return array_merge($query, array(
                &#47;&#47;&#39;fields&#39; =&gt; array(&#39;id&#39;, &#39;name&#39;),
                &#47;&#47;&#39;recursive&#39; =&gt; -1
            ));
        } elseif ($state == &#39;after&#39;) {
            if (empty($results)) {
                return $results;
            }

            &#47;&#47; get the model&#39;s belongs to relation model names
            $belongsTo = Set::extract($this-&gt;belongsTo, &#39;&#47;@*&#39;);

            if(!empty($belongsTo) &amp;&amp; isset($belongsTo&#91;0&#93;) &amp;&amp; is_array($belongsTo&#91;0&#93;))
                $belongsTo = $belongsTo&#91;0&#93;;

            if(!empty($belongsTo))
                foreach($results as &amp;$result)
                {
                    foreach($belongsTo as $modelName)
                    {
                        if(isset($result&#91;$modelName&#93;) &amp;&amp;
                            isset($result&#91;$modelName&#93;&#91;&#39;id&#39;&#93;) &amp;&amp;
                            !empty($result&#91;$modelName&#93;&#91;&#39;id&#39;&#93;))
                        {

                            $data = $this-&gt;$modelName-&gt;find(&#39;first&#39;, array(
                                &#39;conditions&#39; =&gt; array(
                                    $modelName.&#39;.id&#39; =&gt; $result&#91;$modelName&#93;&#91;&#39;id&#39;&#93;
                                ),
                                &#39;recursive&#39; =&gt; -1
                            ));

                            if(!empty($data))
                                $result&#91;$modelName&#93; = $data&#91;$modelName&#93;;
                        }
                    }
                }

            return $results;
        }
    }
}</pre>
<p>What you need to do is just implement the _findTranslated method on your model or on AppModel for general usage with the $_findMethods private variable and instead of using regular find methods for example:</p>
<pre class="prettyprint">$this-&gt;Post-&gt;find(&#39;all&#39;);</pre>
<p>You should use:</p>
<pre class="prettyprint">$this-&gt;Post-&gt;find(&#39;translated&#39;);</pre>
<p>And the result will look as the same above but with translated inner relations.</p>
<p><strong>Not: </strong>I have not implement all types of the relations yet but if you find hard to add such relations let me know, I&#8217;ll do my best.</p>
<p>I hope this could help you. Please leave me some responses to make sure is helpful.</p>
<p>Thank you for reading this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.behcetmutlu.com/blog/cakephp-translate-behaviour-translation-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building the Best SQL Manager With PHP (Part 2)</title>
		<link>http://www.behcetmutlu.com/blog/building-the-best-sql-manager-with-php-2/</link>
		<comments>http://www.behcetmutlu.com/blog/building-the-best-sql-manager-with-php-2/#comments</comments>
		<pubDate>Tue, 31 May 2011 06:27:12 +0000</pubDate>
		<dc:creator>bekco</dc:creator>
				<category><![CDATA[Data Querying]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[MySQL Database]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.behcetmutlu.com/blog/?p=181</guid>
		<description><![CDATA[<div style="padding-top:5px;padding-right:0px;padding-bottom:5px;padding-left:0px;;">
							<iframe
								style="height:25px !important; border:none !important; overflow:hidden !important; width:450px !important;" frameborder="0" scrolling="no" allowTransparency="true"
								src="http://www.linksalpha.com/social?blog=Beh%C3%A7et+Mutlu&link=http%253A%252F%252Fwww.behcetmutlu.com%252Fblog%252Fbuilding-the-best-sql-manager-with-php-2%252F&title=Building+the+Best+SQL+Manager+With+PHP+%28Part+2%29&desc=After+a+long+day+work+on+a+complicated+class+I+finally+finished+my+requiered+querying+algorithm.+The+final+output+of+my+SQL+Manager+class+is+like%0D%0Aselect%0D%0A+%60adproperties%60.%2A%2C+%60properties%60.%2A%0D%0A+FROM+%60adp&fc=333333&fs=arial&fblname=like&fblref=facebook&fbllang=en_US&fblshow=1&fbsctr=1&fbslang=en&twlang=en&twmention=bekco&twrelated1=&twrelated2=&twctr=1&lnkdshow=noshow&lnkdctr=1&buzzctr=1&diggctr=1&stblctr=1">
							</iframe>
						</div>After a long day work on a complicated class I finally finished my requiered querying algorithm. The final output of my SQL Manager class is like select `adproperties`.*, `properties`.* FROM `adproperties` left join (select * FROM `advertisementproperties` WHERE ((`advertisementproperties`.`advertisement_id` = &#39;113&#39;))) `properties` on (`properties`.`adproperty_id` = (`adproperties`.`id`)) WHERE ((`adcategory_id` = (select `advertisements`.`adcategory_id` as adcategory FROM `advertisements` [...]]]></description>
			<content:encoded><![CDATA[<div style="padding-top:5px;padding-right:0px;padding-bottom:5px;padding-left:0px;;">
							<iframe
								style="height:25px !important; border:none !important; overflow:hidden !important; width:450px !important;" frameborder="0" scrolling="no" allowTransparency="true"
								src="http://www.linksalpha.com/social?blog=Beh%C3%A7et+Mutlu&link=http%253A%252F%252Fwww.behcetmutlu.com%252Fblog%252Fbuilding-the-best-sql-manager-with-php-2%252F&title=Building+the+Best+SQL+Manager+With+PHP+%28Part+2%29&desc=After+a+long+day+work+on+a+complicated+class+I+finally+finished+my+requiered+querying+algorithm.+The+final+output+of+my+SQL+Manager+class+is+like%0D%0Aselect%0D%0A+%60adproperties%60.%2A%2C+%60properties%60.%2A%0D%0A+FROM+%60adp&fc=333333&fs=arial&fblname=like&fblref=facebook&fbllang=en_US&fblshow=1&fbsctr=1&fbslang=en&twlang=en&twmention=bekco&twrelated1=&twrelated2=&twctr=1&lnkdshow=noshow&lnkdctr=1&buzzctr=1&diggctr=1&stblctr=1">
							</iframe>
						</div><p>After a long day work on a complicated class I finally finished my requiered querying algorithm. The final output of my SQL Manager class is like</p>
<pre class="prettyprint">select
 `adproperties`.*, `properties`.*
 FROM `adproperties`
 left join (select
 *
 FROM `advertisementproperties`
  WHERE ((`advertisementproperties`.`advertisement_id` = &#39;113&#39;))) `properties` on (`properties`.`adproperty_id` = (`adproperties`.`id`))
  WHERE ((`adcategory_id` = (select
 `advertisements`.`adcategory_id` as adcategory
 FROM `advertisements`
  WHERE (((`advertisements`.`id` = &#39;113&#39;) and (`advertisements`.`account_id` = &#39;1&#39;))))))</pre>
<p>This worked for me perfectly. Even I add some more conditions on it. Later I&#8217;ll have time to fix the paranthesis problem but for the beginning, I&#8217;ll keep using this code instead of the old one.<br />
I must finaly point that, I have not tried it with other kind of configuration array but after I finish the whole it will be the perfect SQL Manager Class written in PHP ever.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.behcetmutlu.com/blog/building-the-best-sql-manager-with-php-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building the Best SQL Manager With PHP</title>
		<link>http://www.behcetmutlu.com/blog/building-the-best-sql-manager-with-php/</link>
		<comments>http://www.behcetmutlu.com/blog/building-the-best-sql-manager-with-php/#comments</comments>
		<pubDate>Sun, 29 May 2011 10:43:25 +0000</pubDate>
		<dc:creator>bekco</dc:creator>
				<category><![CDATA[Data Querying]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[MySQL Database]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.behcetmutlu.com/blog/?p=166</guid>
		<description><![CDATA[<div style="padding-top:5px;padding-right:0px;padding-bottom:5px;padding-left:0px;;">
							<iframe
								style="height:25px !important; border:none !important; overflow:hidden !important; width:450px !important;" frameborder="0" scrolling="no" allowTransparency="true"
								src="http://www.linksalpha.com/social?blog=Beh%C3%A7et+Mutlu&link=http%253A%252F%252Fwww.behcetmutlu.com%252Fblog%252Fbuilding-the-best-sql-manager-with-php%252F&title=Building+the+Best+SQL+Manager+With+PHP&desc=Today+I+decided+to+improve+one+of+my+developed+SQL+Manager+class+to+be+able+to+create+easy+sql+queries+but+complicated+enough+not+to+be+able+ease+work+on+defining+the+complicated+queries.+With+my+old+&fc=333333&fs=arial&fblname=like&fblref=facebook&fbllang=en_US&fblshow=1&fbsctr=1&fbslang=en&twlang=en&twmention=bekco&twrelated1=&twrelated2=&twctr=1&lnkdshow=noshow&lnkdctr=1&buzzctr=1&diggctr=1&stblctr=1">
							</iframe>
						</div>Today I decided to improve one of my developed SQL Manager class to be able to create easy sql queries but complicated enough not to be able ease work on defining the complicated queries. With my old solution I could not be able to complete a query like below. SELECT `adproperties`.*, `properties`.* FROM `adproperties` LEFT [...]]]></description>
			<content:encoded><![CDATA[<div style="padding-top:5px;padding-right:0px;padding-bottom:5px;padding-left:0px;;">
							<iframe
								style="height:25px !important; border:none !important; overflow:hidden !important; width:450px !important;" frameborder="0" scrolling="no" allowTransparency="true"
								src="http://www.linksalpha.com/social?blog=Beh%C3%A7et+Mutlu&link=http%253A%252F%252Fwww.behcetmutlu.com%252Fblog%252Fbuilding-the-best-sql-manager-with-php%252F&title=Building+the+Best+SQL+Manager+With+PHP&desc=Today+I+decided+to+improve+one+of+my+developed+SQL+Manager+class+to+be+able+to+create+easy+sql+queries+but+complicated+enough+not+to+be+able+ease+work+on+defining+the+complicated+queries.+With+my+old+&fc=333333&fs=arial&fblname=like&fblref=facebook&fbllang=en_US&fblshow=1&fbsctr=1&fbslang=en&twlang=en&twmention=bekco&twrelated1=&twrelated2=&twctr=1&lnkdshow=noshow&lnkdctr=1&buzzctr=1&diggctr=1&stblctr=1">
							</iframe>
						</div><p>Today I decided to improve one of my developed SQL Manager class to be able to create easy sql queries but complicated enough not to be able ease work on defining the complicated queries. With my old solution I could not be able to complete a query like below.</p>
<pre class="prettyprint">SELECT
`adproperties`.*,
`properties`.*
FROM `adproperties`
LEFT JOIN (SELECT *
                FROM
                `advertisementproperties`
                WHERE `advertisementproperties`.`advertisement_id` = 110) `properties`
ON `properties`.`adproperty_id` = `adproperties`.`id`
WHERE
`adcategory_id` = (SELECT
                           `advertisements`.`adcategory_id` as `adcategory`
                           FROM `advertisements`
                           WHERE `advertisements`.`id` = 110)</pre>
<p>It is something right? So, what I need to do is adding inner queries up on my old manager even on the Joining phrases and Where clauses. It is not easy to build a complete solution for SQL because you would always think on the current problem to be fixed not the whole. Even though, I could have been able to build a generalization to create search queries on the relational queries of which is also used in many projects of mine.</p>
<p>Now what I can do with it is like:</p>
<pre class="prettyprint">SELECT
`advertisementproperties`.`id`,
`advertisementproperties`.`advertisement_id`,
`advertisementproperties`.`value`,
`adproperties_rel`.`options`,
`adproperties_rel`.`type`,
`adproperties_rel`.`adcategory_id`,
`adproperties_rel`.`id` as `adproperty_id`,
`adproperties_rel`.`name`
FROM `advertisementproperties`
RIGHT JOIN `adproperties` `adproperties_rel`
ON `adproperties_rel`.`id` = `advertisementproperties`.`adproperty_id`
WHERE (
(`adproperties_rel`.`account_id` = &#39;1&#39;) or ISNULL
(`adproperties_rel`.`account_id`)) and (
(`advertisement_id` = 113) or ISNULL
(`advertisement_id`)) and
(`adproperties_rel`.`adcategory_id` = &#39;22&#39;)</pre>
<p>This code above is not able to do what I needed and it is not a good SQL query either. What I really need to do is to create better querying solution for it.</p>
<p>I started from the end to internal core of the code and firstly defined the object that I would like it to create my query:</p>
<pre class="prettyprint">$sqlMgr = new SQLManager(array(
    &#34;method&#34; =&gt; &#34;select&#34;,
    &#34;selections&#34; =&gt; array(&#34;adproperties&#34;  =&gt; &#34;*&#34;, &#34;properties&#34;  =&gt; &#34;*&#34;),
    &#34;tableName&#34; =&gt; &#34;adproperties&#34;,
    &#34;relations&#34; =&gt; array(
        &#34;properties&#34; =&gt; array(
            &#34;join&#34; =&gt; &#34;left&#34;,
            &#34;tableName&#34; =&gt; array(
                &#34;query&#34; =&gt; true,
                &#34;method&#34; =&gt; &#34;select&#34;,
                &#34;tableName&#34; =&gt; &#34;advertisementproperties&#34;,
                &#34;conditions&#34; =&gt; array(&#34;advertisementproperties&#34; =&gt; &#34;advertisement_id&#34;, &#34;=&#34; =&gt; 113)
            ),
            &#34;conditions&#34; =&gt; array(&#34;properties&#34; =&gt; &#34;adproperty_id&#34;, &#34;=&#34; =&gt; array(&#34;adproperties&#34; =&gt; &#34;id&#34;))
        )
    ),
    &#34;conditions&#34; =&gt; array(
        &#34;adcategory_id&#34;,
        &#34;=&#34; =&gt; array(
            &#34;query&#34; =&gt; true,
            &#34;method&#34; =&gt; &#34;select&#34;,
            &#34;tableName&#34; =&gt; &#34;advertisements&#34;,
            &#34;selections&#34; =&gt; array(&#34;advertisements&#34; =&gt; &#34;adcategory_id&#34;),
            &#34;fieldAliases&#34; =&gt; array(
                array(
                    &#34;table&#34; =&gt; &#34;advertisements&#34;,
                    &#34;name&#34; =&gt; &#34;adcategory_id&#34;,
                    &#34;alias&#34; =&gt; &#34;adcategory&#34;
                )
            ),
            &#34;conditions&#34; =&gt; array(array(&#34;advertisements&#34; =&gt; &#34;id&#34;, &#34;=&#34; =&gt; 113), &#34;and&#34; =&gt; array(&#34;advertisements&#34; =&gt; &#34;account_id&#34;, &#34;=&#34; =&gt; 1))
        )
    )
));</pre>
<p>This code should definitely do what I would like to do with my SQL Manager Class. Now is the time to get this configuration in to action.<br />
With my old SQL Helper I&#8217;m no where close <img src='http://www.behcetmutlu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre class="prettyprint">SELECT `*`.*, `*`.*, `l_rel`.`l` as `l`, `adproperties`.`_rel`
 FROM `adproperties`
 L JOIN `l` `l_rel` ON `l_rel`.`l` = `adproperties`.`l`
  JOIN `` `_rel` ON `_rel`.`` = `adproperties`.``
 WHERE (
(`advertisements`.`adcategory_id`) and
(`_rel`.`Array`)) and ()</pre>
<p>Some of the generated query is right how it supposed to be. I&#8217;ll start from the selections.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.behcetmutlu.com/blog/building-the-best-sql-manager-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

