<?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>TYPO3Development &#187; Extbase</title>
	<atom:link href="http://blog.typoplanet.de/tag/extbase/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.typoplanet.de</link>
	<description>A blog for professional TYPO3 developers</description>
	<lastBuildDate>Mon, 28 Mar 2011 18:10:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Effectively use the Repository and Query Object of Extbase?</title>
		<link>http://blog.typoplanet.de/2010/01/27/the-repository-and-query-object-of-extbase/</link>
		<comments>http://blog.typoplanet.de/2010/01/27/the-repository-and-query-object-of-extbase/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 17:13:32 +0000</pubDate>
		<dc:creator>Jochen Rau</dc:creator>
				<category><![CDATA[Extbase]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[extension development]]></category>
		<category><![CDATA[persistence]]></category>
		<category><![CDATA[query object]]></category>
		<category><![CDATA[repository]]></category>

		<guid isPermaLink="false">http://blog.typoplanet.de/?p=327</guid>
		<description><![CDATA[Extbase supports very well the idea of Domain-Driven Design. It supplies a Repository class to comfortably fetch your objects from. Some handy methods are already implemented. Let&#8217;s have a look at some example code. 12345678910111213141516171819202122$blog = new Tx_BlogExample_Domain_Model_Blog&#40;'TYPO3 Development'&#41;; $blog-&#62;setDescription&#40;'A blog for professional TYPO3 developers.'&#41;; $administrator = new Tx_BlogExample_Domain_Model_Administrator&#40;&#41;; $administrator-&#62;setName&#40;'Jochen Rau'&#41;; $administrator-&#62;setEmail&#40;'jochen.rau@example.com'&#41;; $blog-&#62;setAdministrator&#40;$administrator&#41;; $post = [...]]]></description>
			<content:encoded><![CDATA[<p>Extbase supports very well the idea of Domain-Driven Design. It supplies a <code class="codecolorer php dawn"><span class="php">Repository</span></code> class to comfortably fetch your objects from. Some handy methods are already implemented. <span id="more-327"></span> Let&#8217;s have a look at some example code.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$blog</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tx_BlogExample_Domain_Model_Blog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'TYPO3 Development'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$blog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A blog for professional TYPO3 developers.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$administrator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tx_BlogExample_Domain_Model_Administrator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$administrator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Jochen Rau'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$administrator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setEmail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jochen.rau@example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$blog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAdministrator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$administrator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tx_BlogExample_Domain_Model_Post<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Effectively use the Query Object of Extbase'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContent</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Extbase is a framework to develop ... hey, we\'re getting recursive now!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$comment</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tx_BlogExample_Domain_Model_Comment<span style="color: #339933;">;</span><br />
<span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDate</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAuthor</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Peter Pan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setEmail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'peter.pan@example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContent</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Do you need some pills?.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addComment</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$blog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addPost</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$blogRepository</span> <span style="color: #339933;">=</span> t3lib_div<span style="color: #339933;">::</span><span style="color: #004000;">makeInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tx_BlogExample_Domain_Repository_BlogRepository'</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000088;">$blogRepository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$blog</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>In a first step, we create a new blog with an administrator, and a post with a comment. We instanciate the <code class="codecolorer php dawn"><span class="php">BlogRepository</span></code> in line 21 and add the blog to it. That&#8217;s all we have to do. The blog is now persisted and can be fetched later from the repository again. We don&#8217;t have to say <code class="codecolorer php dawn"><span class="php">save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$blog</span><span style="color: #009900;">&#41;</span></span></code> at any time. The repository &#8220;takes care&#8221; of the blog.</p>
<p>Keep in mind that you have to instanciate the repository with <code class="codecolorer php dawn"><span class="php">t3lib_div<span style="color: #339933;">::</span><span style="color: #004000;">makeInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> as it is a Singleton. If you instanciate it with <code class="codecolorer php dawn"><span class="php"><span style="color: #000000; font-weight: bold;">new</span></span></code> the Repository will be always an empty one if Extbase searches for objects to be persisted.</p>
<p>The <code class="codecolorer php dawn"><span class="php">BlogRepository</span></code> class is as simple as this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> Tx_BlogExample_Domain_Repository_BlogRepository <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">extends</span> Tx_Extbase_Persistence_Repository <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Most of the methods needed are already implemented in the <code class="codecolorer php dawn"><span class="php">Tx_Extbase_Persistence_Repository</span></code>. These are:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$existingObject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newObject</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$modifiedObject</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000088;">$repository</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><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">countAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Since Extbase 1.1</span><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">countByProperty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Since Extbase 1.1</span><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByProperty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000088;">$repository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOneByProperty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span></div></div>
<p>The methods <code class="codecolorer php dawn"><span class="php">add<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> and <code class="codecolorer php dawn"><span class="php">remove<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> are self-explanatory. <code class="codecolorer php dawn"><span class="php">replace<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> takes an untracked object and puts it at the &#8220;place&#8221; of an existing one where <code class="codecolorer php dawn"><span class="php">update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> just takes an existing object and updates it with the property values of the modified object.</p>
<p>The <code class="codecolorer php dawn"><span class="php">findByProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> and <code class="codecolorer php dawn"><span class="php">findOneByProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> methods are magic methods where you can replace &#8220;Property&#8221; with the name of a property of the <code class="codecolorer php dawn"><span class="php">Blog</span></code> object. For example we can write <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$blogRepository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'My Blog'</span><span style="color: #009900;">&#41;</span></span></code> to fetch all Blogs with the given title. This also works for objects like in <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$blogRepository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByAdministrator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$administrator</span><span style="color: #009900;">&#41;</span></span></code> but not yet for properties holding aggregates. Thus, <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$blogRepository</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByPost</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span></span></code> won&#8217;t work by now. <code class="codecolorer php dawn"><span class="php">findOneByProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> is pretty similar to <code class="codecolorer php dawn"><span class="php">findByProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> but returns the only first Object found.</p>
<p>The <code class="codecolorer php dawn"><span class="php">countAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> and <code class="codecolorer php dawn"><span class="php">countByProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> methods are similar to the according <code class="codecolorer php dawn"><span class="php">find</span></code> methods. But the don&#8217;t build objects. They only count resulting objects.</p>
<p>All these methods keep the underlying storage solution (mostly a relational database) transparent to the user. But what if we have special constraints that are not covered with the methods above? Let&#8217;s say we want to find the five most recent posts of a given blog. Then we can use a <code class="codecolorer php dawn"><span class="php">Query</span></code> object.  Let&#8217;s ask the <code class="codecolorer php dawn"><span class="php">PostRepository</span></code> for those posts:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> Tx_BlogExample_Domain_Repository_PostRepository<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">extends</span> Tx_Extbase_Persistence_Repository <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> findRecentByBlog<span style="color: #009900;">&#40;</span>Tx_BlogExample_Domain_Model_Blog <span style="color: #000088;">$blog</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">matching</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blog'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$blog</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOrderings</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date'</span> <span style="color: #339933;">=&gt;</span> Tx_Extbase_Persistence_QueryInterface<span style="color: #339933;">::</span><span style="color: #004000;">ORDER_DESCENDING</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLimit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>integer<span style="color: #009900;">&#41;</span><span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$posts</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The method <code class="codecolorer php dawn"><span class="php">createQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> returns an appropriate <code class="codecolorer php dawn"><span class="php">Query</span></code> object suitable for <code class="codecolorer php dawn"><span class="php">Post</span></code> objects. In Line 6 we define the constraint the posts should match: <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">matching</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>constraint<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></span></code>. The constraint is returned by <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blog'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$blog</span><span style="color: #009900;">&#41;</span></span></code>. There are some more methods returning a constraint:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logicalAnd</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$constraint1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$constraint2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logicalOr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$constraint1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$constraint2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logicalNot</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$constraint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">withUid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operand</span><span style="color: #339933;">,</span> <span style="color: #000088;">$caseSensitive</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">in</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Since Extbase 1.1</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Since Extbase 1.1</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">like</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lessThan</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lessThanOrEqual</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">greaterThan</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">greaterThanOrEqual</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>We can define the orderings with <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOrderings</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>orderings<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></span></code> where <code class="codecolorer php dawn"><span class="php"><span style="color: #009900;">&#91;</span>orderings<span style="color: #009900;">&#93;</span></span></code> is expected to be an array of property-order pairs. There are two constants defining the order:</p>
<ul>
<li>Order ascending: <code class="codecolorer php dawn"><span class="php">Tx_Extbase_Persistence_QueryInterface<span style="color: #339933;">::</span><span style="color: #004000;">ORDER_ASCENDING</span></span></code></li>
<li>Order descending: <code class="codecolorer php dawn"><span class="php">Tx_Extbase_Persistence_QueryInterface<span style="color: #339933;">::</span><span style="color: #004000;">ORDER_DESCENDING</span></span></code></li>
</ul>
<p>We limit the result set to a maximum of five posts with <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLimit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>integer<span style="color: #009900;">&#41;</span><span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span></span></code>. And finally invoke <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> which returns us the already build posts with all their comments inside (if we are not &#8220;lazy&#8221; ;-) ). There is also a method <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> which does  not fetch the resulting posts but counts them. Note that the <code class="codecolorer php dawn"><span class="php">Query</span></code> object enables chaining. Thus, the following code does the same as the given above:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> findRecentByBlog<span style="color: #009900;">&#40;</span>Tx_BlogExample_Domain_Model_Blog <span style="color: #000088;">$blog</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">matching</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blog'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$blog</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOrderings</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date'</span> <span style="color: #339933;">=&gt;</span> Tx_Extbase_Persistence_QueryInterface<span style="color: #339933;">::</span><span style="color: #004000;">ORDER_DESCENDING</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLimit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>integer<span style="color: #009900;">&#41;</span><span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>That&#8217;s it for today. I hope you got a deeper insight and I am looking forward to your comments. Maybe you want to join the discussion on the related <a href="http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc">mailing list</a>. I have taken most of the code from the extension <code class="codecolorer php dawn"><span class="php">BlogExample</span></code> which is available in the <a href="http://typo3.org/extensions/repository/view/blog_example/current/">Extension Repository (TER)</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.typoplanet.de/2010/01/27/the-repository-and-query-object-of-extbase/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>A walk through the persistence layer of Extbase</title>
		<link>http://blog.typoplanet.de/2009/07/16/a-walk-trough-the-persistence-layer-of-extbase/</link>
		<comments>http://blog.typoplanet.de/2009/07/16/a-walk-trough-the-persistence-layer-of-extbase/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 09:34:16 +0000</pubDate>
		<dc:creator>Jochen Rau</dc:creator>
				<category><![CDATA[Extbase]]></category>
		<category><![CDATA[extension development]]></category>
		<category><![CDATA[flow3]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://blog.typoplanet.de/?p=275</guid>
		<description><![CDATA[Extbase is the new frame-work for TYPO3 extension developers introduced in TYPO3 v4.3. In this article I will give you a brief overview over the persistence layer of Extbase. It falls into different (sub)layers — each of the with different responsibilities. Let&#8217;s start our journey from the top level. 1. Repository A Repository is the first [...]]]></description>
			<content:encoded><![CDATA[<p>Extbase is the new frame-work for <a title="TYPO3 project page" href="http://www.typo3.org">TYPO3</a> extension developers introduced in TYPO3 v4.3. In this article I will give you a brief overview over the persistence layer of Extbase. It falls into different (sub)layers — each of the with different responsibilities. Let&#8217;s start our journey from the top level.<span id="more-275"></span></p>
<p><strong>1. Repository</strong></p>
<p>A Repository is the first objects a developer comes in contact to. It handles only domain objects and provide task oriented interface to retrieve them by saying findByFoo(&#8216;Bar&#8217;). Most of the time you are done.</p>
<p><strong>2. Query</strong></p>
<p>The next level is the Query object. It provides methods to create an abstract query (abstract in sense of abstracted from where the objects come from). It has no clue about the underlying structure of the storage. It even does not now if it is based on a relational model or a tree model (like the content repository of [FLOW3|FLOW3 is a next generation web application framework built for the upcoming TYPO3 v5]). It also invokes the DataMapper (see below). An extension developer uses the Query object to create his own findByFoo() methods for  his Repositories.</p>
<p><strong>3. QueryObjectModel</strong></p>
<p>Deeper and deeper we dig &#8230; In comparison to the Query object Query Object Model knows about the underlying concepts of relations and thus Joins and Orderings. An extension developer may use this class to specify more complex and &#8220;relational model&#8221; aware queries. That makes the creation of queries (esp. Joins) al little bit laborious. But it helps to keep abstraction.</p>
<p><strong>4. StorageBackend</strong></p>
<p>The Storage Backend translates the Query Object Model in syntactical correct statement (SQL). It also invokes the call to the database via a handler. An extension developer normally does not have to mak his hands dirty with SQL (which is in most cases the best way to improve performance and security).</p>
<p><strong>5. DatabaseHandler</strong></p>
<p>The Database Handler prepares queries, fetches the result set. It can be simply the [t3lib_db|A class in TYPO3 providing methods to access a database], or [PDO|PHP Data Objects], or your preferred solution.</p>
<p><strong>6. DataMapper</strong></p>
<p>And finally the Data Mapper. It maps the resulting rows on the Domain Object (it creates an empty object and fills it with the converted property values). It also resolves relations according to the loading strategy (eg. Lazy Loading Proxy).</p>
<p>Puh! Now we are done. Hope you enjoyed the journey.</p>
<p><strong>F.A.Q</strong></p>
<p><strong>Q</strong> Are you nuts! Six different layers, dozens of objects, and bunch of new things to learn! I only wanted to say &#8220;SELECT * FROM Tx_MyExt_Foo WHERE &#8230;&#8221;!<br />
<strong>A</strong> You are right: I am nuts (at least a litte bit ;-)). But now we are fully compatible to the (Query) API of FLOW3 and reached the best level to port Extensions forward. And I really like the Idea of having totally freedom of choice.</p>
<p><strong>Q</strong> And what about performance?<br />
<strong>A</strong> That’s indeed a very important point. We will do some extensive profiling sessions, implement Lazy Loading strategies, and strive for optimized Queries.</p>
<p>And please keep in mind: 90% of the work is based on the concepts and code of <a title="FLOW3 project page" href="http://flow3.typo3.org/">FLOW3</a>. Credits goes to the FLOW3 team.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.typoplanet.de/2009/07/16/a-walk-trough-the-persistence-layer-of-extbase/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

