Doctrine

Doctrine Generate Models from DB

Automatically generating the models for a project based off of existing database tables is extremely handy. I’ve had to do this several times and the available options are key, especially when generating these for a Zend Framework layout. The following stack overflow entry is very helpful:

http://stackoverflow.com/questions/2605291/what-are-the-options-available-for-doctrines-doctrine-coregeneratemodelsfromd/6541608#6541608

/**
* Doctrine CLI script
*/

define('APPLICATION_ENV', 'development');

define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/../library/Doctrine'),
get_include_path(),
)));

require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/config/default.ini'
);

$application->getBootstrap()->bootstrap('doctrine');

Doctrine::generateModelsFromDb(
APPLICATION_PATH . '/modules/default/models/DbTable',
array('db1'),
array(
'pearStyle' => true,
'generateTableClasses' => true,
'baseClassesDirectory' => '',
'classPrefix'=> 'Model_DbTable_',
'classPrefixFiles' => false,
'baseClassPrefix' => 'Generated_'
)
);

Tuesday, November 15th, 2011 Doctrine, Zend Framework No Comments

RegEx for Doctrine Connection String (DSN Format)

I needed to parse a DSN that was in the format used for Doctrine so that I could create my own PDO instances with the same connection strings found in my application.ini file. The regex is as follows:


^(\w+)\:\/\/(\w+)\:(.+)\@([0-9\.]+)\/(\w+)

This will parse a dsn formatted: mysql://db_username:db_password@db_host/db_name.

Came in very handy and hope it can help someone else to take a shortcut!

Wednesday, July 14th, 2010 Doctrine No Comments

Doctrine ORM – Saving MySQL DateTime Format ‘NOW()’ into a Record

I ran into an issue with a column type of ‘timestamp’ in one of my models. If I did not specifically set the date field, it would save it as a timestamp and throw things off in the database.

So I searched around a bit and found the following solution:

$newUser = new Model_DbTable_User();
$newUser ->fromArray($_POST);
$newUser ->date = new Doctrine_Expression('NOW()');

‘date’ being a column inside of the User model that is type ‘timestamp’. I am utilizing the fromArray method in order to inject my form values directly into the database (so nice!). The fix that I am talking about is the 3rd line of code. In order to save with the datetime format, you must specify the Doctrine Expression as shown here. Works like a charm.

Tuesday, October 27th, 2009 Doctrine 2 Comments

Doctrine – Special Characters in DSN Database Connection String

I couldn’t seem to get one of my connections to work in a recent project and thought that I was doing everything correctly in my Zend application.ini file.

The password had special characters in it, one of them being a character used for other purposes inside of a dsn connection string. So I needed to add some HEX to my connection string to replace the special char. All this info is listed inside the doctrine ORM documentation and was easy to find once I figured out the problem. The following characters need to be replace with their corresponding hex value inside your application.ini:
› Continue reading

Tuesday, September 29th, 2009 Doctrine No Comments

Doctrine – Binding a specific connection to a Model in Zend Framework

I have been looking into this for awhile now and had not figured out how I could bind a connection to a model without having to extend it in some way.

Finally… and of course it was simple and right there in front of me, but without an actual example I didn’t know!
› Continue reading

Friday, September 4th, 2009 Doctrine 7 Comments

Doctrine ORM – Create Native SQL

I just wanted to have this here for my own records!

Credits to: http://blog.whirix.com/2008/12/how-to-make-native-sql-queries-in.html

Sometimes you have situation which require writing of native SQL query in project fully based on Doctrine ORM. Doing DQL queries is very easy, but simple SQL queries maybe difficult to execute because you have to reuse database connection opened inside Doctrine.
Example given below is easy to understand and use in your project(s). Enjoy!

//Note: If you want to select a specific connection first, then call:
//Doctrine_Manager::getInstance()->setCurrentConnection('your_connection_name_1');

// Get Doctrine_Connection object
$con = Doctrine_Manager::getInstance()->connection();

// Execute SQL query, receive Doctrine_Connection_Statement
$st = $con->execute("...............");

// Fetch query result
$result = $st->fetchAll();
Tuesday, September 1st, 2009 Doctrine 2 Comments

Zend Framework – Adding Doctrine ORM into Zend Framework 1.8+

I will add more details as to add things to your application.ini file, but for now

I want to record some OS specific notes – namely for those using Mac OSX.

I kept getting the error:
‘Couldn’t locate driver named mysql’

I ran into this problem when I was trying to create models from the command line (terminal).

My php.ini in MAMP was including the pdo_mysql.so – so I didn’t understand what in the world was going on. After a bit of searching I got the idea that it was using the base PHP included with the OS instead of my installed MAMP installation – sure enough that was it.

So in the terminal I needed to make sure I was running the MAMP version of php by pre-fixing my command with:
/Applications/MAMP/bin/php5/bin/php

Instead of: php

Then everything was happiness and glory.

More to come… I’m just starting with this.

Thursday, August 27th, 2009 Doctrine, Zend Framework No Comments
[ad code=1 align=center]