<?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>cakebaker &#187; database</title>
	<atom:link href="http://cakebaker.42dh.com/tags/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakebaker.42dh.com</link>
	<description>baking cakes with CakePHP</description>
	<lastBuildDate>Mon, 19 Jul 2010 14:23:38 +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>&#8220;updated&#8221; or &#8220;modified&#8221;</title>
		<link>http://cakebaker.42dh.com/2008/10/28/updated-or-modified/</link>
		<comments>http://cakebaker.42dh.com/2008/10/28/updated-or-modified/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 10:17:39 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[convention]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=910</guid>
		<description><![CDATA[There are three columns which are automagically filled by CakePHP if they exist: &#8220;created&#8221;, &#8220;updated&#8221;, and &#8220;modified&#8221;. It is probably obvious that &#8220;created&#8221; is filled at the time a record gets inserted into the database. And &#8220;updated&#8221; and &#8220;modified&#8221; when a record is changed. But what&#8217;s the difference between &#8220;updated&#8221; and &#8220;modified&#8221;? Well, there is [...]]]></description>
			<content:encoded><![CDATA[<p>There are three columns which are automagically filled by CakePHP if they exist: &#8220;created&#8221;, &#8220;updated&#8221;, and &#8220;modified&#8221;. It is probably obvious that &#8220;created&#8221; is filled at the time a record gets inserted into the database. And &#8220;updated&#8221; and &#8220;modified&#8221; when a record is changed. </p>
<p>But what&#8217;s the difference between &#8220;updated&#8221; and &#8220;modified&#8221;? Well, there is none. You can either use &#8220;updated&#8221; or &#8220;modified&#8221;, just don&#8217;t use both as that is redundant. And whichever column name you choose, use it throughout your database to be consistent!</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/10/28/updated-or-modified/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Migrations &#8211; the CakePHP way(?)</title>
		<link>http://cakebaker.42dh.com/2008/04/13/migrations-the-cakephp-way/</link>
		<comments>http://cakebaker.42dh.com/2008/04/13/migrations-the-cakephp-way/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 15:55:19 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=594</guid>
		<description><![CDATA[A quite popular feature in Rails are migrations, a kind of version control system for database schemes. Inspired by this feature, Joel Moss started a while ago the CakePHP migrations project to bring this feature to the CakePHP world. In the meantime there is also a migrations-like feature in the Cake core, even though it [...]]]></description>
			<content:encoded><![CDATA[<p>A quite popular feature in Rails are <a href="http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations">migrations</a>, a kind of version control system for database schemes. Inspired by this feature, <a href="http://joelmoss.info">Joel Moss</a> started a while ago the <a href="http://code.google.com/p/cakephp-migrations/">CakePHP migrations project</a> to bring this feature to the CakePHP world.</p>
<p>In the meantime there is also a migrations-like feature in the Cake core, even though it seems to be a bit half-baked (or maybe I&#8217;m too stupid to understand it *g*). </p>
<p>Anyway, let&#8217;s have a look at it by using the traditional blog example.</p>
<p>First, we create a file which will contain the definitions for our schema. We name the file blog.php and place it in app/config/sql. In this file we create a class BlogSchema:</p>
<pre>
class BlogSchema extends CakeSchema {
}
</pre>
<p>Each property of a schema class represents a table, i.e. the property name is the table name, and the property value contains the table definition as an array. The definition for our posts table looks like:</p>
<pre>
class BlogSchema extends CakeSchema {
    public $posts = array(
        'id' =&gt; array('type' =&gt; 'integer', 'null' =&gt; false, 'default' =&gt; NULL, 'key' =&gt; 'primary'),
        'title' =&gt; array('type' =&gt; 'string', 'null' =&gt; false, 'length' =&gt; 100),
        'content' =&gt; array('type' =&gt; 'text', 'null' =&gt; false),
        'indexes' =&gt; array('PRIMARY' =&gt; array('column' =&gt; 'id', 'unique' =&gt; 1))
    );
}
</pre>
<p>Now, we can switch to the command line and make a test run to verify the schema script will execute the correct SQL statements:</p>
<pre>
cd cake/console
./cake schema run create blog -dry

---------------------------------------------------------------
Cake Schema Shell
---------------------------------------------------------------
Performing a dry run.

The following tables will drop.
posts

Are you sure you want to drop the tables? (y/n)
[n] &gt; y
Dropping tables.
Dry run for posts :
DROP TABLE IF EXISTS `posts`;

The following tables will create.
posts

Are you sure you want to create the tables? (y/n)
[y] &gt; y
Creating tables.
Dry run for posts :
CREATE TABLE `posts` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `title` varchar(100) NOT NULL,
        `content` text NOT NULL,
        PRIMARY KEY  (`id`)
);

End create.
</pre>
<p>Everything looks fine, so we can run the same command again, this time without the -dry parameter, and it should create the posts table.</p>
<p>Ok, now let&#8217;s add a new column &#8220;created&#8221; to our schema:</p>
<pre>
class BlogSchema extends CakeSchema {
    public $posts = array(
        'id' =&gt; array('type' =&gt; 'integer', 'null' =&gt; false, 'default' =&gt; NULL, 'key' =&gt; 'primary'),
        'title' =&gt; array('type' =&gt; 'string', 'null' =&gt; false, 'length' =&gt; 100),
        'content' =&gt; array('type' =&gt; 'text', 'null' =&gt; false),
        'created' =&gt; array('type' =&gt; 'datetime'),
        'indexes' =&gt; array('PRIMARY' =&gt; array('column' =&gt; 'id', 'unique' =&gt; 1))
    );
}
</pre>
<p>To introduce the new column we could use the command from above, which drops the table and recreates it. Or we could use the update command, which performs an &#8220;ALTER TABLE&#8221; statement to synchronize the posts table in the database with the posts table from the schema file:</p>
<pre>
./cake schema run update blog

---------------------------------------------------------------
Cake Schema Shell
---------------------------------------------------------------
Comparing Database to Schema...

The following statements will run.
ALTER TABLE `posts`
        ADD `created` datetime DEFAULT NULL;

Are you sure you want to alter the tables? (y/n)
[n] &gt; y

Updating Database...
posts updated.
End update.
</pre>
<p>With that, our blog database is up-to-date. </p>
<p>Unfortunately, the update command doesn&#8217;t work if you add a new table to the schema file, it doesn&#8217;t create this new table in the database. And so I think the migration process is currently not that useful in practice&#8230;</p>
<p>I used a question mark in the title of this article, as I am not sure if what I described here is really the correct way to use the schema feature. Please correct me, if I am wrong :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/04/13/migrations-the-cakephp-way/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Should the table definition be in the model?</title>
		<link>http://cakebaker.42dh.com/2007/08/04/should-the-table-definition-be-in-the-model/</link>
		<comments>http://cakebaker.42dh.com/2007/08/04/should-the-table-definition-be-in-the-model/#comments</comments>
		<pubDate>Sat, 04 Aug 2007 15:42:25 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[model]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2007/08/04/should-the-table-definition-be-in-the-model/</guid>
		<description><![CDATA[That&#8217;s a question I ask myself after reading an article about an alternative approach to realize migrations in Rails. With that approach, you define the columns of your table in the model and then you generate the migration files from the model. You no longer have to touch any SQL scripts or to write migration [...]]]></description>
			<content:encoded><![CDATA[<p>That&#8217;s a question I ask myself after reading an <a href="http://hobocentral.net/blog/2007/07/06/so-long-migrations/">article</a> about an alternative approach to realize migrations in Rails. With that approach, you define the columns of your table in the model and then you generate the migration files from the model. You no longer have to touch any SQL scripts or to write migration files &#8212; if there are changes in the table definition, you make them in the model.</p>
<p>I like this idea, as it allows you to work on a higher level, i.e. you no longer have to deal with foreign keys, association tables etc. That&#8217;s automatically done for you. And the more I think about this topic, the more I am convinced that the table definition should be in the model. You can look at the column types of a table as basic &#8220;validation rules&#8221; used by the database to check whether the data you want to insert are correct. For example, a column type of varchar(255) could be translated to the validation rule &#8220;maxLength = 255&#8243;. By moving those &#8220;validation rules&#8221; to the model level, we have all validation rules related to a model in one place. And that&#8217;s the logical place where they should be in my opinion.</p>
<p>What do you think about this idea?</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2007/08/04/should-the-table-definition-be-in-the-model/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Support for Oracle databases</title>
		<link>http://cakebaker.42dh.com/2006/12/01/support-for-oracle-databases/</link>
		<comments>http://cakebaker.42dh.com/2006/12/01/support-for-oracle-databases/#comments</comments>
		<pubDate>Fri, 01 Dec 2006 09:29:56 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[feature]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2006/12/01/support-for-oracle-databases/</guid>
		<description><![CDATA[In the latest version from trunk (version 1.1.11.4043) there is now direct support for Oracle databases. From looking at the code I would say it is experimental, but if you use Oracle, give it a try and report possible bugs at https://trac.cakephp.org. Update (2006-12-02): I forgot to mention that the Oracle support has been contributed [...]]]></description>
			<content:encoded><![CDATA[<p>In the latest version from trunk (version 1.1.11.4043) there is now direct support for Oracle databases. From looking at the code I would say it is experimental, but if you use Oracle, give it a try and report possible bugs at <a href="https://trac.cakephp.org">https://trac.cakephp.org</a>.</p>
<p>Update (2006-12-02): I forgot to mention that the Oracle support has been contributed by <a href="http://jeff.loiselles.com/wordpress/">Jeff Loiselle</a> (aka phishy).</p>
<p>Update (2006-12-04): The Oracle driver has been removed from trunk and the 1.1.x.x branch. It is still available in the 1.2 branch.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2006/12/01/support-for-oracle-databases/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Is a dedicated database connection for testing purposes needed?</title>
		<link>http://cakebaker.42dh.com/2006/11/29/is-a-dedicated-database-connection-for-testing-purposes-needed/</link>
		<comments>http://cakebaker.42dh.com/2006/11/29/is-a-dedicated-database-connection-for-testing-purposes-needed/#comments</comments>
		<pubDate>Wed, 29 Nov 2006 09:41:45 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2006/11/29/is-a-dedicated-database-connection-for-testing-purposes-needed/</guid>
		<description><![CDATA[You probably noticed that in app/config/database.php by default two database connections are defined: &#8220;default&#8221; and &#8220;test&#8221;. Even though &#8220;test&#8221; is not used often, it is clear for what it is thought: to define a database connection for testing purposes. But I am not quite sure whether you really need such a dedicated database connection for [...]]]></description>
			<content:encoded><![CDATA[<p>You probably noticed that in app/config/database.php by default two database connections are defined: &#8220;default&#8221; and &#8220;test&#8221;. Even though &#8220;test&#8221; is not used often, it is clear for what it is thought: to define a database connection for testing purposes. But I am not quite sure whether you really need such a dedicated database connection for testing. To me it looks like unnecessary overhead. Wouldn&#8217;t it be easier to use the &#8220;default&#8221; connection on the development machine for testing purposes, and on the live server for the real database? What do you think about it?</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2006/11/29/is-a-dedicated-database-connection-for-testing-purposes-needed/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>DB migrations</title>
		<link>http://cakebaker.42dh.com/2006/11/25/db-migrations/</link>
		<comments>http://cakebaker.42dh.com/2006/11/25/db-migrations/#comments</comments>
		<pubDate>Sat, 25 Nov 2006 10:05:15 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[ant]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2006/11/25/db-migrations/</guid>
		<description><![CDATA[Some days ago Joel Moss released a new version of Cake DB Migrations. As this version also supports advanced installations, I tested it a bit. After fixing some small glitches it worked fine. But, well, it seems that it doesn&#8217;t fit to my working style. As I do a lot of tiny steps to build [...]]]></description>
			<content:encoded><![CDATA[<p>Some days ago Joel Moss released a new version of <a href="http://joelmoss.info/switchboard/blog/1992:Cake_DB_Migrations_strikes_back">Cake DB Migrations</a>. As this version also supports advanced installations, I tested it a bit. After fixing some small glitches it worked fine. But, well, it seems that it doesn&#8217;t fit to my working style. As I do a lot of tiny steps to build a database, the overhead when using Migrations is rather high: for each change there is a migration file. The format of those migration files is rather &#8220;talkative&#8221;, and I miss a bit of magic, i.e. if I use create_table in the UP section, I would expect that automagically a drop_table is done in the DOWN section. Last, but not least, the migration tool works similar to the bake script, which means you have to select several options each time you want to apply a migration. And that is a pain! But test it yourself to see whether it fits to your working style.</p>
<p>And so I am back to <a href="http://ant.apache.org/">Apache Ant</a> and the good old SQL scripts ;-)  My approach follows a simple pattern after a modification of the SQL scripts: drop, create and insert. Here is the Ant script I use:</p>
<pre>
&lt;?xml version="1.0"?&gt;
&lt;project name="myproject" default="default"&gt;
  &lt;property name="driver" value="com.mysql.jdbc.Driver"&gt;&lt;/property&gt;
  &lt;property name="url" value="jdbc:mysql://localhost/myproject?characterEncoding=UTF-8"&gt;&lt;/property&gt;
  &lt;property name="user" value="root"&gt;&lt;/property&gt;
  &lt;property name="password" value=""&gt;&lt;/property&gt;

  &lt;target name="default" &gt;
    &lt;sql driver="${driver}" password="${password}" url="${url}" userid="${user}" src="drop.sql" encoding="UTF-8"&gt;&lt;/sql&gt;
    &lt;sql driver="${driver}" password="${password}" url="${url}" userid="${user}" src="create.sql" encoding="UTF-8"&gt;&lt;/sql&gt;
    &lt;sql driver="${driver}" password="${password}" url="${url}" userid="${user}" src="insert.sql" encoding="UTF-8"&gt;&lt;/sql&gt;
  &lt;/target&gt;
&lt;/project&gt;
</pre>
<p>This script I can then execute with one mouse click directly from my IDE. Simple and fast.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2006/11/25/db-migrations/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using UTF-8 as encoding for the database connection</title>
		<link>http://cakebaker.42dh.com/2006/04/16/using-utf-8-as-encoding-for-the-database-connection/</link>
		<comments>http://cakebaker.42dh.com/2006/04/16/using-utf-8-as-encoding-for-the-database-connection/#comments</comments>
		<pubDate>Sun, 16 Apr 2006 08:28:10 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=167</guid>
		<description><![CDATA[I already wrote about this topic some time ago. In that post I described an approach using the configuration file of MySQL, and mentioned casually that you could execute &#8220;SET NAMES utf8&#8243; if you do not have access to the MySQL configuration file. But I did not show a solution for that case&#8230; Well, today [...]]]></description>
			<content:encoded><![CDATA[<p>I already wrote about this topic some time ago. In that <a href="http://cakebaker.wordpress.com/2006/01/16/trouble-with-utf-8/">post</a> I described an approach using the configuration file of MySQL, and mentioned casually that you could execute &#8220;SET NAMES utf8&#8243; if you do not have access to the MySQL configuration file. But I did not show a solution for that case&#8230;</p>
<p>Well, today I saw a solution, presented by nate in a <a href="http://groups.google.com/group/cake-php/browse_thread/thread/7fab97f558245a3b/ffcea44b2bc8c73f#ffcea44b2bc8c73f">post</a> in the google group:</p>
<pre>
// app/app_model.php
class AppModel extends Model
{
    function __construct()
    {
        parent::__construct();
        $this-&gt;execute("Set NAMES 'UTF8'");
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2006/04/16/using-utf-8-as-encoding-for-the-database-connection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Store your sessions in a different database</title>
		<link>http://cakebaker.42dh.com/2006/03/03/store-your-sessions-in-a-different-database/</link>
		<comments>http://cakebaker.42dh.com/2006/03/03/store-your-sessions-in-a-different-database/#comments</comments>
		<pubDate>Fri, 03 Mar 2006 14:04:41 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=130</guid>
		<description><![CDATA[Since RC5 it is possible to store the sessions in the database. You can enable that with setting the constant CAKE_SESSION_SAVE in app/config/core.php to &#8220;database&#8221; (make sure you have created the necessary table with app/config/sql/sessions.sql). By default, you create this session table in the same database which contains your data. But what to do if [...]]]></description>
			<content:encoded><![CDATA[<p>Since RC5 it is possible to store the sessions in the database. You can enable that with setting the constant CAKE_SESSION_SAVE in app/config/core.php to &#8220;database&#8221; (make sure you have created the necessary table with app/config/sql/sessions.sql). By default, you create this session table in the same database which contains your data. </p>
<p>But what to do if you want to separate your data from the session data for some reason? Well, you need two database configurations in app/config/database.php. The &#8220;default&#8221; configuration must point to the database for the sessions as the session component uses the &#8220;default&#8221; configuration (it is hardcoded). For the second database configuration you can choose any name. You have to add this name to your app/app_model.php like:</p>
<pre>
class AppModel extends Model
{
    var $useDbConfig = 'mydb';
}
</pre>
<p>That&#8217;s it. Thanks to PhpNut for the explanations, and to alexfill for asking questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2006/03/03/store-your-sessions-in-a-different-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Switch your database configuration based on some criteria</title>
		<link>http://cakebaker.42dh.com/2006/02/24/switch-your-database-configuration-based-on-some-criteria/</link>
		<comments>http://cakebaker.42dh.com/2006/02/24/switch-your-database-configuration-based-on-some-criteria/#comments</comments>
		<pubDate>Fri, 24 Feb 2006 13:10:46 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=120</guid>
		<description><![CDATA[Currently, I am experimenting with unit tests. These tests should use an own database. And so I defined a &#8220;test&#8221; configuration in app/config/database.php: var $test = array('driver' =&#62; 'mysql', 'connect' =&#62; 'mysql_pconnect', 'host' =&#62; 'localhost', 'login' =&#62; 'user', 'password' =&#62; 'password', 'database' =&#62; 'project_name-test', 'prefix' =&#62; ''); But it does not automatically use the &#8220;test&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>Currently, I am experimenting with unit tests. These tests should use an own database. And so I defined a &#8220;test&#8221; configuration in app/config/database.php:</p>
<pre>
var $test    =  array('driver'    =&gt; 'mysql',
                      'connect'  =&gt; 'mysql_pconnect',
                      'host'     =&gt; 'localhost',
                      'login'    =&gt; 'user',
                      'password' =&gt; 'password',
                      'database' =&gt; 'project_name-test',
                      'prefix'    =&gt; '');
</pre>
<p>But it does not automatically use the &#8220;test&#8221; configuration when I run a test. So I wrote a constructor for app/config/database.php based on the idea from <a href="http://wiki.cakephp.org/tutorials:change_database_config">http://wiki.cakephp.org/tutorials:change_database_config</a>, which switches the database configuration based on the request url (thanks to gwoo for the hint):</p>
<pre>
function __construct ()
{
    if (strstr($_SERVER['REQUEST_URI'], '/tests'))
    {
        $this-&gt;default = $this-&gt;test;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2006/02/24/switch-your-database-configuration-based-on-some-criteria/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
