<?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>へっぽこ開発室 &#187; Zend_Db</title>
	<atom:link href="http://wp.foliz.net/archives/tag/zend_db/feed" rel="self" type="application/rss+xml" />
	<link>http://wp.foliz.net</link>
	<description>webプログラム　php,Smarty,Zend Framework,ajax,pearネタなど～</description>
	<lastBuildDate>Sat, 21 Jan 2012 06:32:52 +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_Db quoteInto</title>
		<link>http://wp.foliz.net/archives/63</link>
		<comments>http://wp.foliz.net/archives/63#comments</comments>
		<pubDate>Thu, 22 Nov 2007 01:35:33 +0000</pubDate>
		<dc:creator>Fou</dc:creator>
				<category><![CDATA[ZendFramework]]></category>
		<category><![CDATA[Zend_Db]]></category>

		<guid isPermaLink="false">http://wp.foliz.net/archives/63</guid>
		<description><![CDATA[updateでquoteIntoを使ったwhere条件が複数ある場合、記述方法をよく忘れるのでメモ。 // 複数の条件を AND で組み合わせます $where = array&#40; $db-&#62;quoteInt [...]]]></description>
			<content:encoded><![CDATA[<p>updateでquoteIntoを使ったwhere条件が複数ある場合、記述方法をよく忘れるのでメモ。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 複数の条件を AND で組み合わせます</span>
<span style="color: #000088;">$where</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteInto</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first_name = ?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$firstName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteInto</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'noble_title = ?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nobleTitle</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$count</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'round_table'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$set</span><span style="color: #339933;">,</span> <span style="color: #000088;">$where</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 複数の条件を OR で組み合わせます</span>
<span style="color: #000088;">$firstNameOne</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstNameOne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$firstNameTwo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstNameTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$where</span>        <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;first_name = <span style="color: #006699; font-weight: bold;">$firstNameOne</span> OR first_name = <span style="color: #006699; font-weight: bold;">$firstNameTwo</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$count</span>        <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'round_table'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$set</span><span style="color: #339933;">,</span> <span style="color: #000088;">$where</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://wp.foliz.net/archives/63/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend_Db</title>
		<link>http://wp.foliz.net/archives/54</link>
		<comments>http://wp.foliz.net/archives/54#comments</comments>
		<pubDate>Thu, 04 Oct 2007 05:49:58 +0000</pubDate>
		<dc:creator>Fou</dc:creator>
				<category><![CDATA[ZendFramework]]></category>
		<category><![CDATA[Zend_Db]]></category>

		<guid isPermaLink="false">http://wp.foliz.net/archives/54</guid>
		<description><![CDATA[ちょこっと便利そうな関数のメモ 例 10.18. 自動インクリメントのキーにおける lastInsertId() の使用法 &#60; ?php $db-&#62;insert&#40;'bugs', $data&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>ちょこっと便利そうな関数のメモ</p>
<p>例 10.18. 自動インクリメントのキーにおける lastInsertId() の使用法</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bugs'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//自動インクリメントのカラムで最後に生成された値を返します</span>
<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastInsertId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>そのテーブルで自動インクリメントの主キーを定義している場合は、 データを追加した後で lastInsertId() メソッドを使用することができます。このメソッドは、 現在のデータベース接続において最後に自動生成された値を返します。</p>
<p>同じく主キー以外は<br />
<code>$id = $db->lastSequenceId('bugs_id_gen');</code><br />
で取得できる模様</p>
<p>quote() の返り値には、文字列の両端に区切り文字が追加されていることに注意しましょう。 これは、たとえば <a href="http://www.php.net/mysqli_real_escape_string" title="mysql_real_escape_string()">mysql_real_escape_string()</a>のようなエスケープ用関数の挙動とは異なります。</p>
<p>トランザクション</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">//トランザクションを明示的に開始します</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
try <span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">// いくつかクエリを実行します</span>
     <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</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;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</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;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</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: #666666; font-style: italic;">// すべて成功したら、トランザクションをコミットして</span>
     <span style="color: #666666; font-style: italic;">// すべての変更を一度に適用します</span>
     <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// いずれかのクエリが失敗して例外が発生したら、</span>
     <span style="color: #666666; font-style: italic;">// もし他に成功しているクエリがあったとしても</span>
    <span style="color: #666666; font-style: italic;">// それも含めてすべての処理をロールバックします。</span>
    <span style="color: #666666; font-style: italic;">// すべて適用されるか、ひとつも適用されないかのいずれかです。</span>
    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rollBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a href="http://framework.zend.com/manual/ja/zend.db.select.html" title="Zend_Db_Select">Zend_Db_Select</a>オブジェクト</p>
<p>メソッドを使用して句を追加する例</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">// Zend_Db_Select オブジェクトを作成します</span>
<span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</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;">;</span>
<span style="color: #666666; font-style: italic;">// FROM 句を追加します</span>
<span style="color: #000088;">$select</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: #339933;">...</span> <span style="color: #009900;">&#41;</span>
<span style="color: #666666; font-style: italic;">// WHERE 句を追加します</span>
<span style="color: #000088;">$select</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: #339933;">...</span> <span style="color: #009900;">&#41;</span>
<span style="color: #666666; font-style: italic;">// ORDER BY 句を追加します</span>
<span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">order</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">...</span>ソート条件を指定します<span style="color: #339933;">...</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>また個々の句に対してメソッドが用意されている。<br />
from()<br />
join()<br />
where()<br />
orWhere()<br />
group()<br />
having()<br />
order()<br />
limit()<br />
limitPage()<br />
distinct()<br />
など</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">// できあがるクエリは、このようになります</span>
<span style="color: #666666; font-style: italic;">//   SELECT p.&quot;product_id&quot;, p.&quot;product_name&quot;</span>
<span style="color: #666666; font-style: italic;">//   FROM &quot;products&quot; AS p</span>
<span style="color: #666666; font-style: italic;">//   LIMIT 10, 20</span>
<span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</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;">'p'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'products'</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;">'product_id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'product_name'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">limitPage</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>使いこなせば便利そう・・・使い慣れない間はSQLをそのまま記述したほうが早そう・・・＾＾；<br />
具体的な使用例は使いながらＵＰしていくつもり。</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.foliz.net/archives/54/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

