Doctrine Framework in Yii Framework


(Christian Neusz) #1

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


(Qiang Xue) #2

Where is Doctrine_Query.php located?


(Christian Neusz) #3

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


(Qiang Xue) #4

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.


(system) #5

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


(system) #6

This code works for me:

require_once(dirname(__FILE__) . '/framework/vendors/doctrine/Doctrine.php');


spl_autoload_register(array('Doctrine', 'autoload'));





$manager = Doctrine_Manager::getInstance();

(system) #7

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


(Qiang Xue) #8

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


(Qiang Xue) #9

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