<?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>Rodrigo Pérez (bilson)</title> <atom:link href="http://bilson.cl/feed/" rel="self" type="application/rss+xml" /><link>http://bilson.cl</link> <description>blog personal</description> <lastBuildDate>Thu, 01 Sep 2011 19:43:27 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>HABTM search by multiple tags in CakePHP</title><link>http://bilson.cl/2010/12/habtm-search-by-multiple-tags-in-cakephp/</link> <comments>http://bilson.cl/2010/12/habtm-search-by-multiple-tags-in-cakephp/#comments</comments> <pubDate>Fri, 03 Dec 2010 18:47:50 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[CakePHP]]></category> <category><![CDATA[habtm]]></category><guid
isPermaLink="false">http://bilson.cl/?p=88</guid> <description><![CDATA[The Problem A client came to me with an interesting request. They wanted the ability to search a songs database based on tags that defined that song. A similar problem would be faced with a real estate portal trying to search properties that have specific amenities, or posts with certain tags, etc.. etc&#8230; The concept [...]]]></description> <content:encoded><![CDATA[<h2>The Problem</h2><p>A client came to me with an interesting request. They wanted the ability to search a songs database based on tags that defined that song. A similar problem would be faced with a real estate portal trying to search properties that have specific amenities, or posts with certain tags, etc.. etc&#8230;</p><p>The concept is fairly simple, but one that can be a bit tricky to do with a HABTM relationship in CakePHP. This isn&#8217;t just a tricky problem in CakePHP, its a tricky problem on a database level.</p><p>Now searching for all songs based on a single tag is easy enough &#8212; set the join, set the condition, everyone eat cake. However, searching based on multiple tags is a little more involved.</p><h2>The setup and scope</h2><p>Consider the following song list and their related tags (ie &#8230; SongName (Tag1,Tag2,Tag3&#8230;.)):</p><p>* Song1 (Drum Intro, Guitar Solo, No Vocal)<br
/> * Song2 (Drum Intro)<br
/> * Song3 (Guitar Solo, No Vocal)<br
/> * Song4 (Drum Intro, No Vocal)</p><p>Now, I want an easy way to get all songs based on an array of tags (ie &#8230; Search(Tag1,Tag2&#8230;.) = Result Set):</p><p>* Search(Drum Intro, No Vocal) = Song1 and Song4<br
/> * Search(Guitar Solo) = Song1, Song3<br
/> * Search(Drum Intro, Guitar Solo) = no results<br
/> * Search(Drum Intro, Guitar Solo, No Vocal) = Song1</p><h2>Google is our friend</h2><p>While approaching this problem I stumbled across a great post by teknoid <a
href="http://teknoid.wordpress.com/2008/08/06/habtm-and-join-trickery-with-cakephp/">http://teknoid.wordpress.com/2008/08/06/habtm-and-join-trickery-with-cakephp/</a> and although it was close to what I wanted, it had a very large limitation &#8212; Teknoid&#8217;s solution would only return a result set of songs if the tags are an exact match &#8212; no more, no less. I needed something more flexible. I needed a way to search for a set of songs that had at least the specific set of tags, but could possibly have more.</p><h2>The Solution</h2><p>So, with the help of the CakePHP&#8217;s custom find methods I hit was off to work!</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">//Song Model</span>
      <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$hasAndBelongsToMany</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$_findMethods</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'similar'</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>
&nbsp;
      <span style="color: #000000; font-weight: bold;">function</span> _findSimilar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'before'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joins'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
              <span style="color: #0000ff;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'songs_tags'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'SongsTag'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SongsTag.song_id = Song.id'</span><span style="color: #009900;">&#41;</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;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'tags'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Tag'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'Tag.id = SongsTag.tag_id'</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//make it an array.</span>
          <span style="color: #009900;">&#125;</span>
&nbsp;
          <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conditions'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Tag.name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">//set limit</span>
          <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'limit'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'limit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'limit'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'after'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$retval</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$tags</span> <span style="color: #339933;">=</span> Set<span style="color: #339933;">::</span><span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Tag/name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</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: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_intersect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tags</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
              <span style="color: #000088;">$retval</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span>
          <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$retval</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$results</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div><h2>Breakdown &#8230; prepare query</h2><p>We&#8217;re actually doing a few things here. There are two states to a custom find &#8212; before and after. Lets break it down to bite size pieces.</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000000; font-weight: bold;">function</span> _findSimilar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'before'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></pre></div></div><p>In the before state we prepare the incoming query. We only want to manipulate the query if we have a &#8216;tags&#8217; key in our options array, otherwise just pass it onto find(&#8216;all&#8217;).</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joins'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'songs_tags'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'SongsTag'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SongsTag.song_id = Song.id'</span><span style="color: #009900;">&#41;</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;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'tags'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Tag'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'Tag.id = SongsTag.tag_id'</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div><p>We have to manually set our joins so we can use our friendly CakePHP Tag.name condition syntax. We also make sure we&#8217;re dealing with an array of tags (this allows us to pass in a single string if wanted).</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'conditions'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Tag.name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">//set limit</span>
      <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'limit'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'limit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'limit'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span></pre></div></div><p>We set the conditions with an IN operator, set the limit if we don&#8217;t have one (5 in this case) and return the modified query. This query is rather greedy, as such, its best to limit it to maintain performance.</p><p>The end result in the before state is we&#8217;re running a search on songs that have ANY of the tags we&#8217;ve included in our tags array. Later we&#8217;ll take a look at the results and make sure we only return results that include all or more of the tags we specified.</p><h2>Breakdown &#8230; filter results</h2><p>The second part of the custom find is the after state.</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'after'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></pre></div></div><p>So here again we make sure we&#8217;re preforming a tag search, by checking if the tags key is set.</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">          <span style="color: #000088;">$retval</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$tags</span> <span style="color: #339933;">=</span> Set<span style="color: #339933;">::</span><span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Tag/name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</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: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_intersect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tags</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
              <span style="color: #000088;">$retval</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$retval</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div><p>It gets a little tricky here, we go through the result set that the query returned to us and extract all the names using CakePHP&#8217;s built in Set class. $tags ends up being a one dimensional array just like our $options['tags']. We then count the $options['tags'] and make sure its equal to the array_intersect of $tags, and $options['tags']. Array_intersect is great here because it will take two arrays, compare then and give us back what is matching. The result is only songs that have at least the specified search tags (possibly more) will be given as a valid result in the returned set.</p><h2>Example Usage</h2><p>Example usage of this custom similar find would be:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Song</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'similar'</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;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Drum Intro'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'No Vocal'</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: #666666; font-style: italic;">//Song1 and Song4</span>
&nbsp;
      <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Song</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'similar'</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;">=&gt;</span> <span style="color: #0000ff;">'Guitar Solo'</span>
      <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Song1 and Song3</span>
&nbsp;
      <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Song</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'similar'</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;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Drum Intro'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Guitar Solo'</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: #666666; font-style: italic;">//Empty</span></pre></div></div><h2>Extra credit</h2><p>A tip to paginate these results is to overwrite _findAll and _findCount, checking for &#8216;tags&#8217;, and doing the same thing. By doing so, all you&#8217;ll have to do is pass in a &#8216;tags&#8217; option key on an &#8216;all&#8217; or a &#8216;count&#8217; to get the behavior you want. This end result is all you need for pagination to work.</p><p>I hope you&#8217;ve enjoyed this tutorial.</p><p>Artículo original <a
href="http://www.webtechnick.com/blogs/view/237/HABTM_Search_By_Multiple_Tags_in_CakePHP">WebTechNick</a></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2010/12/habtm-search-by-multiple-tags-in-cakephp/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models</title><link>http://bilson.cl/2010/11/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/</link> <comments>http://bilson.cl/2010/11/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/#comments</comments> <pubDate>Tue, 30 Nov 2010 17:44:56 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[CakePHP]]></category> <category><![CDATA[habtm]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://bilson.cl/?p=79</guid> <description><![CDATA[One of CakePHP 1.2 coolest features are known as HABTM with models, which are particularly useful when you have a hasAndBelongsToMany binding between two models that may contain extra information (i.e: table fields) attached to the binding. They are also extremely useful when you need to make some model operations to the join table and [...]]]></description> <content:encoded><![CDATA[<p>One of CakePHP 1.2 coolest features are known as HABTM with models, which are particularly useful when you have a hasAndBelongsToMany binding between two models that may contain extra information (i.e: table fields) attached to the binding. They are also extremely useful when you need to make some model operations to the join table and feel too lazy to modelize it yourself, or when you have modelized the join table and need to tell CakePHP to use your own model as the join model.</p><p>Let’s start with a practical example, the classic Article -> hasAndBelongsToMany -> Tag binding. Set up the articles table in any way you like, and so the tags table. Now create the join table as follows:</p><pre lang=sql>
CREATE TABLE `articles_tags`(
	`id` INT NOT NULL AUTO_INCREMENT,
	`article_id` INT NOT NULL,
	`tag_id` INT NOT NULL,
	PRIMARY KEY(`id`)
);
</pre><p>Yeah, I know, so much for extra fields: just the id. However feel free to add any extra fields yourself and the example here will still be valid. In fact, if there are more than two fields in the join table then CakePHP will assume that you are normally interested on those extra fields.</p><p>Assume you have set up some articles, some tags, and have joined articles with some tags. Let’s do a classic findAll on the Article model:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$records</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$records</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>Bearing in mind the difference between the data you&#8217;ve set up, and the one we have on this example, the result may be something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>Article<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                    <span style="color: #009900;">&#91;</span>title<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> My First Article
                    <span style="color: #009900;">&#91;</span>body<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Hi there<span style="color: #339933;">!</span>
                    <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color: #208080;">03</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">:</span><span style="color: #208080;">01</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">16</span>
                    <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color: #208080;">03</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">31</span><span style="color: #339933;">:</span><span style="color: #208080;">06</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#91;</span>Tag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                        <span style="color: #009900;">&#40;</span>
                            <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                            <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> tag1
                            <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                            <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                            <span style="color: #009900;">&#91;</span>ArticlesTag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                                <span style="color: #009900;">&#40;</span>
                                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                                    <span style="color: #009900;">&#91;</span>article_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                                    <span style="color: #009900;">&#91;</span>tag_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                                <span style="color: #009900;">&#41;</span>
&nbsp;
                        <span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                        <span style="color: #009900;">&#40;</span>
                            <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                            <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> tag3
                            <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                            <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                            <span style="color: #009900;">&#91;</span>ArticlesTag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                                <span style="color: #009900;">&#40;</span>
                                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span>
                                    <span style="color: #009900;">&#91;</span>article_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                                    <span style="color: #009900;">&#91;</span>tag_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                                <span style="color: #009900;">&#41;</span>
                        <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>Article<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
                    <span style="color: #009900;">&#91;</span>title<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> My Second Article
                    <span style="color: #009900;">&#91;</span>body<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Hi there<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> article<span style="color: #339933;">!</span>
                    <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color: #208080;">03</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">:</span><span style="color: #208080;">06</span><span style="color: #339933;">:</span><span style="color: #208080;">02</span>
                    <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color: #208080;">03</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">:</span><span style="color: #208080;">06</span><span style="color: #339933;">:</span><span style="color: #208080;">02</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#91;</span>Tag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                        <span style="color: #009900;">&#40;</span>
                            <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span>
                            <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> tag2
                            <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                            <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                            <span style="color: #009900;">&#91;</span>ArticlesTag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                                <span style="color: #009900;">&#40;</span>
                                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                                    <span style="color: #009900;">&#91;</span>article_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
                                    <span style="color: #009900;">&#91;</span>tag_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span>
                                <span style="color: #009900;">&#41;</span>
                        <span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                        <span style="color: #009900;">&#40;</span>
                            <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                            <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> tag3
                            <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                            <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                            <span style="color: #009900;">&#91;</span>ArticlesTag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                                <span style="color: #009900;">&#40;</span>
                                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
                                    <span style="color: #009900;">&#91;</span>article_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
                                    <span style="color: #009900;">&#91;</span>tag_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                                <span style="color: #009900;">&#41;</span>
&nbsp;
                        <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></div></div><p>Ok now you may be asking: what is up with that ArticlesTag array that is showing up there? Well, you may have also guessed it: that&#8217;s the information coming from the HABTM join table. Let&#8217;s take a closer look at the SQL statements generated by CakePHP that yielded these results:</p><div
class="wp_syntax"><div
class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`Article`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Article`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`title`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Article`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`body`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Article`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`created`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Article`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`modified`</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`articles`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`Article`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`Tag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Tag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Tag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`created`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`Tag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`modified`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`ArticlesTag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`ArticlesTag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`article_id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`ArticlesTag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`tag_id`</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`tags`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`Tag`</span> <span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #ff0000;">`articles_tags`</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">`ArticlesTag`</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`ArticlesTag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`article_id`</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #ff0000;">`ArticlesTag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`tag_id`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">`Tag`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span></pre></div></div><p>The first query does the normal Article search, this is where your findAll conditions would be inserted (so don’t think yet that you can attach conditions based on your join model). The next query finds all the tags linked through the join table to the set of Article IDs obtained during the first query. Yes, CakePHP 1.1 users: this query has also been optimized (CakePHP 1.1 runs one finder query per resulting row).</p><p>What if you want to only get those articles that are linked to a specific tag? You need then to query the join table. This is where auto-with tables can also help you out, since the join table has been auto-modelized for you (hence the auto part of the name). Try this:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$joinRecords</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ArticlesTag</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ArticlesTag.tag_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$joinRecords</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>We now get:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>ArticlesTag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span>
                    <span style="color: #009900;">&#91;</span>article_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                    <span style="color: #009900;">&#91;</span>tag_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>ArticlesTag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
                    <span style="color: #009900;">&#91;</span>article_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
                    <span style="color: #009900;">&#91;</span>tag_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></div></div><p>So you can either play around and get the Article IDs using Set extract:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> Set<span style="color: #339933;">::</span><span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$joinRecords</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'{n}.ArticlesTag.article_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>After which $ids look like:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span></pre></div></div><p>Or use bindModel to join the Article and Tag models to the join table, and then running the query:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ArticlesTag</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindModel</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'belongsTo'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Article'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tag'</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;">$joinRecords</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ArticlesTag</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ArticlesTag.tag_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$joinRecords</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>The records now look like:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>ArticlesTag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span>
                    <span style="color: #009900;">&#91;</span>article_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                    <span style="color: #009900;">&#91;</span>tag_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#91;</span>Article<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
                    <span style="color: #009900;">&#91;</span>title<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> My First Article
                    <span style="color: #009900;">&#91;</span>body<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Hi there<span style="color: #339933;">!</span>
                    <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color: #208080;">03</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">:</span><span style="color: #208080;">01</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">16</span>
                    <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color: #208080;">03</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">31</span><span style="color: #339933;">:</span><span style="color: #208080;">06</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#91;</span>Tag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> tag3
                    <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                    <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>ArticlesTag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
                    <span style="color: #009900;">&#91;</span>article_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
                    <span style="color: #009900;">&#91;</span>tag_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#91;</span>Article<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
                    <span style="color: #009900;">&#91;</span>title<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> My Second Article
                    <span style="color: #009900;">&#91;</span>body<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Hi there<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> article<span style="color: #339933;">!</span>
                    <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color: #208080;">03</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">:</span><span style="color: #208080;">06</span><span style="color: #339933;">:</span><span style="color: #208080;">02</span>
                    <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color: #208080;">03</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">:</span><span style="color: #208080;">06</span><span style="color: #339933;">:</span><span style="color: #208080;">02</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#91;</span>Tag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
                    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> tag3
                    <span style="color: #009900;">&#91;</span>created<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                    <span style="color: #009900;">&#91;</span>modified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2007</span><span style="color: #339933;">-</span><span style="color:#800080;">09</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">19</span> <span style="color: #208080;">03</span><span style="color: #339933;">:</span><span style="color: #208080;">04</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">33</span>
                <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></div></div><p>And if you need more information associated with each Article you can of course play with the $recursive level of the findAll query.</p><p>What if for some reason you have already modelized your join table? Let’s say you created a model called ArticlesTag (located on app/models/articles_tag.php) that looks like this:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ArticlesTag <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ArticlesTag'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$belongsTo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Article'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>How can we now tell CakePHP that the join table should be modelized using our model? Let’s go back to the Article model definition and make a small change where we define the with model:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Article <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Article'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$hasAndBelongsToMany</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'with'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ArticlesTag'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Remember how we had to bind the Article and Tag model to the auto-with model set up with CakePHP? Looking at our own version of the join model that shouldn’t be necessary anymore, so let’s try:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$joinRecords</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ArticlesTag</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ArticlesTag.tag_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$joinRecords</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>You can now see that the yielded results are the same as those obtained when the auto-with model was previously binded.</p><p>Anyway, that’s a short introduction to CakePHP 1.2 with models. As usual with CakePHP, what seems to be a small addition gives us a new set of exciting possibilities.</p><p>Artículo original de <a
href="http://marianoiglesias.com.ar/cakephp/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/">Mariano Iglesias</a></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2010/11/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>sudo: sorry, you must have a tty to run sudo</title><link>http://bilson.cl/2010/10/sudo-sorry-you-must-have-a-tty-to-run-sudo/</link> <comments>http://bilson.cl/2010/10/sudo-sorry-you-must-have-a-tty-to-run-sudo/#comments</comments> <pubDate>Sat, 09 Oct 2010 05:05:51 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[Sin categoría]]></category><guid
isPermaLink="false">http://bilson.cl/?p=73</guid> <description><![CDATA[Tengo un script corriendo vía cron que requiere un comando privilegiado para funcionar. Para permitir el comando, utilizo sudo, agregando el comando y usuario en el archivo /etc/sudoers. Cuando el script se ejecuta, devuelve un error: sudo: sorry, you must have a tty to run sudo Solución: Comentar la línea Default requiretty en /etc/sudoers #Default [...]]]></description> <content:encoded><![CDATA[<p>Tengo un script corriendo vía cron que requiere un comando privilegiado para funcionar. Para permitir el comando, utilizo sudo, agregando el comando y usuario en el archivo /etc/sudoers.</p><p>Cuando el script se ejecuta, devuelve un error:</p><blockquote><p> sudo: sorry, you must have a tty to run sudo</p></blockquote><p><strong>Solución:</strong> Comentar la línea <strong>Default requiretty</strong> en /etc/sudoers</p><blockquote><p> #Default requiretty</p></blockquote><p> <img
src='http://bilson.cl/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /></p><p></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2010/10/sudo-sorry-you-must-have-a-tty-to-run-sudo/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Contar IP y cantidad de accesos en logs de apache</title><link>http://bilson.cl/2010/08/contar-ip-y-cantidad-de-accesos-en-logs-de-apache/</link> <comments>http://bilson.cl/2010/08/contar-ip-y-cantidad-de-accesos-en-logs-de-apache/#comments</comments> <pubDate>Tue, 17 Aug 2010 20:24:46 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[apache]]></category> <category><![CDATA[Linux]]></category><guid
isPermaLink="false">http://bilson.cl/?p=68</guid> <description><![CDATA[No recuerdo de dónde lo saqué pero acá va: awk '{!a[$1]++}END{for(i in a) if ( a[i] &#62;10 ) print a[i],i }' ./access-log Se demoró una fracción de segundo en procesar un log de 500.000 líneas!]]></description> <content:encoded><![CDATA[<p>No recuerdo de dónde lo saqué pero acá va:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{!a[$1]++}END{for(i in a) if ( a[i] &gt;10 ) print a[i],i }'</span> .<span style="color: #000000; font-weight: bold;">/</span>access-log</pre></div></div><p>Se demoró una fracción de segundo en procesar un log de 500.000 líneas!<br
/></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2010/08/contar-ip-y-cantidad-de-accesos-en-logs-de-apache/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Instalar Gnome y Xorg en Centos con yum</title><link>http://bilson.cl/2010/08/instalar-gnome-y-xorg-en-centos-con-yum/</link> <comments>http://bilson.cl/2010/08/instalar-gnome-y-xorg-en-centos-con-yum/#comments</comments> <pubDate>Fri, 13 Aug 2010 00:51:58 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[centos]]></category> <category><![CDATA[Linux]]></category><guid
isPermaLink="false">http://bilson.cl/?p=62</guid> <description><![CDATA[Si instalaste un centos sin entorno gráfico y ahora lo necesitas, la solución es: yum groupinstall &#34;X Window System&#34; &#34;GNOME Desktop Environment&#34; Yum es sensible a mayúsculas y minúsculas!]]></description> <content:encoded><![CDATA[<p> Si instalaste un centos sin entorno gráfico y ahora lo necesitas, la solución es:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">yum groupinstall <span style="color: #ff0000;">&quot;X Window System&quot;</span> <span style="color: #ff0000;">&quot;GNOME Desktop Environment&quot;</span></pre></div></div><p> Yum es sensible a mayúsculas y minúsculas!</p><p></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2010/08/instalar-gnome-y-xorg-en-centos-con-yum/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>No hay lugar como localhost</title><link>http://bilson.cl/2010/07/no-hay-lugar-como-localhost/</link> <comments>http://bilson.cl/2010/07/no-hay-lugar-como-localhost/#comments</comments> <pubDate>Sat, 10 Jul 2010 13:00:47 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[ocio]]></category><guid
isPermaLink="false">http://bilson.cl/blog/?p=42</guid> <description><![CDATA[]]></description> <content:encoded><![CDATA[<p
style="text-align: center;"><a
href="http://bilson.cl/blog/wp-content/uploads/2010/07/localhost.jpg" rel="lightbox[42]" title="No hay lugar como localhost"><img
class="size-medium wp-image-41 aligncenter" title="No hay lugar como localhost" src="http://bilson.cl/blog/wp-content/uploads/2010/07/localhost-300x225.jpg" alt="" width="300" height="225" /></a></p><p></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2010/07/no-hay-lugar-como-localhost/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Túnel SSH</title><link>http://bilson.cl/2010/07/tunel-ssh/</link> <comments>http://bilson.cl/2010/07/tunel-ssh/#comments</comments> <pubDate>Fri, 09 Jul 2010 23:17:49 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[ssh]]></category> <category><![CDATA[Ubuntu]]></category><guid
isPermaLink="false">http://bilson.cl/blog/?p=35</guid> <description><![CDATA[(Reviviendo post añejos) La situación es la siguiente: Estoy en mi casa, necesito conectarme a la máquina 1.2.3.4, por distintos protocolos (FTP, HTTP, SSH, SFTP), pero la máquina 1.2.3.4 tiene acceso restringido por IP, y sólo la IP 1.2.3.5 tiene acceso. Pero yo puedo entrar por ssh a 1.2.3.5. Solución: Hacer un túnel SSH. Cómo [...]]]></description> <content:encoded><![CDATA[<p>(Reviviendo post añejos)</p><p>La situación es la siguiente:</p><p>Estoy en mi casa, necesito conectarme a la máquina 1.2.3.4, por distintos protocolos (FTP, HTTP, SSH, SFTP), pero la máquina 1.2.3.4 tiene acceso restringido por IP, y sólo la IP 1.2.3.5 tiene acceso. Pero yo puedo entrar por ssh a 1.2.3.5.</p><p>Solución: Hacer un túnel SSH.</p><p>Cómo ?</p><p>Fácil:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-L</span> <span style="color: #000000;">10022</span>:1.2.3.4:<span style="color: #000000;">22</span> <span style="color: #660033;">-f</span> <span style="color: #660033;">-N</span> usuario<span style="color: #000000; font-weight: bold;">@</span>1.2.3.5</pre></div></div><p>De esta forma, yo hago ssh a localhost en el puerto 10022 y la conexión es redireccionada a 1.2.3.4, y ya estoy por ssh en la máquina que tenía restringido el acceso <img
src='http://bilson.cl/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /></p><p> Repetir el proceso para redireccionar más puertos.</p><p>Ejemplos:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-L</span> <span style="color: #000000;">1025</span>:1.2.3.4:<span style="color: #000000;">25</span> <span style="color: #660033;">-f</span> <span style="color: #660033;">-N</span> usuario<span style="color: #000000; font-weight: bold;">@</span>1.2.3.5
<span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-L</span> <span style="color: #000000;">1080</span>:1.2.3.4:<span style="color: #000000;">80</span> <span style="color: #660033;">-f</span> <span style="color: #660033;">-N</span> usuario<span style="color: #000000; font-weight: bold;">@</span>1.2.3.5
<span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-L</span> <span style="color: #000000;">10021</span>:1.2.3.4:<span style="color: #000000;">21</span> <span style="color: #660033;">-f</span> <span style="color: #660033;">-N</span> usuario<span style="color: #000000; font-weight: bold;">@</span>1.2.3.5
<span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-L</span> <span style="color: #000000;">10110</span>:1.2.3.4:<span style="color: #000000;">110</span> <span style="color: #660033;">-f</span> <span style="color: #660033;">-N</span> usuario<span style="color: #000000; font-weight: bold;">@</span>1.2.3.5</pre></div></div><p>Para puertos menores a 1025 se debe tener privilegios de root.</p><p></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2010/07/tunel-ssh/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>mod_rewrite Cheat Sheet</title><link>http://bilson.cl/2009/12/mod_rewrite-cheat-sheet/</link> <comments>http://bilson.cl/2009/12/mod_rewrite-cheat-sheet/#comments</comments> <pubDate>Mon, 28 Dec 2009 19:53:32 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[apache]]></category><guid
isPermaLink="false">http://bilson.cl/blog/?p=18</guid> <description><![CDATA[Navegando por ahí&#8230; encontré este buen resumen de lo que podemos hacer con mod_rewrite&#8230; para tener siempre a mano: http://www.cheat-sheets.org/saved-copy/mod_rewrite_cheat_sheet.pdf]]></description> <content:encoded><![CDATA[<p>Navegando por ahí&#8230; encontré este buen resumen de lo que podemos hacer con mod_rewrite&#8230; para tener siempre a mano:</p><p><a
href="http://www.cheat-sheets.org/saved-copy/mod_rewrite_cheat_sheet.pdf">http://www.cheat-sheets.org/saved-copy/mod_rewrite_cheat_sheet.pdf</a></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2009/12/mod_rewrite-cheat-sheet/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Configurar Sony Ericsson MD300 de EntelPCS en Ubuntu Intrepid Ibex (Conexión 3g)</title><link>http://bilson.cl/2009/02/configurar-sony-ericsson-md300-de-entelpcs-en-ubuntu-intrepid-ibex-conexion-3g/</link> <comments>http://bilson.cl/2009/02/configurar-sony-ericsson-md300-de-entelpcs-en-ubuntu-intrepid-ibex-conexion-3g/#comments</comments> <pubDate>Sun, 01 Feb 2009 20:22:33 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[Sin categoría]]></category><guid
isPermaLink="false">http://bilson.cl/blog/?p=10</guid> <description><![CDATA[La información que aquí publico está tomada básicamente de tres partes (ubuntu-cl.org, laudecioliveira.org y ubuntu-es.org). Ha sido probado con EntelPCS en Chile, y funciona sin problemas Lo primero es ayudar a udev a identificar el módem como tal, para eso necesitamos el id del dispositivo y de vendedor. Esta información la obtenemos así: sudo lsusb [...]]]></description> <content:encoded><![CDATA[<p> La información que aquí publico está tomada básicamente de tres partes (<a
href="http://foros.ubuntu-cl.org/viewtopic.php?t=4990&#038;postdays=0&#038;postorder=asc&#038;start=0">ubuntu-cl.org</a>, <a
href="http://laudecioliveira.org/blog/?p=70">laudecioliveira.org</a> y <a
href="http://www.ubuntu-es.org/index.php?q=node/94744">ubuntu-es.org</a>). Ha sido probado con EntelPCS en Chile, y funciona sin problemas <img
src='http://bilson.cl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p> Lo primero es ayudar a udev a identificar el módem como tal, para eso necesitamos el id del dispositivo y de vendedor. Esta información la obtenemos así:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> lsusb <span style="color: #660033;">-v</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-i</span> vendor
&nbsp;
  idVendor           0x0fce Sony Ericsson Mobile Communications AB</pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> lsusb <span style="color: #660033;">-v</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-i</span> product
&nbsp;
  idProduct          0xd0cf 
  iProduct                <span style="color: #000000;">2</span> Sony Ericsson MD300</pre></div></div><p> Ahora creamos el archivo <em>/etc/udev/rules.d/50-md300modem.rules</em> con nuestro editor favorito:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">ACTION<span style="color: #000000; font-weight: bold;">!</span>=<span style="color: #ff0000;">&quot;add&quot;</span>, <span style="color: #007800;">GOTO</span>=<span style="color: #ff0000;">&quot;3G_End&quot;</span>
<span style="color: #007800;">BUS</span>==<span style="color: #ff0000;">&quot;usb&quot;</span>, SYSFS<span style="color: #7a0874; font-weight: bold;">&#123;</span>idProduct<span style="color: #7a0874; font-weight: bold;">&#125;</span>==<span style="color: #ff0000;">&quot;d0cf&quot;</span>, SYSFS<span style="color: #7a0874; font-weight: bold;">&#123;</span>idVendor<span style="color: #7a0874; font-weight: bold;">&#125;</span>==<span style="color: #ff0000;">&quot;0fce&quot;</span>, <span style="color: #007800;">PROGRAM</span>=<span style="color: #ff0000;">&quot;/bin/sh -c 'echo 3 &gt; /sys/%p/device/bConfigurationValue'&quot;</span>
<span style="color: #007800;">LABEL</span>=<span style="color: #ff0000;">&quot;3G_END&quot;</span></pre></div></div><p>Ahora desconectamos el módem y descargamos los módulos del kernel, lo volvemos a cargar con los parámetros que necesitamos</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> modprobe <span style="color: #660033;">-r</span> usbserial
<span style="color: #c20cb9; font-weight: bold;">sudo</span> rmmod usb_storage
<span style="color: #c20cb9; font-weight: bold;">sudo</span> modprobe usbserial <span style="color: #007800;">vendor</span>=0x0fce <span style="color: #007800;">product</span>=0xd0cf</pre></div></div><p>Creamos el archivo <strong>/etc/modprobe.d/md300</strong> con nuestro editor de texto favorito. Este archivo tendrá un alias para que el módulo se cargue correctamente luego de reiniciar el pc.</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#Sony Ericsson MD300</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> md300 usbserial
options md300 <span style="color: #007800;">vendor</span>=0x0fce <span style="color: #007800;">product</span>=0xd0cf</pre></div></div><p> Reiniciamos udev:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>udev restart</pre></div></div><p> Ahora debemos configurar wvdial, editamos el archivo <em>/etc/wvdial.conf</em></p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>Dialer Defaults<span style="color: #7a0874; font-weight: bold;">&#93;</span>
Modem = <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>ttyACM0
ISDN = off
Modem Type = USB Modem
Baud = <span style="color: #000000;">460800</span>
Init = ATZ
Init2 = AT+<span style="color: #007800;">CFUN</span>=<span style="color: #000000;">1</span>
Init3 = ATQ0 V1 E1 <span style="color: #007800;">S0</span>=<span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&amp;</span>C1 <span style="color: #000000; font-weight: bold;">&amp;</span>D2 +<span style="color: #007800;">FCLASS</span>=<span style="color: #000000;">0</span>
Init4 = 
Init5 = ATZ+<span style="color: #007800;">cgdcont</span>=<span style="color: #000000;">1</span>,<span style="color: #ff0000;">&quot;IP&quot;</span>,<span style="color: #ff0000;">&quot;imovil.entelpcs.cl&quot;</span>
Init6 = 
Init7 = 
Init8 = 
Init9 = 
Phone = <span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">99</span><span style="color: #666666; font-style: italic;">#</span>
Phone1 = 
Phone2 = 
Phone3 = 
Phone4 = 
Dial Prefix = 
Dial Attempts = <span style="color: #000000;">1</span>
Dial Command = ATM1L3DT
Ask Password = off
Password = entelpcs
Username = entelpcs
Auto Reconnect = off
Abort on Busy = off
Carrier Check = off
Check Def Route = on
Abort on No Dialtone = on
Stupid Mode = on
Idle Seconds = <span style="color: #000000;">0</span>
Auto DNS = on</pre></div></div><p> La línea <strong>Init2 = AT+CFUN=1</strong> es para activar la función radio del módem. Sin esta línea, pppd se cierra antes de conectar.</p><p> Enchufamos nuevamente el módem, y ejecutamos wvdial para conectarnos a internet:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> wvdial</pre></div></div><p> Obtendremos como resultado algo como esto:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">--<span style="color: #000000; font-weight: bold;">&gt;</span> WvDial: Internet dialer version <span style="color: #000000;">1.60</span>
--<span style="color: #000000; font-weight: bold;">&gt;</span> Cannot get information <span style="color: #000000; font-weight: bold;">for</span> serial port.
--<span style="color: #000000; font-weight: bold;">&gt;</span> Initializing modem.
--<span style="color: #000000; font-weight: bold;">&gt;</span> Sending: ATZ
ATZ
OK
--<span style="color: #000000; font-weight: bold;">&gt;</span> Sending: AT+<span style="color: #007800;">CFUN</span>=<span style="color: #000000;">1</span>
AT+<span style="color: #007800;">CFUN</span>=<span style="color: #000000;">1</span>
OK
--<span style="color: #000000; font-weight: bold;">&gt;</span> Sending: ATQ0 V1 E1 <span style="color: #007800;">S0</span>=<span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&amp;</span>C1 <span style="color: #000000; font-weight: bold;">&amp;</span>D2 +<span style="color: #007800;">FCLASS</span>=<span style="color: #000000;">0</span>
ATQ0 V1 E1 <span style="color: #007800;">S0</span>=<span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&amp;</span>C1 <span style="color: #000000; font-weight: bold;">&amp;</span>D2 +<span style="color: #007800;">FCLASS</span>=<span style="color: #000000;">0</span>
OK
--<span style="color: #000000; font-weight: bold;">&gt;</span> Sending: ATZ+<span style="color: #007800;">cgdcont</span>=<span style="color: #000000;">1</span>,<span style="color: #ff0000;">&quot;IP&quot;</span>,<span style="color: #ff0000;">&quot;imovil.entelpcs.cl&quot;</span>
ATZ+<span style="color: #007800;">cgdcont</span>=<span style="color: #000000;">1</span>,<span style="color: #ff0000;">&quot;IP&quot;</span>,<span style="color: #ff0000;">&quot;imovil.entelpcs.cl&quot;</span>
OK
--<span style="color: #000000; font-weight: bold;">&gt;</span> Modem initialized.
--<span style="color: #000000; font-weight: bold;">&gt;</span> Sending: ATM1L3DT<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">99</span><span style="color: #666666; font-style: italic;">#</span>
--<span style="color: #000000; font-weight: bold;">&gt;</span> Waiting <span style="color: #000000; font-weight: bold;">for</span> carrier.
ATM1L3DT<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">99</span><span style="color: #666666; font-style: italic;">#</span>
~<span style="color: #7a0874; font-weight: bold;">&#91;</span>7f<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #666666; font-style: italic;">#@!}!}!} }9}#}%B#}%}(}&quot;}'}&quot;}&quot;}&amp;} } } } }%}&amp;=;+zZC~</span>
CONNECT
--<span style="color: #000000; font-weight: bold;">&gt;</span> Carrier detected.  Starting PPP immediately.
--<span style="color: #000000; font-weight: bold;">&gt;</span> Starting pppd at Sun Feb  <span style="color: #000000;">1</span> <span style="color: #000000;">20</span>:<span style="color: #000000;">21</span>:<span style="color: #000000;">26</span> <span style="color: #000000;">2009</span>
--<span style="color: #000000; font-weight: bold;">&gt;</span> Pid of pppd: <span style="color: #000000;">8630</span>
--<span style="color: #000000; font-weight: bold;">&gt;</span> Using interface ppp0
--<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #7a0874; font-weight: bold;">local</span>  IP address 186.9.56.27
--<span style="color: #000000; font-weight: bold;">&gt;</span> remote IP address 10.64.64.64
--<span style="color: #000000; font-weight: bold;">&gt;</span> primary   DNS address 164.77.252.249
--<span style="color: #000000; font-weight: bold;">&gt;</span> secondary DNS address 164.77.252.215</pre></div></div><p> <strong><em>Actualización</em></strong>: Si tienen distribuciones basadas en RPM, pueden bajar <a
href="https://forge.betavine.net/projects/d-semd300-entcl/">este &#8220;driver&#8221;</a> de betavine (desarrollado por EntelPCS), que básicamente es lo mismo que hice yo a mano, y con algunas pequeñas modificaciones básicamente en el archivo wvdial.conf. Desde ubuntu pueden pasarlo a .deb con alien.</p><p> Si prefieren, también pueden usar gnome-ppp para discar.  Eso es todo&#8230; disfruten <img
src='http://bilson.cl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2009/02/configurar-sony-ericsson-md300-de-entelpcs-en-ubuntu-intrepid-ibex-conexion-3g/feed/</wfw:commentRss> <slash:comments>17</slash:comments> </item> <item><title>Get VOIP</title><link>http://bilson.cl/2008/10/get-voip/</link> <comments>http://bilson.cl/2008/10/get-voip/#comments</comments> <pubDate>Thu, 09 Oct 2008 08:28:35 +0000</pubDate> <dc:creator>bilson</dc:creator> <category><![CDATA[Sin categoría]]></category><guid
isPermaLink="false">http://bilson.cl/blog/?p=9</guid> <description><![CDATA[Get VOIP Originally uploaded by mringlein Notable !! No será mucho digo yo ?]]></description> <content:encoded><![CDATA[<div
style="float: right; margin-left: 10px; margin-bottom: 10px;"> <a
href="http://www.flickr.com/photos/mringlein/2924987019/" title="photo sharing"><img
src="http://farm4.static.flickr.com/3166/2924987019_64e2f6a711_m.jpg" alt="" style="border: solid 2px #000000;" /></a><br
/> <br
/> <span
style="font-size: 0.9em; margin-top: 0px;"><br
/> <a
href="http://www.flickr.com/photos/mringlein/2924987019/">Get VOIP</a><br
/> <br
/> Originally uploaded by <a
href="http://www.flickr.com/people/mringlein/">mringlein</a><br
/> </span></div><p>Notable !!</p><p>No será mucho digo yo ?<br
/> <br
clear="all" /></p> ]]></content:encoded> <wfw:commentRss>http://bilson.cl/2008/10/get-voip/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic (User agent is rejected)
Database Caching 9/14 queries in 0.031 seconds using disk: basic

Served from: bilson.cl @ 2012-02-06 17:03:14 -->
