<?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>juliaholland.org &#187; CakePHP</title>
	<atom:link href="http://www.juliaholland.org/category/cakephp/feed" rel="self" type="application/rss+xml" />
	<link>http://www.juliaholland.org</link>
	<description></description>
	<lastBuildDate>Fri, 16 Jul 2010 05:39:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>CakePHP Multi-record forms</title>
		<link>http://www.juliaholland.org/2009/01/23/cakephp-multi-record-forms</link>
		<comments>http://www.juliaholland.org/2009/01/23/cakephp-multi-record-forms#comments</comments>
		<pubDate>Fri, 23 Jan 2009 23:06:06 +0000</pubDate>
		<dc:creator>Julia</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.juliaholland.org/?p=74</guid>
		<description><![CDATA[While developing a registration form for a client I had the following problem: This form needed to be able to grab multi-record values for one table. In this case I had a form where a person could sign up more than one participant. (Using CakePHP standard form names) echo $form-&#62;input&#40;'Participant.0.first_name'&#41;; echo $form-&#62;input&#40;'Participant.0.last_name'&#41;; echo $form-&#62;input&#40;'Participant.1.first_name'&#41;; echo [...]]]></description>
			<content:encoded><![CDATA[<p>While developing a registration form for a client I had the following problem:</p>
<p>This form needed to be able to grab multi-record values for one table. In this case I had a form where a person could sign up more than one participant. (Using CakePHP standard form names)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Participant.0.first_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Participant.0.last_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Participant.1.first_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Participant.1.last_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>CakePHP has a really great saveAll feature which allows you to validate and save to multiple models (tables). In this case though I don&#8217;t want to save the data immediately to the database. I am using the Wizard component which saves all of the form data to an array to be used later.</p>
<p><span id="more-74"></span></p>
<p>Simply using the validates() function doesn&#8217;t work in this case because of the multiple records. In the docs it says there is an open for saveAll &#8216;validate&#8217;=&gt;&#8217;only&#8217; but I couldn&#8217;t get that to work either.</p>
<p>After Googling I found this post: <a href="http://lemoncake.wordpress.com/2007/08/06/multi-record-forms/" target="_blank">http://lemoncake.wordpress.com/2007/08/06/multi-record-forms/</a> and was finally able to make some progress.</p>
<p>For the view code example above I used the following for my controller:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$invalidBookFields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Participant'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$participant</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000088;">$participant</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Participant'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$participant</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Participant</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$participant</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Participant</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validates</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// save the validationErrors and reset for the next iteration</span>
    <span style="color: #000088;">$invalidBookFields</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Participant</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">invalidFields</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$invalidPartFields</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> 
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// put all the errors back in the model so they make it back to the view</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Participant</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$invalidPartFields</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;">'invalidPartFields'</span><span style="color: #339933;">,</span><span style="color: #000088;">$invalidPartFields</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And now it validates according to the $validate array in your model. Yay!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.juliaholland.org/2009/01/23/cakephp-multi-record-forms/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CakePHP and TCPDF</title>
		<link>http://www.juliaholland.org/2009/01/23/cakephp-and-tcpdf</link>
		<comments>http://www.juliaholland.org/2009/01/23/cakephp-and-tcpdf#comments</comments>
		<pubDate>Fri, 23 Jan 2009 23:00:53 +0000</pubDate>
		<dc:creator>Julia</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.juliaholland.org/?p=75</guid>
		<description><![CDATA[I needed to be able to generate PDFs for a client project and came upon this tutorial for using CakePHP and TCPDF. It turned out I needed to make some tweaks and after gathering info from different Google sources finally got it to work. Thought I’d save someone else the trouble and post my version. [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to be able to generate PDFs for a client project and came upon <a href="http://bakery.cakephp.org/articles/view/creating-pdf-files-with-cakephp-and-tcpdf">this tutorial</a> for using CakePHP and TCPDF. It turned out I needed to make some tweaks and after gathering info from different Google sources finally got it to work. Thought I’d save someone else the trouble and post my version. </p>
<h3 class="sub-header">Step 1:</h3>
<p><a href="http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf">Download the latest version of TCPDF.</a></p>
<h3 class="sub-header">Step 2:</h3>
<p>Upload TCPDF files to app/vendor</p>
<h3 class="sub-header">Step 3:</h3>
<p>Create your layout app/views/layouts/pdf.ctp</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$content_for_layout</span><span style="color: #339933;">;</span></pre></div></div>

<h3 class="sub-header">Step 4:</h3>
<p>Controller code (very basic)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> view_pdf<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$id</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Sorry, there was no PDF selected.'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</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>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'pdf'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//this will use the pdf.ctp layout</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3 class="sub-header">Step 5:</h3>
<p>View code (page view_pdf.ctp)</p>
<p>See example code on the TCPDF site for details but here is a basic shell to get you going (I used writeHTML):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Vendor'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'tcpdf/tcpdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tcpdf</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TCPDF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$textfont</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'helvetica'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetAuthor</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Julia Holland&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetAutoPageBreak</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPrintHeader</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPrintFooter</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetTextColor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetFont</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$textfont</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create some HTML content</span>
<span style="color: #000088;">$htmlcontent</span> <span style="color: #339933;">=</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
Add HTML content here to print
EOF</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// output the HTML content</span>
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">writeHTML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$htmlcontent</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tcpdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Output</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'filename.pdf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'D'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That’s it! <img src='http://www.juliaholland.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.juliaholland.org/2009/01/23/cakephp-and-tcpdf/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
