<?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>PHP Log &#187; HABTM</title>
	<atom:link href="http://www.php-log.de/tags/habtm/feed" rel="self" type="application/rss+xml" />
	<link>http://www.php-log.de</link>
	<description>Eine feine Sammlung nützlicher PHP Notizen</description>
	<lastBuildDate>Thu, 27 May 2010 04:43:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>CakePHP &#8211; Pagination bei einer HABTM Beziehung</title>
		<link>http://www.php-log.de/383/php/cakephp-pagination-bei-einer-habtm-beziehung</link>
		<comments>http://www.php-log.de/383/php/cakephp-pagination-bei-einer-habtm-beziehung#comments</comments>
		<pubDate>Tue, 21 Jul 2009 02:04:50 +0000</pubDate>
		<dc:creator>ralle</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[HABTM]]></category>
		<category><![CDATA[Pagination]]></category>

		<guid isPermaLink="false">http://www.php-log.de/?p=383</guid>
		<description><![CDATA[Das Handbuch ist nicht allzu Gesprächig was dieses Thema angeht. Glücklicherweise gibt es eine funktionierende, aber nicht gut dokumentierte Lösung. Zu finden ist sie beim Cakebaker. Es gibt einen Key für die Pagination, für den ich keine Doku finden konnte. Falls jemand in der offiziellen Doku etwas finden sollte, freue ich mich über jeden Hinweis [...]]]></description>
			<content:encoded><![CDATA[<p>Das Handbuch ist nicht allzu Gesprächig was dieses Thema angeht. Glücklicherweise gibt es eine funktionierende, aber nicht gut dokumentierte Lösung.<span id="more-383"></span></p>
<p>Zu finden ist sie beim <a target="_blank" href="http://cakebaker.42dh.com/2007/10/17/pagination-of-data-from-a-habtm-relationship/">Cakebaker</a>. Es gibt einen Key für die Pagination, für den ich keine Doku finden konnte. Falls jemand in der offiziellen Doku etwas finden sollte, freue ich mich über jeden Hinweis und Kommentar.<br />
Der Key lautet <code>joins</code> und erwartet ein Array das die Beziehung definiert.</p>
<p>In meinem Fall ist es so, dass Produkte und User in einer HABTM Beziehung stehen. Die Auflösungstabelle ist products_users und hat die übliche HABTM Struktur.  Hier der Code, der für mich in der Index Funktion meines Controllers funktioniert.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginate</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'Product'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'joins'</span> <span style="color: #339933;">=&gt;</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;">'products_users'</span><span style="color: #339933;">,</span> 
		            <span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ProductUser'</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;">'ProductUser.product_id = Product.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;">'users'</span><span style="color: #339933;">,</span> 
		            <span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</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;">'User.id = ProductUser.user_id'</span><span style="color: #339933;">,</span> 
		                <span style="color: #0000ff;">'User.id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</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;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'products'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Product'</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>Das ganze ist natürlich einiges an Code, es gibt auch eine <a href="http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find">Lösung</a> die das meiste in den app_controller packt, die ich nicht benutze, aber der Vollständigkeit erwähnen wollte. Sollte jemand eine bessere Lösung kennen bitte ich um Erleuchtung <img src='http://www.php-log.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-log.de/383/php/cakephp-pagination-bei-einer-habtm-beziehung/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
