<?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>Digital Base - Blog &#187; database</title>
	<atom:link href="http://www.digitalbase.eu/blog/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.digitalbase.eu/blog</link>
	<description>A blog about webdesign, PHP, development and IT</description>
	<lastBuildDate>Mon, 10 May 2010 16:16:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Xajax driven CriteriaBuilder for Propel / php</title>
		<link>http://www.digitalbase.eu/blog/xajax-driven-criteriabuilder-for-propel-php/</link>
		<comments>http://www.digitalbase.eu/blog/xajax-driven-criteriabuilder-for-propel-php/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 11:00:00 +0000</pubDate>
		<dc:creator>Bart Vanderstukken</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[datagrid]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[propel]]></category>

		<guid isPermaLink="false">http://dibav3.gnelisse.desktop01/blog/?p=58</guid>
		<description><![CDATA[The CriteriaBuilder is a GUI to create a <a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a> (<a href="http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3" target="_blank">Propel</a>), which then can be used by any php object implementing our CriteriaClient interface.
Here's how we use it in combination with our grid_propel.]]></description>
			<content:encoded><![CDATA[<p>Earlier this week, I talked about our custom datagrid for <a href="http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3" target="_blank">Propel</a> and<br />
how it was being rewritten to interact with our CriteriaBuilder (<a href="/blog/custom-propel-grid-update" target="_blank">read<br />
more</a>). Well, I&#8217;ll give you another glance into our coding<br />
efforts.</p>
<h4 class="green">Goal</h4>
<p>We wanted to make<br />
an object, which would manipulate the data being shown in our<br />
grid_propel. For normal people, this means: to filter the data.</p>
<p>Early on, we<br />
decided it had to &#8216;build a <a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a>&#8216; (because <a href="http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3" target="_blank">Propel</a> rules!), which<br />
then could be used by our grid_propel.</p>
<h4 class="green">Structure</h4>
<p>The<br />
CriteriaBuilder is in essence nothing but an array of FilterData<br />
objects, which is indexed by the FilterColumn&#8217;s name (<a href="http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3" target="_blank">Propel</a> peer<br />
constant), so there could only be one FilterData object per<br />
FilterColumn.<br />
Of course, there&#8217;s more to it, about a thousand<br />
lines of code actually (<a href="http://www.phpdoc.org/">phpDoc</a> included), but almost half of that are<br />
xajax functions for user interaction, and then there are methods like<br />
addAvailableColumn &amp; qAddAvailableColumn to initialize the<br />
CriteriaBuilder, and internal functions, which I&#8217;m not all going to<br />
explain here. I&#8217;ll touch some of them in the next section.</p>
<h4 class="green">Behind<br />
the scenes</h4>
<p>The __toString<br />
function, will loop all FilterData objects and create a selector,<br />
with their column name or with a more human readable alias, using our<br />
XHTML package, which I&#8217;ll perhaps tell you more about on a<br />
quite/rainy day. It&#8217;s an object-oriented package to quickly create<br />
readable and XHTML valid code, I can tell you that.</p>
<div style="text-align: center"><img title="CriteriaBuilder column selector" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/columns.png" border="0" alt="CriteriaBuilder column selector" width="450" height="295" /></div>
<p>The add filter<br />
button will trigger the xajax function addFilter, which adds a<br />
UserFilter to the FilterData object related to the selected/passed<br />
value and then updates the &#8216;criteriaform&#8217;.</p>
<div style="text-align: center"><img title="Filter Example" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/filter.png" border="0" alt="Criteriabuilder UserFilter example" width="445" height="113" /></div>
<p>This criteriaform<br />
shows for every UserFilter a &#8216;criteriaLine&#8217;, with a combination box<br />
(disabled if only one line present), a filter method selector and<br />
something to determine the search value.</p>
<p>Clicking &#8216;apply<br />
filters&#8217; will trigger another xajax function, which will build the<br />
<a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a> based on the current UserFilters, and pass it to all<br />
registered CriteriaClients through the CriteriaClient interface<br />
method processCriteria. This is an implementation of what Java<br />
programmers probably know as the Listener pattern, meaning that an<br />
object implementing the CriteriaClient interface registers itself as<br />
Listener/CriteriaClient, by calling<br />
CriteriaBuilder::addClient(CriteriaClient).<br />
<strong><br />
This means that the<br />
CriteriaBuilder can serve any (and more than one) object implementing<br />
the CriteriaClient interface, not just our grid_propel!</strong></p>
<p>The &#8216;remove this<br />
filter&#8217; button will trigger a xajax fuction, which will remove the<br />
UserFilter from the FilterColumn&#8217;s UserFilter array, reorder its<br />
keys, and update the criteriaform.<br />
The &#8216;clear all<br />
filters&#8217;-button, naturally, deletes all UserFilters and also updates<br />
the CriteriaForm. Because there are no filters left, the criteriaform<br />
will disappear again.<br />
Both functions will also immediately update<br />
the grid by calling buildCriteria (which calls processCriteria on its<br />
clients).</p>
<p>I&#8217;ve recently<br />
added a &#8217;save filter&#8217;-button, which makes this box appear,</p>
<div style="text-align: center"><img title="save filter combination" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/save_filter.png" border="0" alt="CriteriaBuilder save filter combination" width="443" height="144" /></div>
<p>giving the user<br />
the option to save his filter combination. Which is then linked to<br />
the CriteriaBuilder&#8217;s name and saved in our database. When a<br />
CriteriaBuilder is &#8216;__tostringed&#8217;, it will search for these filters,<br />
and if one&#8217;s found, you&#8217;ll get something like this.</p>
<div style="text-align: center"><img title="load filter combination" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/load_filter.png" border="0" alt="CriteriaBuilder load saved UserFilter" width="444" height="72" /></div>
<p>Selecting a saved<br />
filter will once again trigger a xajax function, which will load the<br />
filter (update criteriaform and build <a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a>, and thus updating the<br />
grid).<br />
For now, these are personal filters, because they&#8217;re linked<br />
to the user who&#8217;s created them. Later, we could easily extend the<br />
code, so they can be shared, or something like that&#8230;<br />
Note that<br />
by linking the saved filters to the CriteriaBuilder&#8217;s name, we&#8217;re<br />
able to use the same filters on different pages, by naming the<br />
CriteriaBuilders the same.</p>
<h4 class="green">Everything<br />
configurable!</h4>
<p>Now, you know<br />
what&#8217;s happening behind the scenes when a user uses our<br />
CriteriaBuilder, but that&#8217;s not even half the story. Still using our<br />
user grid, I&#8217;ll explain how the CriteriaBuilder&#8217;s FilterData objects<br />
are being initialized.</p>
<p>We<br />
use this code on our page (FYI: our pages are also created using an<br />
object-oriented framework)</p>
<p>[code]CB_pageContent[/code]</p>
<p style="margin-top: 0.2cm; margin-bottom: 0.2cm">So&#8230;<br />
We initialize our grid_propel, grid_users (<a href="/blog/custom-propel-grid-update">see this post</a>), and<br />
specify its starting <a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a>. You can also neglect this, then a new<br />
(empty) <a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a> will be used. Next, we use the CiteriaBuilder&#8217;s<br />
constructor to pass the grid as CriteriaClient, set its name, and set<br />
the original <a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a>, which is used to restore the grid to its<br />
original state.</p>
<p>Now,<br />
that I come to think of it, this should be stored in the grid_propel,<br />
not in the CriteriaBuilder, because the original <a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a> can be<br />
different per client!<br />
You see this is code in progress&#8230; We<br />
actually don&#8217;t use multiple CriteriaBuilders nor CriteriaClients<br />
ourselves, yet&#8230; <img src='http://www.digitalbase.eu/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
I simply added the method getOriginalCriteria<br />
to the interface class CriteriaClient. CriteriaBuilder::buildCriteria<br />
now retrieves its &#8216;<a href="http://propel.phpdb.org/trac/wiki/Development/Criteria" target="_blank">Criteria</a> to work with&#8217; via that method, thus from<br />
the grid_propel, which of course has an extra variable<br />
originalCriteria now. Grid_propel::setCriteria is now</p>
<p>[code]CB_setCriteria[/code]</p>
<p>Voila,<br />
solved!</p>
<p>Ok,<br />
to continue&#8230; $cb-&gt;qAddUserColumnSet<br />
is a convenience method.<br />
Admitted, these functions should better<br />
be in an extending class, but nobody&#8217;s perfect <img src='http://www.digitalbase.eu/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
And since we&#8217;re<br />
the only ones using it&#8230;</p>
<p>[code]CB_qAddUserColumnSet[/code]</p>
<p>As<br />
you can see, this method adds the columns we say the grid can be<br />
filtered on. These could also be columns which aren&#8217;t shown in the grid_propel.</p>
<p>[code]CB_qAddAvailableColumn[/code]</p>
<p style="margin-top: 0.2cm; margin-bottom: 0.2cm">So&#8230;<br />
In our case, we use the CriteriaBuilder&#8217;s constants, for example<br />
CriteriaBuilder::FILTERSET_TEXT<br />
stands for the array</p>
<p>[code]CB_ex_array[/code]</p>
<h4 class="green">UserFilter<br />
types</h4>
<p>This<br />
defines which &#8216;type&#8217; of UserFilter will be used. For each constant,<br />
there&#8217;s a descendant of UserFilter. They complete UserFilter by<br />
defining the abstract __toString function. So each descendant will be<br />
shown different.</p>
<h5 class="green">TYPE_TEXT</h5>
<p>simple inputfield</p>
<div style="text-align: center"><img title="UserFilter type text" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/type_text.png" border="0" alt="UserFilter type text" width="444" height="44" /></div>
<h5 class="green">TYPE_BOOLEAN</h5>
<p>simple select</p>
<div style="text-align: center"><img title="UserFilter type boolean" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/type_boolean.png" border="0" alt="UserFilter type boolean" width="446" height="113" /></div>
<h5 class="green">TYPE_DATE</h5>
<p>JavaScript calendar widget</p>
<div style="text-align: center"><img title="UserFilter type date" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/type_date.png" border="0" alt="UserFilter type date" /></div>
<h5 class="green">TYPE_SPECIFIC</h5>
<p>selector with values available in database for that column (=<br />
dynamic)</p>
<div style="text-align: center"><img title="UserFilter type specific" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/type_specific.png" border="0" alt="UserFilter type specific" /></div>
<h5 class="green">TYPE_SPECIFIC_SWITCH</h5>
<p>special specific, it has extra filtermethods &#8216;contains&#8217; &amp; &#8216;does<br />
not contain&#8217;. When this filter is selected the search value<br />
determinator, will change into an inputfield.</p>
<p>First</p>
<div style="text-align: center"><img title="UserFilter type specific_switch normal" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/type_specific_switch.png" border="0" alt="UserFilter type specific_switch normal" /></div>
<p>Then</p>
<div style="text-align: center"><img title="UserFilter type specific_switch for (does not) contains" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/type_specific_switch_after.png" border="0" alt="UserFilter type specific_switch for (does not) contains" /></div>
<h5 class="green">Of course, you can combine as many filters as you want.</h5>
<div style="text-align: center"><img title="use as many filters as you want" src="/uploads/assets//blog/Tutorials/CriteriaBuilder/combination.png" border="0" alt="combination of multiple UserFilters" width="445" height="255" /></div>
]]></content:encoded>
			<wfw:commentRss>http://www.digitalbase.eu/blog/xajax-driven-criteriabuilder-for-propel-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simplifying Queries: Using sum, count, etc in Propel</title>
		<link>http://www.digitalbase.eu/blog/simplifying-queries-using-sum-count-etc-in-propel/</link>
		<comments>http://www.digitalbase.eu/blog/simplifying-queries-using-sum-count-etc-in-propel/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 17:43:59 +0000</pubDate>
		<dc:creator>Bart Vanderstukken</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[propel]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://dibav3.gnelisse.desktop01/blog/?p=21</guid>
		<description><![CDATA[Using database functions like sum, count, etc in propel. A small explanation.]]></description>
			<content:encoded><![CDATA[<p>Simplifying Queries: Using sum, count, etc in Propel</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$c</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>addSelectColumn<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sum('</span><span style="color: #339933;">.</span>dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">TOTAL</span><span style="color: #339933;">.</span><span style="color: #0000ff;">') as total'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">doSelectRS</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rs</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>next<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: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rs</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getInt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, simply use addSelectColumn. You have to work with the ResultSet offcourse, since you won&#8217;t be able to populateObjects.</p>
<p>In this case you can simply use &#8216;1&#8242; in getInt(column index), since we&#8217;re sure only one column is being fetched. Note that the index starts at 1 and not at 0.</p>
<p>Now, imagine a case where you are not sure what the index is (it could also change in the project&#8217;s life cycle). Then you can use, dbTimeBlockPeer::translateFieldName(dbTimeBlockPeer::TOTAL, BasePeer::TYPE_COLNAME, BasePeer::TYPE_NUM)), which will return the position of column &#8216;total&#8217;.</p>
<p>This needs no explanation, but I&#8217;ll give it anyway: translateFieldName accepts as first argument a string (source), the source type name and the destination type name. You can go and look in the basePeer which type you need.</p>
<p>basedbTimeBlockPeer (generated by Propel)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #990000;">static</span> <span style="color: #000088;">$fieldNames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span> BasePeer<span style="color: #339933;">::</span><span style="color: #004000;">TYPE_PHPNAME</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UserId'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ProjectId'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Ticketnr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Changeset'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'TimeIn'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'TimeOut'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Total'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> BasePeer<span style="color: #339933;">::</span><span style="color: #004000;">TYPE_COLNAME</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">USER_ID</span><span style="color: #339933;">,</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">PROJECT_ID</span><span style="color: #339933;">,</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">TICKETNR</span><span style="color: #339933;">,</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">CHANGESET</span><span style="color: #339933;">,</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">DESCRIPTION</span><span style="color: #339933;">,</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">TIME_IN</span><span style="color: #339933;">,</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">TIME_OUT</span><span style="color: #339933;">,</span> dbTimeBlockPeer<span style="color: #339933;">::</span><span style="color: #004000;">TOTAL</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> BasePeer<span style="color: #339933;">::</span><span style="color: #004000;">TYPE_FIELDNAME</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'project_id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ticketnr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'changeset'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'time_in'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'time_out'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'total'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> BasePeer<span style="color: #339933;">::</span><span style="color: #004000;">TYPE_NUM</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.digitalbase.eu/blog/simplifying-queries-using-sum-count-etc-in-propel/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

