<?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; redirect</title>
	<atom:link href="http://webmaster.verybs.com/tag/redirect/feed/" rel="self" type="application/rss+xml" />
	<link>http://webmaster.verybs.com</link>
	<description>所有的都是幻觉，是幻觉！你吓不倒我的！</description>
	<lastBuildDate>Thu, 06 Aug 2009 11:20:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Going for Symfony &#124; 第4.5天</title>
		<link>http://webmaster.verybs.com/2009/02/10/going-for-symfony-%e7%ac%ac45%e5%a4%a9/</link>
		<comments>http://webmaster.verybs.com/2009/02/10/going-for-symfony-%e7%ac%ac45%e5%a4%a9/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 02:01:49 +0000</pubDate>
		<dc:creator>TR@SOE</dc:creator>
				<category><![CDATA[编程、软件、技术]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://webmaster.verybs.com/2009/02/10/going-for-symfony-%e7%ac%ac45%e5%a4%a9/</guid>
		<description><![CDATA[在第四天的心得中，我留下了一个小问题：
一个小bug：现在的情况是，只能用点击那个按钮的方式来提交新的tags，如果输入了一些tags然后用回车的话，会出错。这个问题我将留在日后解决。先让我得意一会吧……

当时我认为这个问题很容易解决，因为我想到了至少两种解决方法：

在jQuery中截获回车，然后不加任何处理；
在action.class.php中进行进一步的操作；

实践证明，第一种方法没有成功，因为我对js的掌握还是没有我对PHP的掌握来的深入。所以我集中在第二种方法。

首先，不论是按回车还是点击按钮，其后台处理方式都是基本一致的。但是，在点击按钮的时候，我需要使用AJAX处理；而在按回车时，是一个标准的POST的处理，牵涉到后台处理和处理完毕后的重定向。
其次，在实践中，发现addBookTaglist并不能很好的判断重复的id+tag。原因在于在我的处理过程中对于每个tag都创建了一个新的BookTagList对象，这个对象和已有的对象必定是&#8221;不同&#8221;的：Symfony后台的判断只是用了in_array，而没有用严格等于判断。这个错误我是通过Fx下的Firebug调试发现的，感谢Fx，感谢Firebug……
因此，我先改写了doUpdateTags函数：

//BookBookPeer.php&#60;br&#62;    static public function doUpdateTags($id, $tags)&#60;br&#62;    {&#60;br&#62;        $book=BookBookPeer::retrieveByPk($id);&#60;br&#62;        $currentTagLists=$book-&#38;gt;getBookTaglists();&#60;br&#62;        $tmp=array();&#60;br&#62;        foreach ($currentTagLists as $value)&#60;br&#62;        [...]]]></description>
			<content:encoded><![CDATA[<p>在<a href="http://www.rsywx.net/wordpress/2009/02/1441">第四天</a>的心得中，我留下了一个小问题：</p>
<blockquote><p>一个小bug：现在的情况是，只能用点击那个按钮的方式来提交新的tags，如果输入了一些tags然后用回车的话，会出错。这个问题我将留在日后解决。先让我得意一会吧……</p>
</blockquote>
<p>当时我认为这个问题很容易解决，因为我想到了至少两种解决方法：</p>
<ol>
<li>在jQuery中截获回车，然后不加任何处理；</li>
<li>在action.class.php中进行进一步的操作；</li>
</ol>
<p>实践证明，第一种方法没有成功，因为我对js的掌握还是没有我对PHP的掌握来的深入。所以我集中在第二种方法。</p>
<p><span id="more-30"></span></p>
<p>首先，不论是按回车还是点击按钮，其后台处理方式都是基本一致的。但是，在点击按钮的时候，我需要使用AJAX处理；而在按回车时，是一个标准的POST的处理，牵涉到后台处理和处理完毕后的重定向。</p>
<p>其次，在实践中，发现addBookTaglist并不能很好的判断重复的id+tag。原因在于在我的处理过程中对于每个tag都创建了一个新的BookTagList对象，这个对象和已有的对象必定是&#8221;不同&#8221;的：Symfony后台的判断只是用了in_array，而没有用严格等于判断。这个错误我是通过Fx下的Firebug调试发现的，感谢Fx，感谢Firebug……</p>
<p>因此，我先改写了doUpdateTags函数：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//BookBookPeer.php&lt;br&gt;    static public function doUpdateTags($id, $tags)&lt;br&gt;    {&lt;br&gt;        $book=BookBookPeer::retrieveByPk($id);&lt;br&gt;        $currentTagLists=$book-&amp;gt;getBookTaglists();&lt;br&gt;        $tmp=array();&lt;br&gt;        foreach ($currentTagLists as $value)&lt;br&gt;        {&lt;br&gt;            $tmp[]=$value-&amp;gt;getTag();&lt;br&gt;        }&lt;br&gt;        $tagslist=explode(&quot; &quot;, $tags);&lt;br&gt;        foreach ($tagslist as $tag)&lt;br&gt;        {&lt;br&gt;            if(!in_array($tag, $tmp))&lt;br&gt;            {&lt;br&gt;                $tl=new BookTaglist();&lt;br&gt;                $tl-&amp;gt;setId($id);&lt;br&gt;                $tl-&amp;gt;setTag($tag);&lt;br&gt;                $book-&amp;gt;addBookTaglist($tl);&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;        $book-&amp;gt;save();&lt;br&gt;        return $book-&amp;gt;getBookTaglists();&lt;br&gt;    }</span></pre></div></div>

<p>然后我要修改action.class.php中的executeUpdatetags函数：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeUpdatetags<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>    <span style="color: #009900;">&#123;</span><span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>isXmlHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>        <span style="color: #009900;">&#123;</span><span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>            <span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getParameter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;</span>br<span style="color: #339933;">&gt;</span>            <span style="color: #000088;">$tags</span><span style="color: #339933;">=</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getParameter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;</span>br<span style="color: #339933;">&gt;</span>            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>renderPartial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'books/tags'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tags'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span>BookBookPeer<span style="color: #339933;">::</span><span style="color: #004000;">doUpdateTags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tags</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;</span>br<span style="color: #339933;">&gt;</span>        <span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>        else<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>        <span style="color: #009900;">&#123;</span><span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>            <span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getParameter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'txtBookID'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;</span>br<span style="color: #339933;">&gt;</span>            <span style="color: #000088;">$tags</span><span style="color: #339933;">=</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getParameter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'txtNewTag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;</span>br<span style="color: #339933;">&gt;</span>            BookBookPeer<span style="color: #339933;">::</span><span style="color: #004000;">doUpdateTags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tags</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;</span>br<span style="color: #339933;">&gt;</span>            <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>book<span style="color: #339933;">=</span>BookBookPeer<span style="color: #339933;">::</span><span style="color: #004000;">retrieveByPk</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;</span>br<span style="color: #339933;">&gt;</span>            <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>redirect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>generateUrl<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book_detail'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;</span>br<span style="color: #339933;">&gt;</span>        <span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;</span>br<span style="color: #339933;">&gt;</span>    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>我用到了isXmlHttpRequest来判断是否是AJAX调用，如果是的话，那么按照AJAX的处理方式；否则就是常规的POST操作：获得参数，进行更新（复用了doUpdateTags函数），重新获得书籍对象，最后是重定向。</p>
<p>这里，我花费了不少时间去找到这个重定向中的URL生成函数，多亏了Symfony的教程。</p>
]]></content:encoded>
			<wfw:commentRss>http://webmaster.verybs.com/2009/02/10/going-for-symfony-%e7%ac%ac45%e5%a4%a9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
