<?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/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>無限なる開発ブログ &#187; iPhone</title>
	<atom:link href="http://www.oto-kichi.com/category/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.oto-kichi.com</link>
	<description>ロサンゼルス在住ITエンジニアの開発ブログ</description>
	<lastBuildDate>Thu, 21 Apr 2011 00:33:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.oto-kichi.com/category/iphone/feed/" />
		<item>
		<title>[CakePHP] JSON出力時にstring型をint型に戻す</title>
		<link>http://www.oto-kichi.com/2010/12/18/cakephp-json%e5%87%ba%e5%8a%9b%e6%99%82%e3%81%abstring%e5%9e%8b%e3%82%92int%e5%9e%8b%e3%81%ab%e6%88%bb%e3%81%99/</link>
		<comments>http://www.oto-kichi.com/2010/12/18/cakephp-json%e5%87%ba%e5%8a%9b%e6%99%82%e3%81%abstring%e5%9e%8b%e3%82%92int%e5%9e%8b%e3%81%ab%e6%88%bb%e3%81%99/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 05:05:33 +0000</pubDate>
		<dc:creator>Viruke</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.oto-kichi.com/?p=174</guid>
		<description><![CDATA[久々のブログ、今回はCakePHPでJSON出力をするときにInteger型、String型に型変換して出力する方法について書きます。 （あ、今回も他力本願、満載） まず、CakePHPでJSON出力をする為には Con [...]]]></description>
			<content:encoded><![CDATA[
<!-- wp-jquery-lightbox, a WordPress plugin by ulfben --> 
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.oto-kichi.com%2F2010%2F12%2F18%2Fcakephp-json%25e5%2587%25ba%25e5%258a%259b%25e6%2599%2582%25e3%2581%25abstring%25e5%259e%258b%25e3%2582%2592int%25e5%259e%258b%25e3%2581%25ab%25e6%2588%25bb%25e3%2581%2599%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.oto-kichi.com%2F2010%2F12%2F18%2Fcakephp-json%25e5%2587%25ba%25e5%258a%259b%25e6%2599%2582%25e3%2581%25abstring%25e5%259e%258b%25e3%2582%2592int%25e5%259e%258b%25e3%2581%25ab%25e6%2588%25bb%25e3%2581%2599%2F&amp;source=Viruke&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>久々のブログ、今回はCakePHPでJSON出力をするときにInteger型、String型に型変換して出力する方法について書きます。 （あ、今回も他力本願、満載）</p>
<p>まず、CakePHPでJSON出力をする為には</p>
<p>Controllerで</p>
<pre class="brush:php">$yourJsonObect= $this-&gt;YourModel-&gt;find('all');
$this-&gt;set('yourJsonObect', $yourJsonObect);</pre>
<p>としてやり</p>
<p>Viewで</p>
<pre class="brush:php">&lt;?php echo $javascript-&gt;object($yourJsonObect); ?&gt;</pre>
<p>としてやれば簡単に実現できます。</p>
<p>出力されるコードは以下のようになります。</p>
<pre class="brush:js">[{"YourModel":{"id":"10060","foreign_id":"1","short_description":"Short Desc Test.","long_description":"Long Desc Test"},
{}
.....
)]</pre>
<p>これで、殆ど場合は問題ないのですが、データベース上でIntegerで持っているスキーマ（id, foreign_idなど）も全てString型として出力されます。</p>
<p>もし、JSONデータを受け取る側が厳密にInteger型、String型を区別して受け取りたい場合などに以下の方法でスマートに解決できたので紹介します。</p>
<p>余談ですが、自分の場合は、iPhone開発において、JSONデータをウェブサーバーから受け取り、Core DataにInsertする時に、iPhone側で型チェックや型変換をさせたくなかったので、サーバー側で吸収してしまおうという経緯がありました。</p>
<p>参考にしたサイトはこちらです。</p>
<blockquote><p>CakePHPのmodelの結果配列でstring型をint型に戻す。</p>
<p>CakePHPでmodel::findなどの関数で結果配列を取得したときに、データベースではint型の値ｓｔｒｉｎｇ型として返って来ます。</p>
<p>今回は、flashにjson出力でデータを渡したかったので、別に文字列型のまま渡しても問題ないのかもしれないけど、なんか気持ち悪い。</p>
<p>それに例えばよくある、intの0,1をフラグと扱って if (flag) {&#8230;. }なんて書いたらマズいんじゃないかと思うので、settypeで型変換を行う方法。</p>
<p>引用元: <a href="http://roany.cocolog-nifty.com/diary/2010/05/cakephpmodelstr.html">CakePHPのmodelの結果配列でstring型をint型に戻す。: ろーにーの日記</a>.</p></blockquote>
<p>モデルの親クラスで、Findで取得したデータ（デフォルトは全てString）をデータベースのTypeを参照して、型変換を行っています。</p>
<p>app_model.phpにafterFind（）オーバーライド関数を追加してやるだけ。</p>
<pre class="brush:php">class AppModel extends Model{
	public function afterFind($results, $primary = false) {
		parent::afterFind($results, $primary);
		$columnTypes = $this-&gt;getColumnTypes();
		foreach($results as $index =&gt; $result) {
			foreach($columnTypes as $field =&gt; $columnType) {
				if($columnType == 'integer') {
					if(isset($results[$index][$this-&gt;name][$field])) {
						settype($results[$index][$this-&gt;name][$field], 'integer');
					}
				}
			}
		}
		return $results;
	}
}</pre>
<p>これで全てのモデルが自動変換されるようになりました。   感激～</p>
<p>変換後のJSON出力を見ると以下のようになります。</p>
<pre class="brush:js">[{"YourModel":{"id":10060,"foreign_id":1,"short_description":"Short Desc Test.","long_description":"Long Desc Test"},
{}
.....
)]</pre>
<p>Integer型の数字の部分からダブルクォーテーションが消えました。</p>
<p>めでたしめでたし。</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">http://app.cocolog-nifty.com/t/trackback/120702/48427427</div>
]]></content:encoded>
			<wfw:commentRss>http://www.oto-kichi.com/2010/12/18/cakephp-json%e5%87%ba%e5%8a%9b%e6%99%82%e3%81%abstring%e5%9e%8b%e3%82%92int%e5%9e%8b%e3%81%ab%e6%88%bb%e3%81%99/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.oto-kichi.com/2010/12/18/cakephp-json%e5%87%ba%e5%8a%9b%e6%99%82%e3%81%abstring%e5%9e%8b%e3%82%92int%e5%9e%8b%e3%81%ab%e6%88%bb%e3%81%99/" />
	</item>
		<item>
		<title>iPhoneでFlashのネイティブ対応が可能に</title>
		<link>http://www.oto-kichi.com/2009/10/11/iphone%e3%81%a7flash%e3%81%ae%e3%83%8d%e3%82%a4%e3%83%86%e3%82%a3%e3%83%96%e5%af%be%e5%bf%9c%e3%81%8c%e5%8f%af%e8%83%bd%e3%81%ab/</link>
		<comments>http://www.oto-kichi.com/2009/10/11/iphone%e3%81%a7flash%e3%81%ae%e3%83%8d%e3%82%a4%e3%83%86%e3%82%a3%e3%83%96%e5%af%be%e5%bf%9c%e3%81%8c%e5%8f%af%e8%83%bd%e3%81%ab/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 10:09:13 +0000</pubDate>
		<dc:creator>Viruke</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.oto-kichi.com/?p=91</guid>
		<description><![CDATA[Appleの「iPhone」上で、Flashアプリケーションの対応を図れるようになると明らかにした。 引用元: アドビ、「Flash Professional CS5」を発表&#8211;iPhoneでFlashのネイテ [...]]]></description>
			<content:encoded><![CDATA[
<!-- wp-jquery-lightbox, a WordPress plugin by ulfben --> 
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.oto-kichi.com%2F2009%2F10%2F11%2Fiphone%25e3%2581%25a7flash%25e3%2581%25ae%25e3%2583%258d%25e3%2582%25a4%25e3%2583%2586%25e3%2582%25a3%25e3%2583%2596%25e5%25af%25be%25e5%25bf%259c%25e3%2581%258c%25e5%258f%25af%25e8%2583%25bd%25e3%2581%25ab%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.oto-kichi.com%2F2009%2F10%2F11%2Fiphone%25e3%2581%25a7flash%25e3%2581%25ae%25e3%2583%258d%25e3%2582%25a4%25e3%2583%2586%25e3%2582%25a3%25e3%2583%2596%25e5%25af%25be%25e5%25bf%259c%25e3%2581%258c%25e5%258f%25af%25e8%2583%25bd%25e3%2581%25ab%2F&amp;source=Viruke&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<blockquote><p>Appleの「iPhone」上で、Flashアプリケーションの対応を図れるようになると明らかにした。</p>
<p>引用元: <a href="http://japan.cnet.com/apple/story/0,2000076557,20401128,00.htm">アドビ、「Flash Professional CS5」を発表&#8211;iPhoneでFlashのネイティブ対応が可能に:アップルセンター &#8211; CNET Japan</a>.</p></blockquote>
<p>これでFlashプログラマーがiPhoneアプリの開発に参入できることになりますね。</p>
<p>Flashから常に敬遠していた自分は負け組みだぁ～(/_;)</p>
<p>今からでも勉強していこうかな・・・ でもうちはデザイン会社じゃないって理由でAdobeの高価なソフトは買ってもらえそうにないので、触るとしたらFlex SDKを使うんだろうけど、これ以上覚える言語を増やしていいのか？と頭の中で警告が鳴る。</p>
<p>それだったらやっぱりiPhoneの開発プラットフォームであるObjective-Cの方が断然興味あり。</p>
<p>あと、やっぱり気になるのがこれ。</p>
<blockquote><p>Adobeの究極の目標は現在も変わっておらず、Flashを「Safari」ブラウザに統合してiPhoneで提供することが目指されている。同社最高 技術責任者（CTO）のKevin Lynch氏は、インタビューに応えて「ただ1つのウェブのみが存在するということこそ私の願いである」と述べた。</p></blockquote>
<p>iPhone SafariのFlashプラグインはまだ先になりそうですが、近い将来に是非実現してほしいものです。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oto-kichi.com/2009/10/11/iphone%e3%81%a7flash%e3%81%ae%e3%83%8d%e3%82%a4%e3%83%86%e3%82%a3%e3%83%96%e5%af%be%e5%bf%9c%e3%81%8c%e5%8f%af%e8%83%bd%e3%81%ab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.oto-kichi.com/2009/10/11/iphone%e3%81%a7flash%e3%81%ae%e3%83%8d%e3%82%a4%e3%83%86%e3%82%a3%e3%83%96%e5%af%be%e5%bf%9c%e3%81%8c%e5%8f%af%e8%83%bd%e3%81%ab/" />
	</item>
	</channel>
</rss>

