<?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>Life &#187; framework</title>
	<atom:link href="http://life.co-hey.com/category/web-pc/framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://life.co-hey.com</link>
	<description></description>
	<lastBuildDate>Tue, 17 Jan 2012 08:15:38 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Zend_Applicationを使ったZend_Router設定</title>
		<link>http://life.co-hey.com/2011/09/zend_application%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9fzend_router%e8%a8%ad%e5%ae%9a/</link>
		<comments>http://life.co-hey.com/2011/09/zend_application%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9fzend_router%e8%a8%ad%e5%ae%9a/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 08:00:53 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[web & pc]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=1039</guid>
		<description><![CDATA[今回は、以下のことをやりたかったのです。 【http://test.com/page/3/】とういうページングURLを実現しつつ、他のルーティングは既存のまま。 【http://test.com/index/index/ [...]]]></description>
			<content:encoded><![CDATA[<p>
今回は、以下のことをやりたかったのです。<br />
【http://test.com/page/3/】とういうページングURLを実現しつつ、他のルーティングは既存のまま。<br />
【http://test.com/index/index/page/3/】の indexの部分を出したくなかったのです。<br />
<br />
それを実現するために application.ini に追加した記述が以下です。<br />
<br />

<div class="wp_codebox"><table><tr id="p10392"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1039code2"><pre class="ini" style="font-family:Osaka-mono,monospace;"><span style="color: #666666; font-style: italic;">; ルート設定</span>
resources.router.routes.default.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Zend_Controller_Router_Route_Module&quot;</span>
&nbsp;
resources.router.routes.top.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Zend_Controller_Router_Route&quot;</span>
resources.router.routes.top.route <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;page/:page/&quot;</span>
resources.router.routes.top.defaults.controller <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;index&quot;</span>
resources.router.routes.top.defaults.action <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;index&quot;</span>
resources.router.routes.top.defaults.page <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 1</span></pre></td></tr></table></div>

<br />
<br />
上から順番に検索していって、最後にヒットしたルートが有効になるようです。なので、defaultルートは一番上になります。<br />
<br />
Zend_Application_Resource_Router <br />
=> Zend_Controller_Router_Rewrite <br />
=> Zend_Controller_Router_Route <br />
=> Zend_Controller_Router_Route_Module<br />
<br />
こんな順番でソースを見てたら、面白くなってきて、興味があちこちいってしまい、<br />
動作を確認するまでに、結構時間がかかってしまいました。<br />
<br />
Zend_Controller_Router_Rewiteの中に、各Routeクラスを設定していく構成なんですね。<br />
今まで、基本ルールばかりを使ってたので、勉強になりました。<br />
<br />
途中で試した Zend_Controller_Route_Regex を動作させれなかったのが気になるので、近いうちにまた実験しします。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend_Application%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9FZend_Router%E8%A8%AD%E5%AE%9A%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F42wmvhv&amp;in_reply_to_status_id=113885124503932929" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2011/09/zend_application%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9fzend_router%e8%a8%ad%e5%ae%9a/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZendFramework1.11.1はPCREのUnicode対応が必要</title>
		<link>http://life.co-hey.com/2011/02/zendframework1-11-1%e3%81%afpcre%e3%81%aeunicode%e5%af%be%e5%bf%9c%e3%81%8c%e5%bf%85%e8%a6%81/</link>
		<comments>http://life.co-hey.com/2011/02/zendframework1-11-1%e3%81%afpcre%e3%81%aeunicode%e5%af%be%e5%bf%9c%e3%81%8c%e5%bf%85%e8%a6%81/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 04:53:41 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[web & pc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=937</guid>
		<description><![CDATA[ZendFramework1.10.1から、1.11.1に変えたときに、Noticeメッセージがでました。内容は、「PCREがUnicodeをサポートしてませんよ」というもの。 Niticeメッセージはでないようにしてた [...]]]></description>
			<content:encoded><![CDATA[<p>
ZendFramework1.10.1から、1.11.1に変えたときに、Noticeメッセージがでました。内容は、「PCREがUnicodeをサポートしてませんよ」というもの。<br />
Niticeメッセージはでないようにしてたのにと思って調べてみたら、ZendFrameworkのZend_Locale内で、trigger_errorを使ってメッセージを出してました。なので、ちゃんとPCREをUnicode対応にしました。<br />
<br />
参考にさせていただいたサイトは以下です。<br />
<a href="http://junrei.dip.jp/wordpress/nagios/pnp4nagios%EF%BC%880-6-2%EF%BC%89%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB/">http://junrei.dip.jp/wordpress/nagios/pnp4nagios%EF%BC%880-6-2%EF%BC%89%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB/</a><br />
<br />
以下のコマンドで、PCREがUnicodeに対応してないことを確認。<br />
<br />

<div class="wp_codebox"><table><tr id="p9377"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p937code7"><pre class="sh" style="font-family:Osaka-mono,monospace;"># pcretest -C
PCRE version 8.02 2010-03-19
Compiled with
  UTF-8 support
  No Unicode properties support
  Newline sequence is LF
  \R matches all Unicode newlines
  Internal link size = 2
  POSIX malloc threshold = 10
  Default match limit = 10000000
  Default recursion depth limit = 10000000
  Match recursion uses stack</pre></td></tr></table></div>

<br />
<br />
<br />
まずは、以下のファイルを作成。<br />
デフォルトのリポジトリに、pcre-unicodeがないので、下記のリポジトリを追加します。私の環境だと、phpをインストールするときに、追加してました。（忘れてました。。）<br />
<br />

<div class="wp_codebox"><table><tr id="p9378"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p937code8"><pre class="sh" style="font-family:Osaka-mono,monospace;"># vi /etc/yum.repos.d/utterramblings.repo
&nbsp;
[utterramblings]
name=Jason's Utter Ramblings Repo
baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
enabled=0
gpgcheck=1
gpgkey=http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka</pre></td></tr></table></div>

<br />
<br />
<br />
そして、以下のコマンドを実行<br />
<br />

<div class="wp_codebox"><table><tr id="p9379"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p937code9"><pre class="sh" style="font-family:Osaka-mono,monospace;"># yum install pcre-unicode</pre></td></tr></table></div>

<br />
<br />
<br />
再度、unicodeに対応しているかをコマンドで確認。<br />
<br />

<div class="wp_codebox"><table><tr id="p93710"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p937code10"><pre class="sh" style="font-family:Osaka-mono,monospace;"># pcretest -C
PCRE version 8.02 2010-03-19
Compiled with
  UTF-8 support
  Unicode properties support
  Newline sequence is LF
  \R matches all Unicode newlines
  Internal link size = 2
  POSIX malloc threshold = 10
  Default match limit = 10000000
  Default recursion depth limit = 10000000
  Match recursion uses stack</pre></td></tr></table></div>

<br />
<br />
Unicode properties supportになったのでOK。<br />
Noticeメッセージもでなくなりました。<br />
ZendFrameworkを利用するときに覚えておく必要がありそうです。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20ZendFramework1.11.1%E3%81%AFPCRE%E3%81%AEUnicode%E5%AF%BE%E5%BF%9C%E3%81%8C%E5%BF%85%E8%A6%81%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F4g4f9qu&amp;in_reply_to_status_id=32662932630282240" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2011/02/zendframework1-11-1%e3%81%afpcre%e3%81%aeunicode%e5%af%be%e5%bf%9c%e3%81%8c%e5%bf%85%e8%a6%81/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZendFrameworkの環境変数APPLICATION_ENVを設定する</title>
		<link>http://life.co-hey.com/2011/02/zendframework%e3%81%ae%e7%92%b0%e5%a2%83%e5%a4%89%e6%95%b0application_env%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b/</link>
		<comments>http://life.co-hey.com/2011/02/zendframework%e3%81%ae%e7%92%b0%e5%a2%83%e5%a4%89%e6%95%b0application_env%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 01:36:50 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[web & pc]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=945</guid>
		<description><![CDATA[ZendFrameworkでは、index.phpにもともと書いてある記述に以下の環境変数に関わるものがあります。 1 2 3 / Define application environment defined&#40;' [...]]]></description>
			<content:encoded><![CDATA[<p>
ZendFrameworkでは、index.phpにもともと書いてある記述に以下の環境変数に関わるものがあります。<br />
<br />

<div class="wp_codebox"><table><tr id="p94514"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p945code14"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #339933;">/</span> <span style="color: #990000;">Define</span> application environment
<span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">||</span> <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">getenv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">getenv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'production'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
APPLICATION_ENVが設定されてなかったら、&#8217;production&#8217; という文字列を、APPLICATION_ENVとして設定するという内容です。このAPPLICATION_ENVはどこで使われているかというと、Bootstrap内で、application.iniを読み込む際に、パラメタとして渡されています。<br />
<br />
application.iniは、DB接続設定等を書くファイルで、APPLICATION_ENVの値を、本番環境、テスト環境等で変えることによって、ファイルを編集することなく、DB接続先の設定を環境に合ったものに設定することができます。iniファイルの書き方は、こちらをご参照ください(<a href="http://framework.zend.com/manual/1.11/ja/zend.config.adapters.ini.html">http://framework.zend.com/manual/1.11/ja/zend.config.adapters.ini.html</a>)<br />
<br />
今回、<strong>/etc/sysconfig/httpd</strong> に以下の記述を追加すると、apacheにて環境変数を追加することができました。<br />
<br />

<div class="wp_codebox"><table><tr id="p94515"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p945code15"><pre class="sh" style="font-family:Osaka-mono,monospace;">export APPLICATION_ENV=&quot;development&quot;</pre></td></tr></table></div>

<br />
<br />
.htaccessを使うと、&#8221;setEnv&#8221;を使って記述することもできます。その場合の書き方は以下になります。<br />
<br />

<div class="wp_codebox"><table><tr id="p94516"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p945code16"><pre class="htaccess" style="font-family:Osaka-mono,monospace;">setEnv APPLICATION_ENV development</pre></td></tr></table></div>

<br />
<br />
これまで、.htaccessを使ったやり方のみでやってきて、/etc/sysconfig/httpd を編集する方法を知りませんでした。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20ZendFramework%E3%81%AE%E7%92%B0%E5%A2%83%E5%A4%89%E6%95%B0APPLICATION_ENV%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F4jgqbza&amp;in_reply_to_status_id=32251003998834688" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2011/02/zendframework%e3%81%ae%e7%92%b0%e5%a2%83%e5%a4%89%e6%95%b0application_env%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend_Mailで文字化けしなくなっていた</title>
		<link>http://life.co-hey.com/2011/01/zend_mail%e3%81%a7%e6%96%87%e5%ad%97%e5%8c%96%e3%81%91%e3%81%97%e3%81%aa%e3%81%8f%e3%81%aa%e3%81%a3%e3%81%a6%e3%81%84%e3%81%9f/</link>
		<comments>http://life.co-hey.com/2011/01/zend_mail%e3%81%a7%e6%96%87%e5%ad%97%e5%8c%96%e3%81%91%e3%81%97%e3%81%aa%e3%81%8f%e3%81%aa%e3%81%a3%e3%81%a6%e3%81%84%e3%81%9f/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 06:30:38 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[web & pc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=931</guid>
		<description><![CDATA[以前、こんな記事を書きました。 http://life.co-hey.com/2009/02/zendframework_175_1/ その後、Zend_Mailをいじることはなかったんですが、最近また利用する機会があっ [...]]]></description>
			<content:encoded><![CDATA[<p>
以前、こんな記事を書きました。<br />
<a href="http://life.co-hey.com/2009/02/zendframework_175_1/">http://life.co-hey.com/2009/02/zendframework_175_1/</a><br />
<br />
その後、Zend_Mailをいじることはなかったんですが、最近また利用する機会があったのですが、Zend_Mailの中身を修正しなくても、文字化けしなくなっていました。<br />
<br />

<div class="wp_codebox"><table><tr id="p93118"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p931code18"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> Zend_Debug<span style="color: #339933;">::</span><span style="color: #004000;">dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exception</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</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>
<span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Mail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'iso-2022-jp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test@co-hey.com'</span><span style="color: #009900;">&#41;</span>
     <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error@co-hey.com'</span><span style="color: #009900;">&#41;</span>
     <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSubject</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_convert_encoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'例外が発生しました'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'iso-2022-jp'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBodyText</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_convert_encoding</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">htmlspecialchars_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'iso-2022-jp'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
ErrorController.phpの中で、上記のプログラムでエラー発生箇所をメールするという用途で使っています。バージョンアップの度に、Zend_Mailを１行修正する手間がいらなくなるのはうれしいです。<br />
<br />
動作を試したのは、1.10.1ですが、いつから文字化けが解消されていたのかはわかりません。。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend_Mail%E3%81%A7%E6%96%87%E5%AD%97%E5%8C%96%E3%81%91%E3%81%97%E3%81%AA%E3%81%8F%E3%81%AA%E3%81%A3%E3%81%A6%E3%81%84%E3%81%9F%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F4nrbqxl&amp;in_reply_to_status_id=31962547674415105" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2011/01/zend_mail%e3%81%a7%e6%96%87%e5%ad%97%e5%8c%96%e3%81%91%e3%81%97%e3%81%aa%e3%81%8f%e3%81%aa%e3%81%a3%e3%81%a6%e3%81%84%e3%81%9f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zendを使ってZip解凍</title>
		<link>http://life.co-hey.com/2010/09/zend%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6zip%e8%a7%a3%e5%87%8d/</link>
		<comments>http://life.co-hey.com/2010/09/zend%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6zip%e8%a7%a3%e5%87%8d/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 02:43:39 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=792</guid>
		<description><![CDATA[以前、Zendを使ってZip圧縮という記事を書きましたが、今回は逆のZendを使ってZip解凍についてです。 前回は、Zend_Filter_Compressというクラスを使いましたが、今回はZend_Filter_De [...]]]></description>
			<content:encoded><![CDATA[<p>
以前、<a href="http://life.co-hey.com/2010/05/zendを使ってzip圧縮/">Zendを使ってZip圧縮</a>という記事を書きましたが、今回は逆のZendを使ってZip解凍についてです。<br />
<br />
前回は、Zend_Filter_Compressというクラスを使いましたが、今回はZend_Filter_Decompressというクラスを使います。<br />
使い方は以下です。<br />
<br />

<div class="wp_codebox"><table><tr id="p79223"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p792code23"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Decompress<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setArchive</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルのパス付ファイル名'</span><span style="color: #009900;">&#41;</span>
　　　 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTarget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hoge'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// hogeはnullじゃなければ何でもいいです。</span>
&nbsp;
または
&nbsp;
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Decompress<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTarget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルのパス付ファイル名'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
または
&nbsp;
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Decompress<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Zip'</span><span style="color: #339933;">,</span> 
            <span style="color: #0000ff;">'options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                            <span style="color: #0000ff;">'archive'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span><span style="color: #339933;">,</span> 
                            <span style="color: #0000ff;">'target'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span>
                         <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hoge'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// hogeはnullじゃなければ何でもいいです。</span>
&nbsp;
または
&nbsp;
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Decompress<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Zip'</span><span style="color: #339933;">,</span> 
            <span style="color: #0000ff;">'options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'target'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルのパス付ファイル名'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
または
&nbsp;
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Decompress<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zip'</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAdapterOptions</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
             <span style="color: #0000ff;">'archive'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span><span style="color: #339933;">,</span> 
             <span style="color: #0000ff;">'target'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hoge'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// hogeはnullじゃなければ何でもいいです。</span></pre></td></tr></table></div>

<br />
<br />
解凍、圧縮共に Zip の部分を Gz, Lzf, Rar, Tar, Bz2 に変更するとそれぞれの形式で圧縮解凍ができます。<br />
今回疑問に思ったのは（何で前回思わなかったのか。。）、なんで、こんな感じでかけないのかということ。<br />
<br />

<div class="wp_codebox"><table><tr id="p79224"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p792code24"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Decompress<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setArchive</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルのパス付ファイル名'</span><span style="color: #009900;">&#41;</span>
　　　 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTarget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
または
&nbsp;
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Decompress<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTarget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルの解凍先ディレクトリパス'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'圧縮ファイルのパス付ファイル名'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
不思議に思ってプログラムを観てみると、setTargetとかsetArchiveから返ってきているオブジェクトが、Zend_Filter_Decompressではなくて、Zend_Filter_Decompress内部で持っているZip処理のアダプターZend_Filter_Compress_Zipでした。<br />
Zend_Filter_Compress_Zipにはfilterっていうメソッドはないので、エラーがでてたんですね。<br />
<br />
Zend_Filter_Decompressの中で、<br />
<br />

<div class="wp_codebox"><table><tr id="p79225"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p792code25"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$adapter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adapter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Zend/Filter/Exception.php'</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown method '<span style="color: #006699; font-weight: bold;">{$method}</span>'&quot;</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: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adapter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
<br />
ってなっている部分を<br />
<br />

<div class="wp_codebox"><table><tr id="p79226"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p792code26"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$adapter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adapter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Zend/Filter/Exception.php'</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown method '<span style="color: #006699; font-weight: bold;">{$method}</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adapter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
<br />
ってしたら、やりたい書き方ができそうだけど、どんな弊害があるのかは細かく調べてないのでわかりません。ざっとみたところなさそう。アダプター使いたいなら、getAdapterで取得できるので、不具合もなさそう。なんで、アダプターを返すような作りにしたんだろう？読み取れてない意図があるのかな。<br />
</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6Zip%E8%A7%A3%E5%87%8D%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F22u7dbg" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/09/zend%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6zip%e8%a7%a3%e5%87%8d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZendでDbのMeta情報をキャッシュする</title>
		<link>http://life.co-hey.com/2010/09/zend%e3%81%a7db%e3%81%aemeta%e6%83%85%e5%a0%b1%e3%82%92%e3%82%ad%e3%83%a3%e3%83%83%e3%82%b7%e3%83%a5%e3%81%99%e3%82%8b/</link>
		<comments>http://life.co-hey.com/2010/09/zend%e3%81%a7db%e3%81%aemeta%e6%83%85%e5%a0%b1%e3%82%92%e3%82%ad%e3%83%a3%e3%83%83%e3%82%b7%e3%83%a5%e3%81%99%e3%82%8b/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 03:50:46 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=788</guid>
		<description><![CDATA[なんのこっちゃ？って感じですよね。僕もそう思います。 ZendFrameworkを利用する場合、Zend_Db_Table（Row、Rowsetも）を利用することが多いと思います。Zend_Db_Table_Abstra [...]]]></description>
			<content:encoded><![CDATA[<p>
なんのこっちゃ？って感じですよね。僕もそう思います。<br />
<br />
ZendFrameworkを利用する場合、Zend_Db_Table（Row、Rowsetも）を利用することが多いと思います。Zend_Db_Table_Abstractを継承して、テーブル名と他テーブルとのリレーションを定義するだけで、DBの登録、更新、削除ができてしまう優れものです。個人的にはこれだけ、ZendFrameworkを使ってみようってきっかけになりました。<br />
<br />
これらがテーブル名とリレーションだけしか定義してないのに、存在しないカラム名を指定して処理しようとすると、”そんなカラムはないよ！”ってエラーを出してくれます。賢いです。これができるのって、テーブル関連の処理をするたびに&#8221;describe&#8221;が実行されて、テーブルの情報を取得しているからなのです。気がきいてます。<br />
<br />
ただ１点、&#8221;describe&#8221;が実行される量が多すぎるんです。これが処理が遅くなる原因になることがあります。ここででてくるのがDbのMeta情報のキャッシュ。DbのMeta情報というのは、&#8221;describe&#8221;の実行結果です。なので、これをキャッシュとして保持しておくと、&#8221;describe&#8221;をしなくても、情報が手に入るため余計なSQLをDBに実行しなくてすみます。<br />
<br />
たかが&#8221;describe&#8221;ですが、実行回数がほんとに多いので、だいぶ変わります。<br />
Zend_Db_Table使うときは、セットで設定するくらいでいいのではと思っています。<br />
<br />
Bootstrap.phpの中にこんな感じで書いておくと使えます。<br />
<br />

<div class="wp_codebox"><table><tr id="p78828"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p788code28"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _initDbCache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$frontendOptions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'automatic_serialization'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$backendOptions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache_dir'</span> <span style="color: #339933;">=&gt;</span> APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'cacheディレクトリへの相対パス'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> Zend_Cache<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Core'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'File'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$frontendOptions</span><span style="color: #339933;">,</span> <span style="color: #000088;">$backendOptions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Pb_Db_Table_Abstract<span style="color: #339933;">::</span><span style="color: #004000;">setDefaultMetadataCache</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend%E3%81%A7Db%E3%81%AEMeta%E6%83%85%E5%A0%B1%E3%82%92%E3%82%AD%E3%83%A3%E3%83%83%E3%82%B7%E3%83%A5%E3%81%99%E3%82%8B%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F2eszsnx" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/09/zend%e3%81%a7db%e3%81%aemeta%e6%83%85%e5%a0%b1%e3%82%92%e3%82%ad%e3%83%a3%e3%83%83%e3%82%b7%e3%83%a5%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend_Db_Selectのcolumnsメソッドではまった</title>
		<link>http://life.co-hey.com/2010/06/zend_db_select%e3%81%aecolumns%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%81%a7%e3%81%af%e3%81%be%e3%81%a3%e3%81%9f/</link>
		<comments>http://life.co-hey.com/2010/06/zend_db_select%e3%81%aecolumns%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%81%a7%e3%81%af%e3%81%be%e3%81%a3%e3%81%9f/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 01:00:40 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[web & pc]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=677</guid>
		<description><![CDATA[Zend_Db_Selectのメソッドにcolumnsというものがあります。 selectする対象を後付で追加できるメソッドです。使い方はこんな感じ。 1 2 3 4 5 6 7 ※ $adapterは、Zend_Db_ [...]]]></description>
			<content:encoded><![CDATA[<p>
Zend_Db_Selectのメソッドにcolumnsというものがあります。<br />
selectする対象を後付で追加できるメソッドです。使い方はこんな感じ。<br />
<br />

<div class="wp_codebox"><table><tr id="p67737"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p677code37"><pre class="php" style="font-family:Osaka-mono,monospace;">※ <span style="color: #000088;">$adapter</span>は、Zend_Db_Adapter_Pdo_Mysql。
&nbsp;
<span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;account&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">columns</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;update_date&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;account = ?&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;testest&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
<br />
今回やっていたのは、2つのSQL文をUNIONで結合するという内容でした。<br />
取得するカラムが、&#8221;id&#8221;, &#8220;account&#8221;, &#8220;state&#8221;, &#8220;update_date&#8221;として、UNIONで結合するのですが、結合する2つのSQL文の対象となるテーブルに、&#8221;state&#8221;っていうカラムがないために、後付で&#8221;0&#8243;をつけるっていうことをやっていました。（実際は複数のテーブルを結合していて、複雑なので、簡略化してます。）<br />
<br />

<div class="wp_codebox"><table><tr id="p67738"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p677code38"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000088;">$select1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t2&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1.id = t2.account_id&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000088;">$select2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t2&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">columns</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;state&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Db_Expr<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">union</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$select2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
<br />
テーブルのイメージはこう。<br />
<br />

<div class="wp_codebox"><table><tr id="p67739"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p677code39"><pre class="sql" style="font-family:Osaka-mono,monospace;"><span style="color: #993333; font-weight: bold;">create</span> <span style="color: #993333; font-weight: bold;">table</span> hoge_table <span style="color: #66cc66;">&#40;</span>
    id int <span style="color: #66cc66;">,</span> 
    account varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
    state varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
    update_date timestamp<span style="color: #66cc66;">,</span> 
&nbsp;
    <span style="color: #993333; font-weight: bold;">primary</span> <span style="color: #993333; font-weight: bold;">key</span> <span style="color: #ff0000;">`id`</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">create</span> <span style="color: #993333; font-weight: bold;">table</span> hogehoge_table <span style="color: #66cc66;">&#40;</span>
    id int<span style="color: #66cc66;">,</span> 
    account varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
    update_date timestamp<span style="color: #66cc66;">,</span> 
&nbsp;
    <span style="color: #993333; font-weight: bold;">primary</span> <span style="color: #993333; font-weight: bold;">key</span> <span style="color: #ff0000;">`id`</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">create</span> <span style="color: #993333; font-weight: bold;">table</span> hogehogehoge_table <span style="color: #66cc66;">&#40;</span>
    id int <span style="color: #993333; font-weight: bold;">auto_increment</span><span style="color: #66cc66;">,</span> 
    account_id int<span style="color: #66cc66;">,</span> 
    name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>  
&nbsp;
    <span style="color: #993333; font-weight: bold;">primary</span> <span style="color: #993333; font-weight: bold;">key</span> <span style="color: #ff0000;">`id`</span>
<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<br />
<br />
<br />
こういう場合、columnsで追加したカラムは、取得結果でも、後ろに追加した形ででてきます。<br />
<br />

<div class="wp_codebox"><table><tr id="p67740"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p677code40"><pre class="sql" style="font-family:Osaka-mono,monospace;"><span style="color: #808080; font-style: italic;">--------------------------------------------</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> account <span style="color: #66cc66;">|</span> update_date <span style="color: #66cc66;">|</span> name <span style="color: #66cc66;">|</span> state <span style="color: #66cc66;">|</span> 
<span style="color: #808080; font-style: italic;">--------------------------------------------</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">1</span>  <span style="color: #66cc66;">|</span>  test   <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">2010</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">12</span>  <span style="color: #66cc66;">|</span> hoge  <span style="color: #66cc66;">|</span> valid  <span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">---------------------------------------------</span></pre></td></tr></table></div>

<br />
<br />
しかし、もともとテーブルに含まれるものについては、Zend_Db_Selectを利用しているためか、SQL文が作成される際に、並び替えられていて、stateがどの位置にくるか分かりません。<strong>（ここが問題の原因となります。）</strong><br />
しかも、カラム名、カラム数が一致しているのでUNIONにもエラーがでません。<br />
<br />
columnsを利用しなかったSQL文の取得結果が、<br />
<br />

<div class="wp_codebox"><table><tr id="p67741"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p677code41"><pre class="sql" style="font-family:Osaka-mono,monospace;"><span style="color: #808080; font-style: italic;">--------------------------------------------</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> account <span style="color: #66cc66;">|</span> update_date <span style="color: #66cc66;">|</span> state <span style="color: #66cc66;">|</span> name <span style="color: #66cc66;">|</span> 
<span style="color: #808080; font-style: italic;">--------------------------------------------</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">3</span>  <span style="color: #66cc66;">|</span>  test   <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">2010</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">15</span>  <span style="color: #66cc66;">|</span> valid  <span style="color: #66cc66;">|</span> aaaa  <span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">---------------------------------------------</span></pre></td></tr></table></div>

<br />
という順番で取得された場合、UNIONの結果は、<br />
<br />

<div class="wp_codebox"><table><tr id="p67742"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p677code42"><pre class="sql" style="font-family:Osaka-mono,monospace;"><span style="color: #808080; font-style: italic;">--------------------------------------------</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> account <span style="color: #66cc66;">|</span> update_date <span style="color: #66cc66;">|</span> state <span style="color: #66cc66;">|</span> name <span style="color: #66cc66;">|</span> 
<span style="color: #808080; font-style: italic;">--------------------------------------------</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">3</span>  <span style="color: #66cc66;">|</span>  test   <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">2010</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">12</span>  <span style="color: #66cc66;">|</span> valid  <span style="color: #66cc66;">|</span> aaaa  <span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">---------------------------------------------</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">1</span>  <span style="color: #66cc66;">|</span>  test   <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">2010</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">15</span>  <span style="color: #66cc66;">|</span> hoge  <span style="color: #66cc66;">|</span> valid  <span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">---------------------------------------------</span></pre></td></tr></table></div>

<br />
<br />
となって取得されます。<br />
<br />
項目名と違う値が入ってくるのです。今回は、互い違いになったのが数値と日付で、それをsumして合計値を取得してたために、全く予測できない値が取得されてしまいました。<br />
<br />
解決策は、片方でcolumnsを使って、カラム名を追加した場合は、UNIONで結合するSQLは全て、同じカラム名は、columnsを使って追加する方法をとるということです。<br />
<br />
今回の例だと。<br />
<br />

<div class="wp_codebox"><table><tr id="p67743"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p677code43"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000088;">$select1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
                          <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;account&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;update_date&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t2&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">columns</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;state&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;t1.state&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1.id = t2.account_id&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000088;">$select2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t2&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">columns</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;state&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Db_Expr<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">union</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$select2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
と書くことです。または、columnsを使わない形で、このような書き方もできると思います。<br />
<br />

<div class="wp_codebox"><table><tr id="p67744"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p677code44"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000088;">$select1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t2&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1.id = t2.account_id&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000088;">$select2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
                          <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;account&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;state&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Db_Expr<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;update_date&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;t2&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;hogehogehoge_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">columns</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;state&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Db_Expr<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">union</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$select2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
テーブルのカラム数が多いとcolumnsで後付した方が、記述量が少なくなるので、どちらを使うかはその時のテーブルによるかなと思います。重要なのはcolumnsの利用の有無を揃えること。<br />
<br />
<br />
この説明、うまくやるの難しいですね。。。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend_Db_Select%E3%81%AEcolumns%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E3%81%A7%E3%81%AF%E3%81%BE%E3%81%A3%E3%81%9F%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F26ygj6v" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/06/zend_db_select%e3%81%aecolumns%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%81%a7%e3%81%af%e3%81%be%e3%81%a3%e3%81%9f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZendFrameworkを使ってBasic認証を行う</title>
		<link>http://life.co-hey.com/2010/06/zendframework%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6basic%e8%aa%8d%e8%a8%bc%e3%82%92%e8%a1%8c%e3%81%86/</link>
		<comments>http://life.co-hey.com/2010/06/zendframework%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6basic%e8%aa%8d%e8%a8%bc%e3%82%92%e8%a1%8c%e3%81%86/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 02:09:23 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[web & pc]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=632</guid>
		<description><![CDATA[ZendFrameworkで構築したサイトの一部にbasic認証をかけたいけど、どうしたらできるか知ってる？と聞かれたのでやってみました。 考えたことなかったですが、.htaccess使うとindex.phpと同じ階層に [...]]]></description>
			<content:encoded><![CDATA[<p>
ZendFrameworkで構築したサイトの一部にbasic認証をかけたいけど、どうしたらできるか知ってる？と聞かれたのでやってみました。<br />
考えたことなかったですが、.htaccess使うとindex.phpと同じ階層に置くことになって、サイト全体にしかかけれないのかもしれません。試してはないので推測です。<br />
<br />
やりたかった内容は、特定のURLにbasic認証をかけて、認証NGな場合は他の画面を表示し、認証OKな場合は、そのまま指定されたURLの画面を表示するというものです。<br />
<br />
サンプルソースは、以下になります。<br />
今回は、controller内に全部書きました。<br />
<br />

<div class="wp_codebox"><table><tr id="p63247"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p632code47"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'accept_schemes'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'basic'</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// basic認証指定</span>
            <span style="color: #0000ff;">'realm'</span>           <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'aaaaa'</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// realm(passwd.txtの内容と一致させる必要あり)</span>
            <span style="color: #0000ff;">'digest_domains'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'/login/'</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// このURL以下には全てbasic認証要</span>
            <span style="color: #0000ff;">'nonce_timeout'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3600</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$resolver</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Adapter_Http_Resolver_File<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$resolver</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'passwd.txt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// index.phpがある階層と同じところに置いてます。</span>
&nbsp;
        <span style="color: #000088;">$adapter</span>  <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Adapter_Http<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBasicResolver</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resolver</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequest</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setResponse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 認証エラーの場合</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_forward<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test1'</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;">// 認証OK</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
<br />
<br />
passwd.txtの中身はこちら<br />
<br />

<div class="wp_codebox"><table><tr id="p63248"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p632code48"><pre class="php" style="font-family:Osaka-mono,monospace;">test<span style="color: #339933;">:</span>aaaaa<span style="color: #339933;">:</span>hogehoge</pre></td></tr></table></div>

<br />
<br />
左から、アカウント、realm、パスワードです。パスワードは生パスワードでOKです。<br />
realmは、はプログラム内で指定したものと一致する必要があります。<br />
アカウントとrealmが一対となっていて、それに対してパスワードが存在するイメージです。<br />
<br />
basic認証の画面は、authenticate()の部分で生成され、Zend_Controller_Response_Httpに設定されています。<br />
authenticate()の部分で実行されるということは、認証画面が表示される際は、必ず &#8220;認証エラー&#8221; のルートを通ることになります。<br />
なので、このルートを単なるエラールートと捉えない方がいいです。例えば、Exceptionをthrowしたりしたら、必ずthrowされます。<br />
<br />
あと、はまった点が１つありました。&#8221;認証エラー&#8221;のルートに $this->_redirect()を利用すると、期待する動きはしません。<br />
basic認証画面の出力より、リダイレクト処理が優先されてしまい、常にリダイレクトされることになります。<br />
そのため、上記サンプルでは $this->_forward()を利用しています。これなら、認証画面にてキャンセルを押した場合にforward先の画面が出力されます。<br />
<br />
Zendのソースを見つつ、認証画面にてキャンセルを押された場合を検出しようとしたんですが、判別の方法がわかりませんでした。<br />
認証画面をキャンセルした際に、サーバ側にリクエストがきてないようだったので、サーバ側で判別するのは無理と判断しました。<br />
今回は、Responseにbasic認証の情報と、forwardの情報を両方設定し、basic認証がキャンセルされたら、forwardが実行される形になりました。<br />
</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20ZendFramework%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6Basic%E8%AA%8D%E8%A8%BC%E3%82%92%E8%A1%8C%E3%81%86%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F2al6dch" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/06/zendframework%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6basic%e8%aa%8d%e8%a8%bc%e3%82%92%e8%a1%8c%e3%81%86/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zendを使ってZip圧縮</title>
		<link>http://life.co-hey.com/2010/05/zend%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6zip%e5%9c%a7%e7%b8%ae/</link>
		<comments>http://life.co-hey.com/2010/05/zend%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6zip%e5%9c%a7%e7%b8%ae/#comments</comments>
		<pubDate>Fri, 28 May 2010 02:18:33 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[web & pc]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=608</guid>
		<description><![CDATA[ZendFramework使って、ディレクトリのzipってできますか？ と聞かれたので調べたらでてきた。しかも、filterのところで。 filterは、validateが不正データを検出するのに対して、フィルタリングす [...]]]></description>
			<content:encoded><![CDATA[<p>
ZendFramework使って、ディレクトリのzipってできますか？<br />
と聞かれたので調べたらでてきた。しかも、filterのところで。<br />
<br />
filterは、validateが不正データを検出するのに対して、フィルタリングするってイメージで、入力チェックのイメージが強かったから意外だった。<br />
<br />
filterを通すことで、データを変換するってイメージならファイル圧縮がfilterにあるのも納得できる。あー、なるほどねーって思わされた。<br />
<br />
使い方は、こんな感じ。<br />
<br />

<div class="wp_codebox"><table><tr id="p60851"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p608code51"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Compress<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setArchive</span><span style="color: #009900;">&#40;</span>作成したい圧縮ファイルのパス付ファイル名<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span>圧縮したいファイルのパス付ファイル名<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
<br />
他にもこんな書き方もできそう。<br />
<br />

<div class="wp_codebox"><table><tr id="p60852"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p608code52"><pre class="php" style="font-family:Osaka-mono,monospace;"><span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_Compress<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
     <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Zip'</span><span style="color: #339933;">,</span> 
     <span style="color: #0000ff;">'options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'archive'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'作成したい圧縮ファイルのパス付ファイル名'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span>圧縮したいファイルのパス付ファイル名<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<br />
<br />
<br />
中身は、ZipArchiveクラス使ってました。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6Zip%E5%9C%A7%E7%B8%AE%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F275bcue" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/05/zend%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6zip%e5%9c%a7%e7%b8%ae/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend_Db_SelectでUNIONとHAVINGを同時に使えない</title>
		<link>http://life.co-hey.com/2010/04/zend_db_select%e3%81%a7union%e3%81%a8having%e3%82%92%e5%90%8c%e6%99%82%e3%81%ab%e4%bd%bf%e3%81%88%e3%81%aa%e3%81%84/</link>
		<comments>http://life.co-hey.com/2010/04/zend_db_select%e3%81%a7union%e3%81%a8having%e3%82%92%e5%90%8c%e6%99%82%e3%81%ab%e4%bd%bf%e3%81%88%e3%81%aa%e3%81%84/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 06:37:31 +0000</pubDate>
		<dc:creator>co-hey</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[web & pc]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://life.co-hey.com/?p=585</guid>
		<description><![CDATA[訂正：2010/04/09 通りすがりさんにコメントを頂き、UNIONで連結した全体にhavingをかけるのはできないということで正しいということが分かりました。 たまたま、$select2の方にだけhavingでフィル [...]]]></description>
			<content:encoded><![CDATA[<p>
<strong>訂正：2010/04/09</strong><br />
<br />
通りすがりさんにコメントを頂き、UNIONで連結した全体にhavingをかけるのはできないということで正しいということが分かりました。<br />
<br />
たまたま、$select2の方にだけhavingでフィルターされるデータがあったため、結果的に意図通りのデータが出力されただけで、全体にhavingが掛かっているわけではありませんでした。<br />
<br />
サブクエリを使う方法が適切です。<br />
<br />

<div class="wp_codebox"><table><tr id="p58558"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p585code58"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #666666; font-style: italic;">// $adapterはZend_Db_Adapterのクラス。MySQLを利用。</span>
    <span style="color: #000088;">$select1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$select2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">union</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$select2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ←havingで書いてた条件</pre></td></tr></table></div>

<br />
<br />
=========<br />
<br />
UNIONとHAVINGを同時に使うSQLを組んでいたときに、Zend_Db_Selectから生成されたSQLからHAVING句が抜け落ちていました。<br />
<br />
プログラムは以下のようなものです。<br />
<br />

<div class="wp_codebox"><table><tr id="p58559"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p585code59"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #666666; font-style: italic;">// $adapterはZend_Db_Adapterのクラス。MySQLを利用。</span>
    <span style="color: #000088;">$select1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$select2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">union</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$select2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">having</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.....</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$select</span><span style="color: #339933;">;</span>   ← ここで HAVING句がなくなってる</pre></td></tr></table></div>

<br />
<br />
上記でできるSQLのイメージは、これです。<br />
<br />

<div class="wp_codebox"><table><tr id="p58560"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p585code60"><pre class="php" style="font-family:Osaka-mono,monospace;">   select <span style="color: #339933;">......</span> from <span style="color: #339933;">....</span> where <span style="color: #339933;">....</span>
   union
   select <span style="color: #339933;">......</span> from <span style="color: #339933;">....</span> where <span style="color: #339933;">....</span>
   having <span style="color: #339933;">.....</span></pre></td></tr></table></div>

<br />
<br />
このSQLをDBサーバで実行すると、動くので構文はあってます。<br />
<br />
そこで、Zend_Db_Selectクラスのhaving句を出力するところを見たら、こうなってました。(バージョンは10.2）<br />
<br />

<div class="wp_codebox"><table><tr id="p58561"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p585code61"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #009933; font-style: italic;">/**
     * Render HAVING clause
     *
     * @param string   $sql SQL query
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _renderHaving<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</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>_parts<span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">FROM</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_parts<span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">HAVING</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$sql</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">SQL_HAVING</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_parts<span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">HAVING</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        Zend_Debug<span style="color: #339933;">::</span><span style="color: #004000;">dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
<br />
FROM句とHAVING句が両方ある場合に、HAVINGをつけるって処理ですね。確かにこれだと、UNIONと同時に使ったときは、FROM句がないので、HAVING句がつかないのは納得です。<br />
<br />
というわけで、以下のように修正して、UNIONとHAVINGが同時に使えることを確認しました。<br />
<br />

<div class="wp_codebox"><table><tr id="p58562"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p585code62"><pre class="php" style="font-family:Osaka-mono,monospace;">    <span style="color: #009933; font-style: italic;">/**
     * Render HAVING clause
     *
     * @param string   $sql SQL query
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _renderHaving<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// FROMかUNIONがあればOKに変更</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_parts<span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">FROM</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_parts<span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">UNION</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_parts<span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">HAVING</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$sql</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">SQL_HAVING</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_parts<span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">HAVING</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        Zend_Debug<span style="color: #339933;">::</span><span style="color: #004000;">dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<br />
<br />
そのうちZend本体でも実装してくれることに期待します。</p>

<a href="http://twitter.com/?status=RT%20%40%3A%20Zend_Db_Select%E3%81%A7UNION%E3%81%A8HAVING%E3%82%92%E5%90%8C%E6%99%82%E3%81%AB%E4%BD%BF%E3%81%88%E3%81%AA%E3%81%84%20-%20Life%20http%3A%2F%2Ftinyurl.com%2F23eofym" class="tweet-this" ><img src="http://life.co-hey.com/wp-content/plugins/simple-tweet/img/tweet.gif" title="つぶやく" alt="つぶやく" /></a>]]></content:encoded>
			<wfw:commentRss>http://life.co-hey.com/2010/04/zend_db_select%e3%81%a7union%e3%81%a8having%e3%82%92%e5%90%8c%e6%99%82%e3%81%ab%e4%bd%bf%e3%81%88%e3%81%aa%e3%81%84/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

