<?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>Juozas devBlog &#187; example</title>
	<atom:link href="http://dev.juokaz.com/tag/example/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend Framework and Doctrine. Part 2</title>
		<link>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2</link>
		<comments>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2#comments</comments>
		<pubDate>Wed, 18 Nov 2009 10:05:32 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[application.ini]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=866</guid>
		<description><![CDATA[Today we start actual development with Doctrine and Zend Framework. Base of this post is my code which I have been using for quite a few projects and it worked really well.
These are the steps required to setup Doctrine:

Create MySQL (or any other adapter supported by Doctrine) database
Download Doctrine 1.2 (as of today &#8211; 1.2.0beta3). [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/11/doctrine-orm-php5.png" alt="doctrine-orm-php5" title="doctrine-orm-php5" width="191" height="53" class="size-full wp-image-847" style="float: left; margin-right: 5px;" />Today we start actual development with Doctrine and Zend Framework. Base of this post is my code which I have been using for quite a few projects and it worked really well.</p>
<p>These are the steps required to setup Doctrine:</p>
<ol>
<li>Create MySQL (or any other adapter <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/introduction-to-connections">supported</a> by Doctrine) database</li>
<li>Download Doctrine 1.2 (as of today &#8211; <a href="http://www.doctrine-project.org/download/1_2_0_BETA3/format/tgz/package/">1.2.0beta3</a>). Believe, it&#8217;s stable enough (no problems at all for me) and supports functions all functions we are going to use later</li>
<li>Setup <em>application.ini</em> and <a href="http://framework.zend.com/manual/en/zend.application.quick-start.html#zend.application.quick-start.resources">application resource</a></li>
<li>Generate models from database</li>
<li>Profit!</li>
</ol>
<p>To start with, <strong>download</strong> this <a href="http://dev.juokaz.com/examples/doctrine/doctrine_v1.zip">archive</a> (or get updated version from my public <a href="http://github.com/juokaz/php-examples/tree/master/doctrine-application-resource/">repository</a>). It has everything you will need. Some of the files are quite long so I&#8217;m not going to post them here, it&#8217;s better that you download them and have them ready to be used as the article progresses. </p>
<h5>Create MySQL database</h5>
<p>For MySQL databases I use a product called &#8220;<a href="http://dev.mysql.com/downloads/workbench/5.1.html">MySQL Workbench</a>&#8220;. If you haven&#8217;t tried it I definitely recommend to give it a go &#8211; it basically allows you create a database as a <a href="http://forge.mysql.com/w/images/a/a8/Mysql_workbench_tbl_editor.png">diagram</a> and then export it to sql file or update actual database (can be risky). Since I expect you to have enough knowledge how to create a database I won&#8217;t write anything more &#8211; you can find <a href="http://www.webpronews.com/topnews/2005/06/06/mysql-for-beginners-how-to-create-a-mysql-database">tutorials</a> all over the web.</p>
<h5>Download Doctrine</h5>
<p>After downloading Doctrine extract it to <em>library</em> folder. You should have <em>./library/Doctrine.php</em> in your library folder. Although, you can use <em>svn:externals</em> (if you are using SVN at all) to remove Doctrine code from repository, but in this case you probably need to setup paths in application resource file explain below to use Doctrine_Core if you just get Doctrine folder contents (from <a href="http://svn.doctrine-project.org/tags/1.2.0-BETA3/lib/Doctrine/">here</a>).</p>
<p>Most important point here &#8211; don&#8217;t forget to <a href="http://www.doctrine-project.org/download">download</a> <strong>1.2</strong>, not version 1.1. Even though it doesn&#8217;t have a stable release, 1.1 doesn&#8217;t support models generation as we want them to be (auto-loadable). Also this week all bugs have been fixed which I reported and now models generation works perfectly. From my personal experience, I haven&#8217;t even used 1.1 at all (started with 1.2 when it was in alpha) and didn&#8217;t had any problems, so my recommendation &#8211; use 1.2. </p>
<h5>Setup application.ini and application resource</h5>
<p>From the file listed above open a file called <em>application.ini</em>. Append you current configuration (more on getting Zend Framework project running can be found <a href="http://framework.zend.com/docs/quickstart/create-your-project">here</a>) with settings in that file (leave compiled and cache options to false for now). Doctrine uses DNS strings to connect to a database, there are quite a few <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/introduction-to-connections#dsn,-the-data-source-name:examples">examples</a> in the documentation. If you have used <a href="http://php.net/manual/en/book.pdo.php">PDO</a> before you be very familiar.</p>
<p>In archive there is a file called <em>library/resource.php</em>. Depending on what namespace you use for outside-Zend code, paste it to chosen folder. If you choose to have &#8220;App&#8221; as a namespace, just copy that file to <em>library/App/Application/Resource/Doctrine.php</em>. And don&#8217;t forget to have:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">autoloaderNamespaces<span style="">&#91;</span><span style="">&#93;</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;App_&quot;</span>
pluginpaths.App_Application_Resource <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;App/Application/Resource&quot;</span></pre></div></div>

<p>in <em>application.ini</em> also, otherwise library code won&#8217;t be auto-loaded. I don&#8217;t recommend putting any code in Zend folder, because it will create tons of problems in the future. Having App or ProjectName library folders allows to have your own code in separate packages which you can use in later projects (I usually have App, ProjectName, CompanyName). </p>
<p>Provided <em>application.ini</em> file isn&#8217;t that much configurable, mainly because it wasn&#8217;t supposed to be released at all. If you are looking for something more dynamic, look no further than this <a href="http://framework.zend.com/wiki/display/ZFPROP/Zend_Application_Resource_Doctrine+-+Matthew+Lurz">proposal</a> in Zend incubator. Nevertheless, my code should work fine &#8211; it has all options required to get you started and have been tweaked with settings which I found to be really useful. </p>
<h5>Generate models from database</h5>
<p>This part is the most awesome. To start, just copy and paste everything from <em>scripts</em> folder in the zip archive to your scripts folder in application (I have scripts folder in the same level as application and library, in the root of project). There actually only two files &#8211; <em>doctrine-cli.php</em> and custom task in <em>Doctrine/Task/GenerateModels.php</em>. You can run doctrine-cli.php like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php doctrine-cli.php</pre></div></div>

<p>and you should get a list of <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/utilities#command-line-interface">all the possible tasks</a> (if you setup everything properly). To run my custom task, append &#8220;generate-models&#8221; to the end of previous command to get:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php doctrine-cli.php generate-models</pre></div></div>

<p>This task will load your database schema and create all classes (table, base model, model) required for models. Doctrine also supports generating models from <em><a href="http://en.wikipedia.org/wiki/YAML">yaml</a></em> configuration files, though I&#8217;ve never used it before, but configuration should be almost the same.</p>
<h5>It works</h5>
<p>To test the models you can look at the generate code or start coding you application (or even run <em><a href="http://www.doctrine-project.org/documentation/manual/1_2/en/dql-doctrine-query-language">dql</a></em> task from <em>doctrine-cli.php</em>). For example you can test simply like this (adjust by models you have):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$product</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Model_Product<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Product name'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Model_Product'</span><span style="color: #009900;">&#41;</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;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If everything has been configured properly you shouldn&#8217;t get any errors and this code (put it in controller) should output array of your saved data. Because of the settings in application resource, save() method also runs <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/data-validation">validation</a> so if you missed a field and it breaks a <em>not null</em> constrain, this code will throw a validator exception. </p>
<h5>What&#8217;s next?</h5>
<p>This article has enough information to get you started and almost all the code provided it left as simple as possible and open for further modifications. I hope that in coming months code for Doctrine and Zend Framework integration will be completed and you can use Doctrine without any outside code.</p>
<p>Since we have application running now we can dive into actual usage, testing and more advanced stuff. I have code for these also and will be posting more articles very soon, even though the best resource for Doctrine is <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/introduction">documentation</a>. If you have any problems with this code just leave a comment here or find my on twitter (<a href="http://www.twitter.com/juokaz">here</a>) and I will try to explain more.</p>
<p><strong>All parts</strong>:</p>
<ol>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-1">Zend Framework and Doctrine. Part 1</a></li>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2">Zend Framework and Doctrine. Part 2</a></li>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-3">Zend Framework and Doctrine. Part 3</a></li>
</ol>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Using AJAX with onSubmit event in jQuery</title>
		<link>http://dev.juokaz.com/javascript/using-ajax-with-onsubmit-event-in-jquery</link>
		<comments>http://dev.juokaz.com/javascript/using-ajax-with-onsubmit-event-in-jquery#comments</comments>
		<pubDate>Tue, 10 Mar 2009 17:09:12 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[submit]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=333</guid>
		<description><![CDATA[Last week I was working on one task, which required me to check data with AJAX when form is being submitted. Users clicks the button, request is made, data arrives, some hidden fields are modified, actual submit happens. The problem I ran into is, how to control form submission and ensure that it&#8217;s submitted only [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I was working on one task, which required me to check data with AJAX when form is being submitted. Users clicks the button, request is made, data arrives, some hidden fields are modified, actual submit happens. The problem I ran into is, how to control form submission and ensure that it&#8217;s submitted only when AJAX has finished it&#8217;s job.</p>
<p>What I had, was a form, where user enters username using auto-completion. Problem was, setting <em>user_id</em> hidden field after user selection (from search results) was sometimes faulty. If users enters &#8220;Joh&#8221;, selects &#8220;John&#8221;, <em>user_id</em> is set to 2. But if user changes value to &#8220;John J&#8221;, selection even is not executed and <em>user_id</em> remains 2. This is a problem, because this is not the users meant and on submit I need to check if given username is correct, and in this case set <em>user_id</em> to <em>null</em>.</p>
<p>It&#8217;s quite easy to forget what A in AJAX stands for. A means asynchronous, which means that when HTTP calls are made JavaScript doesn&#8217;t wait for it to return response. This property made AJAX so useful, but in some cases it&#8217;s not what developers want. For example in jQuery, all AJAX functions has possibility to supply it with anonymous function &#8211; callback.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#Form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>
		url<span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// callback with data</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Again, this is what most of the times developers need, but I was working on different problem. Problem is, callback function here will <strong>never be executed</strong>. JavaScript will make a call, go to &#8220;return true;&#8221; statement, and browser is sent to other page. In my case, I needed form to be submitted only when response data has arrived and some manipulations have been done.</p>
<p>It may seem quite an easy problem, but it turned not to be. The problem is, you need to modify this even function, such that it can be called again when callback function have done it&#8217;s work (or modify it, to unbind submit event function and when submit again). First time, function creates HTTP request and returns false (to stop browser from actually submitting it).</p>
<p>After data has completed loading, callback function calls submit event again. However, now function knows that it&#8217;s second call (hence, callback has been called) and returns true &#8211; browser submits the form. What I came up with, is this code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#Form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">formLoading</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>
			url<span style="color: #339933;">,</span>
			<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			<span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// callback with data</span>
				$.<span style="color: #660066;">formLoading</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#Form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It works perfectly. You can test it <a href="http://dev.juokaz.com/examples/jquery/submit/call.html">here</a>: first form works correctly, second one never finishes loading (have a look at code).</p>
<p>I believe, that AJAX on submit is not the best way to do such things, but required functionality was for administration panel, so there was no risk of users cheating. It was possible to do the same work in server-side when handling submitted data, but for better user experience, I wanted to make data check without refreshes.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/javascript/using-ajax-with-onsubmit-event-in-jquery/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
