Yii Framework Forum: Doctrine Framework in Yii Framework - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Doctrine Framework in Yii Framework Rate Topic: -----

#1 User is offline   NoiZy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 10-February 09

Posted 11 February 2009 - 02:36 AM

Hi Guys,

I hope you can help me.
I'm new to the Yii Framework but I already finish a little project and Yii is great!

But now I have to develop a bigger project with DB and I want to use the Doctrine Framework for all DB operations.

Now is the question, how can I use it??

I extract the lib into "protected/vendors/Doctrine/"
and I create the following controller:

<?php
Yii::import('application.vendors.*');
require_once('Doctrine/Doctrine.php');


class wtzController extends CController
{
protected $DB;


public function  __construct($id)
{
$this->DB = new Doctrine_Query();

parent::__construct($id);
}
}
?>


which I use for controllers.
But I get the exception:
include(Doctrine_Query.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory


I hope somebody can help me!

Best regards
NoiZy
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 11 February 2009 - 07:03 AM

Where is Doctrine_Query.php located?
0

#3 User is offline   NoiZy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 10-February 09

Posted 11 February 2009 - 07:51 AM

Hi,

structure:
/protected/vendors/Doctrine/doctrine.php -> Main file with autoload function
/protected/vendors/Doctrine/Doctrine/query.php -> Doctrine_Query class
/protected/vendors/Doctrine/Doctrine/Many files and folders
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 11 February 2009 - 09:54 AM

Because the class file does not have the same name as the class name, Yii is unable to load it automatically. You will need to install a separate autoload function (maybe it is provided by Doctrine already?)
You may also explicitly include the needed files.
0

#5 User is offline   pillais 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-February 09

Posted 18 February 2009 - 02:43 AM

Hey,
I'm having the same problem. Doctrine also has it's own autoload function in Doctrine.php and is also called autoload(). I tried renaming this function to doctrine_autoload() and ran the following code.

require_once ('protected/vendors/Doctrine/lib/Doctrine.php');
// register the autoloader
spl_autoload_register(array('Doctrine','doctrine_autoload'));

Doctrine_Manager::getInstance()->setAttribute('model_loading','conservative');
Doctrine::loadModels('classes'); // This call will not require the found. php files

When it comes to the Doctrine_Manager statement, Yii is still trying to use the YiiBase autoload() function to load the Doctrine_Manager class and it errors out.

How do I install the doctrine_autoload() function to be called only if it's tryiing to load a Doctrine class? Should I edit YiiBase.php autoload() function? Is that the correct way to do it?

Thanks for your help in advance.
Sujeet Pillai
0

#6 User is offline   extesy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 14-January 09

Posted 18 February 2009 - 03:23 AM

This code works for me:

require_once(dirname(__FILE__) . '/framework/vendors/doctrine/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));

$manager = Doctrine_Manager::getInstance();

0

#7 User is offline   pillais 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-February 09

Posted 18 February 2009 - 03:26 AM

I got it working finally. The problem is that the YiiBase::autoload function is the first in the autoload stack and it errors out in that function before it goes to the Doctrine autoload function. If the YiiBase::autoload function returned a false if it didn't find the file, the stack would progress to the next autoload function. This has been done in the Doctrine autoload function. so I essentially put the Doctrine Autoload function first in the stack and readded the YiiBase::autoload second. Now it works.

Note: Do NOT rename the autoload function in doctrine since it is called explicitly in some places in Doctrine.

Here's the hack I used.

require_once ('protected/vendors/Doctrine/lib/Doctrine.php');
// register the autoloader
spl_autoload_unregister(array('YiiBase','autoload'));
spl_autoload_register(array('Doctrine','autoload'));
spl_autoload_register(array('YiiBase','autoload'));
//print_r(spl_autoload_functions());
Doctrine_Manager::getInstance()->setAttribute('model_loading','conservative');
Doctrine::loadModels('protected/models'); // This call will not require the found. php files



Hope that helps. I however would like to know if this is the right way to do it?
Any comments guys?

Thanks in advance,
Sujeet Pillai

0

#8 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 18 February 2009 - 06:56 AM

Yes, calling Doctrine's autoload is the right way to go.
0

#9 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 18 February 2009 - 06:57 AM

Maybe you can post your findings as a [url=/doc/cookbook]cookbook page[/ur]?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users