Joomla with Doctrine in my Component

84 views
Skip to the first unread message

João Neto

unread,
10 Oct 2009, 08:36:2110/10/2009
to Joomla! General Development
Hello!

I'm working on a new component and found it interesting to show the
community how to put the Doctrine ORM to run!
Basically I'm doing a common component, with controls, models and
view.
What I did was to change the JModel Models for Doctrine_Records
instances ...

In my router i put this:

require_once( dirname(__FILE__) . DS . '_lib' . DS . 'doctrine' . DS .
'Doctrine.php' );
spl_autoload_register( array( 'JLoja_Router', 'autoload' ) );
$manager = Doctrine_Manager::getInstance();
$dsn = 'mysql:dbname=jloja_db2;host=localhost';
$user = 'root';
$password = 'root';

$dbh = new PDO($dsn, $user, $password);
Doctrine_Manager::connection($dbh);

Doctrine_Manager::getInstance()->setAttribute
(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);

Doctrine::loadModels( dirname(__FILE__) . DS . 'models' );

My autoload function override the Joomla and the Doctrine Autoload:

public static function autoload( $className )
{
Doctrine::autoload( $className ); //Doctrine Autoload
JLoader::load( $className ); // Joomla Autoload

$fields = str_replace( array( '_' ), array( DS ) , strtolower
( $className ) );

//is for my component ( not necessary )
if( file_exists( JPATH_COMPONENT_ADMINISTRATOR . DS . '_lib' . DS .
$fields . '.php' ) ){
require_once JPATH_COMPONENT_ADMINISTRATOR . DS . '_lib' . DS .
$fields . '.php';
}

}

Doctrine it works without requires and includes in my component and no
conflicts with Joomla...

João Neto

unread,
10 Oct 2009, 08:38:4610/10/2009
to Joomla! General Development
Remembering that I generated from my bank models from YML in the
"models" folder, as the default installation of Doctrine.

dukeofgaming

unread,
10 Oct 2009, 11:05:0510/10/2009
to joomla-de...@googlegroups.com
Hi, I also use doctrine in Joomla and found it to be so good that I suggested it for Joomla itself (because the ORM project was abandoned).


Also, perhaps you'll want to integrate it like this: http://pastie.org/508338 so you don't have to specify the DSN manually if you change configurations, that also handles the table prefix.

Regards,

David

dukeofgaming

unread,
10 Oct 2009, 22:00:5410/10/2009
to Joomla! General Development
Whoops, pasted bad links, I meant these two:

http://groups.google.com/group/joomla-dev-cms/browse_thread/thread/46ca45a47524d9f/f2e667625b314ca6?lnk=gst&q=doctrine#f2e667625b314ca6

http://pastie.org/508338

And yes, Doctrine supports dynamic table prefixes, but joomla's prefix
will not always be "jos_", so you must retrieve the prefix from
joomla's config (as shown, in the pastie link), otherwise it is not a
proper integration:

$config =& JFactory::getConfig();
$DOCTRINE->connection = Doctrine_Manager::connection( $config->getValue
('config.dbtype').'://'.
$config->getValue('config.user').':'.
$config->getValue('config.password').'@'.
$config->getValue('config.host').'/'.
$config->getValue('config.db'),
'default');
Doctrine_Manager::getInstance()->setAttribute
(Doctrine::ATTR_TBLNAME_FORMAT, $config->getValue
('config.dbprefix').'%s');

Regards =),

David

On 10 Oct, 10:05, dukeofgaming <dukeofgam...@gmail.com> wrote:
> Hi, I also use doctrine in Joomla and found it to be so good that I
> suggested it for Joomla itself (because the ORM project was abandoned).
> You can see the thread here:http://mail.google.com/mail/#inbox/1243e733e410972a
>
> <http://mail.google.com/mail/#inbox/1243e733e410972a>Also, perhaps you'll
> want to integrate it like this:http://pastie.org/508338so you don't have

Ed Rowland

unread,
12 Oct 2009, 03:28:3612/10/2009
to joomla-de...@googlegroups.com
Great stuff. Thanks for the ideas and thanks David for the links. The
(pastie.org/508338so) is super.

Regards
Ed

dukeofgaming

unread,
12 Oct 2009, 15:51:2012/10/2009
to joomla-de...@googlegroups.com
Hi, no problem, glad it helped. I'd like to add that using these attributes additionally is recommendable:

Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, true);

The first one enforces some type matching (including type sizes) and null values. The second one wraps the column names in backticks (in the case of MySQL) so if you have columns named date, status, order, etc... you won't get conflicts.

Regards
Reply all
Reply to author
Forward
0 new messages