Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2009-11-23 16:51:19

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

ZC38 – Introducing Doctrine 1.2 Integration

By popular demand, I've put together an introduction to Doctrine 1.2 integration with the Zend Framework. Doctrine is a fantastic ORM (object-relational mapping tool). If you've jumped from NHibernate or Hibernate and are itching to have a powerful tool for handling database relationships, Doctrine is probably your best bet. On the mailing lists, there's a lot of talk about integrating Doctrine 2.0 into the 2.0 release of the Zend Framework. Regardless, this will help you get up to speed today. In next week's video, I'll show you how you can unit test those models in the Zend Framework.

Enjoy!

Grab a copy of the project or browse the repository.

Offline

#2 2009-11-24 16:21:31

Vaidas
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

I used full your code (with same structure)

Why I am getting error
"PHP Fatal error:  Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Resource matching "doctrine" not found' "

After calling "./doctrine build-all-reload"

#3 2009-11-24 16:27:27

Vaidas
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

Vaidas wrote:

I used full your code (with same structure)

Why I am getting error
"PHP Fatal error:  Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Resource matching "doctrine" not found' "

After calling "./doctrine build-all-reload"


Sorry error in application.ini

#4 2009-11-24 22:32:30

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC38 – Introducing Doctrine 1.2 Integration

sounds like your method name in the Bootstrap.php file might not be named "_initDoctrine"

can you double check it?

Offline

#5 2009-11-25 05:14:15

abtris
New member
From: Prague
Registered: 2009-11-13
Posts: 5
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

Nice tutorial.

Can you give me advice, how you use doctrine with models. How change my architecture using doctrine instead Zend_Db.

I make class for data (User.php) and mapper (UserMapper.php) for gateway to Zend_Db class mapped to Table.

Doctrine save some some part work, but i can write schema, forms, model manually.

In symfony can generate from schema forms. Is possible make them now in ZF?

Thanks for your work.

Offline

#6 2009-11-25 15:25:17

ryan.horn
Member
From: Buffalo, NY
Registered: 2009-09-08
Posts: 41
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

You're probably better off waiting to see if an official integration between doctrine and Zend will happen. I looked into having auto-generated forms based on doctrine models, but right now it is too much of a headache to get it working properly. If you're curious, take a look at http://n4.nabble.com/Zend-Form-and-Doct … ml#a669957 for starters.

Offline

#7 2009-12-02 21:03:26

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

I posted this in the comments of the video before i found the forums.

Hi. Very interesting video. I’m quite new to zend framework and completely new to doctrine, but I'm eager to learn how to use it.

I downloaded the project and tried to run it but I’m getting this error. I’m not shure if its related to xampp, zend or doctrine. Can anyone help?

Error message:
“Warning: include(Doctrine\Locator\Injectable.php) [function.include]: failed to open stream: No such file or directory in D:\dev\xampp\php\PEAR\Zend\Loader.php on line 83

Warning: include() [function.include]: Failed opening ‘Doctrine\Locator\Injectable.php’ for inclusion (include_path=’D:\dev\xampp\htdocs\doctrineintro\application/../library;D:\dev\xampp\htdocs\doctrineintro\library;.;D:\dev\xampp\php\PEAR’) in D:\dev\xampp\php\PEAR\Zend\Loader.php on line 83

Fatal error: Class ‘Doctrine_Locator_Injectable’ not found in D:\dev\xampp\htdocs\doctrineintro\library\Doctrine\Configurable.php on line 35″



I have used JPA with Java earlier. How does Doctrine compare to JPA?

Last edited by harri (2009-12-02 21:27:51)

Offline

#8 2009-12-02 22:56:45

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC38 – Introducing Doctrine 1.2 Integration

hey Harri, it looks like for some reason, your Doctrine classes aren't being loaded. First of all, are you using Doctrine 1.2? Doctrine 2 will require a different bootstrapping strategy.

in terms of JPA, I can't comment, however I was immediately attracted to Doctrine as someone who was working with NHibernate in C# projects. Doctrine 2 will follow the POPO (plain-old PHP objects) approach and remove any sort of inheritance to Doctrine's persistence mechanisms. I don't see this as a huge issue right now since there are few serious architectural contenders to Doctrine in the PHP space and decoupling will happen in Doctrine 2 regardless.

Offline

#9 2009-12-02 23:13:01

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

I am using the source code you had made available for the video. So that would be Doctrine 1.2?

Offline

#10 2009-12-02 23:15:18

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC38 – Introducing Doctrine 1.2 Integration

yes. I think you might have a path issue, since your htdocs folder should be the equivalent of the Zend Framework's "public" folder...

The Zend Framework folder structure is such that only the public/ folder is externally accessible. That could be causing issues.

Offline

#11 2009-12-03 04:00:07

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

I found out that i hadn't added Zend library to the project in the htdocs, had only added it to where the netbeans projects was. But after added the Zend library it still wouldn't work.

I have tried moving around xampp, htdocs, the project and checked if it had something to do with / vs \, it shouldn't but i tested it. I have also set ut a virtual host so that points to the public folder.

The wierd thing is that I have the zend quick start project working....
*1-2 hours of making sure that it actually works*
...on localhost/QuickStart/ with no problem. I'm not done with the QuickStart tutorial but it works. It works by just going to localhost/QuickStart/public.

So I've been trying to get this to work for what must be 3 or 4 hours. I have been fixing some other problems as well (the zend_tool 1.9.0 problem among others). The thing I have found out is that when I comment out all the Doctrine related stuff in application.ini and Bootstrap.php, I get no errors.

Offline

#12 2009-12-03 17:10:41

tawfekov
Administrator
From: www
Registered: 2009-08-06
Posts: 25
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

Hey buddies ,
I was working on setup with doctrine and ZF today so i followed all the steps mentioned by jon
but i was getting many crazy errors as you might see in the pic down is one of them
PunBB bbcode test
until i changed this part of _initDoctrine function in the bootstrap file
[code=php] $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, false); [/code]
i had changed the value form true to false
and i had removed this line of code
[code=php] spl_autoload_register(array('Doctrine', 'modelsAutoload'));[/code]

and i got it working very well
i was using WAMP - PHP 5.3 and doctrine 1.2



this is my complete initdoctrine function
[code=php]
    protected function _initDoctrine()
    {
        $this->getApplication()->getAutoloader()
                               ->pushAutoloader(array('Doctrine', 'autoload'));
        //spl_autoload_register(array('Doctrine', 'modelsAutoload'));
        $manager = Doctrine_Manager::getInstance();
        $manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
        $manager->setAttribute(
            Doctrine::ATTR_MODEL_LOADING,
            Doctrine::MODEL_LOADING_CONSERVATIVE
        );
        $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, false);
        $manager->setCharset('utf8');
        $doctrine = $this->getOption('doctrine');
        $conn = Doctrine_Manager::connection($doctrine['dsn'], 'doctrine');
        $conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
        return $conn;
    }
[/code]


update : sorry i am using doctrine version 1.1.5 stable
any one has seen this problem before ??????

Last edited by tawfekov (2009-12-03 18:44:27)

Offline

#13 2009-12-03 17:38:21

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

I thought I had written it but I hadn't.
For some reason I get no errors now. Maybe I fixed the problem when I fixed the stuff for the quick start project. Anywho, when I removed the commenting I had done on the bootstrap everything seems to work.

But now I have some other problems.

My quick start project and the doctrine project is acting kind of different.
In the QS project I have to comment out this part.

includePaths.library = APPLICATION_PATH "/../library"

to not get this error:

Fatal error: Cannot redeclare class Zend_Loader in D:\dev\xampp\htdocs\QuickStart\library\Zend\Loader.php on line 31

which from what I understand means that the zend library have been included before. So then I removed the inclusion of zend in the index.php but I still get the error. So I cant have the inclusion in the application.ini file, only in index.php.

But in my doctrine project I cant remove the inclusion of zend in index.php because then I get that error.

I am confused.

Offline

#14 2009-12-03 17:47:31

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC38 – Introducing Doctrine 1.2 Integration

hmm... include paths shouldn't be messing with you, but doing 'require' multiple times certainly will... not sure what's going on here.

Offline

#15 2009-12-09 21:50:32

Artsemis
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

Jon,

Thanks for this! I'm new to Doctrine with a little Zend experience... with Doctrine 1.2 being moved to "Stable" so recently, this was by far the most up-to-date material on the topic.

I've had a few issues and I'd greatly appreciate it if you can help out a little.

First off, I followed your directions by doing it myself and whenever I try to run the cli script, I get an error:

--
Uncaught exception 'LogicException' with message 'Passed array does not specify an existing static method ' in C:\Pro[...]\Bootstrap.php:17
--

Line 17 in the Bootstrap is the spl_autoload_register() call. Here's the fun part... if I change Bootstrap to Doctrine_Core in spl_autoload_register and the line above it, I get a slightly more clear error that the class 'Doctrine' is not found. If I go ahead and change the static member references of Doctrine that throw that error to Doctrine_Core, everything works great.

I'm on Windows, Doctrines' files are all in my SHARED library folder (on the same level as /Zend/), my shared library folder is in my include_path, and Zend components work just fine.

Now, aside from changing the references of Doctrine to Doctrine_Core in the bootstrapper, another fix is to move the Doctrine library to the PROJECT library folder.

So, I'm desperate for help on this -- why won't Doctrine work in the shared ZF library folder and why would renaming it's usages to Doctrine_Core fix the issue? I understand Doctrine extends Doctrine_Core but that really just makes it more confusing to me with respect to why it's not working. My only guess is that it has to do with ZF registering "Doctrine" as an autoloader namespace, so it's expecting a class name to follow... but that wouldn't explain why it works in the project's library folder. smile


Completely unrelated but I'm also wondering if these are the same thing?
spl_autoload_register(array('Doctrine', 'modelsAutoload'));
Doctrine::loadModels($doctrineConf['models']);

You used the former and I've seen others use the latter.

Thanks much for any help!

#16 2009-12-10 02:24:14

Artsemis
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

Couple minor updates:

1. I downloaded the project file from your website and everything works fine as expected. However, if I move the 2 Doctrine-related directories and Doctrine.php from the library folder to the shared library folder (where my include_path points), it breaks just like mine. So it's definitely not a typo I've made.

2. I'm really beginning to think this has to do with Windows. I'm not sure why it would work in the projects library folder if that were the case, but everyone I've spoken to that had this work following the Zendcast has been on Linux or Mac.

Hopefully this helps you solve the issue for us Windows guys smile

#17 2009-12-10 14:50:14

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC38 – Introducing Doctrine 1.2 Integration

hmmm... the Doctrine_Core issue is something that I need to be investigating myself, since this was a recent move by the Doctrine development team to move everything into the Doctrine_ namespace. This way, you could just register Doctrine like any other namespace in your library folder and use Doctrine_Core:: instead of Doctrine::

I would start there. In terms of the second issue, I addressed this in the last video and mentioned that I needed to run loadModels. There's a lot of confusion around this due to the lack of consistent namespacing for models in Zend. It seems that the best practice going forward is to user Model_ModelName or Module_Model_ModelName.

Doctrine allows this by setting up "pear style model loading". I haven't investigated this personally, but I plan to revisit this part of the project in an upcoming video.

Hope that helps,

j

Offline

#18 2009-12-10 15:04:56

Artsemis
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

Jon,

Thanks for the response.

I agree it's probably best to just use Doctrine_Core directly since Doctrine is just in place for backwards compatibility in the first place. It just really baffled me that taking your project and moving the Doctrine library to the shared library (rather than the project) breaks things. Does this happen for you? If not, I'm pretty convinced it has to do with the include_path stuff going on in our /scripts/doctrine.php file... at least on Windows. Then again, Zend works in the shared library.

So confused!

Regardless, I'm really looking forward to your future videos... I started going through the archive some last night and I see all kinds of them I plan to check out over the coming weeks.

#19 2009-12-11 02:46:20

jiewmeng
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

i am also having issues with autoloading Doctrine class ... i get

D:\Projects\ZF\doctrine\application\scripts>php.exe doctrine.php
PHP Fatal error:  Uncaught exception 'LogicException' with message 'Passed array does not specify an existing static met
hod (class 'Doctrine' not found)' in D:\Projects\ZF\doctrine\application\Bootstrap.php:7
Stack trace:
#0 D:\Projects\ZF\doctrine\application\Bootstrap.php(7): spl_autoload_register(Array)
#1 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(662): Bootstrap-
>_initDoctrine()
#2 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(622): Zend_Appli
cation_Bootstrap_BootstrapAbstract->_executeResource('doctrine')
#3 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(579): Zend_Appli
cation_Bootstrap_BootstrapAbstract->_bootstrap('doctrine')
#4 D:\Projects\ZF\doctrine\application\scripts\doctrine.php(25): Zend_Application_Bootstrap_BootstrapAbstract->bootstrap
('doctrine')
#5 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Loader.php(136): include('D:\Projects\ZF in D:\Projects\ZF\d
octrine\application\Bootstrap.php on line 0

i am also using shared library ... i have my php.ini include path point to the lib folder of doctrine

#20 2009-12-11 06:08:29

Artsemis
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

jiewmeng wrote:

i am also having issues with autoloading Doctrine class ... i get

D:\Projects\ZF\doctrine\application\scripts>php.exe doctrine.php
PHP Fatal error:  Uncaught exception 'LogicException' with message 'Passed array does not specify an existing static met
hod (class 'Doctrine' not found)' in D:\Projects\ZF\doctrine\application\Bootstrap.php:7
Stack trace:
#0 D:\Projects\ZF\doctrine\application\Bootstrap.php(7): spl_autoload_register(Array)
#1 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(662): Bootstrap-
>_initDoctrine()
#2 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(622): Zend_Appli
cation_Bootstrap_BootstrapAbstract->_executeResource('doctrine')
#3 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(579): Zend_Appli
cation_Bootstrap_BootstrapAbstract->_bootstrap('doctrine')
#4 D:\Projects\ZF\doctrine\application\scripts\doctrine.php(25): Zend_Application_Bootstrap_BootstrapAbstract->bootstrap
('doctrine')
#5 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Loader.php(136): include('D:\Projects\ZF in D:\Projects\ZF\d
octrine\application\Bootstrap.php on line 0

i am also using shared library ... i have my php.ini include path point to the lib folder of doctrine

I assume you know from reading my above posts but just incase you don't, a quick fix for this is to just move the library to your project's library folder. I'd still love to know the reason for this though.

Are you on Windows by chance?

#21 2009-12-11 07:58:54

Artsemis
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

Another oddity I've noticed when messing with some debugging output... whenever I get the above mentioned errors, my doctrine.php (cli script) and associated methods (bootstrapper) as well as my ini file execute twice.

The first pass through stops where the error would occur but nothing gets spit out yet -- it then starts over at the beginning of the cli script until it hits that same location again where it stopped on the first pass -- except this time it spits out the error.

Anyone have a clue why this would run twice? I'm aware of the fixes to the errors, this is more abuut simple understanding at this point.

#22 2009-12-11 12:39:52

tawfekov
Administrator
From: www
Registered: 2009-08-06
Posts: 25
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

@jiewmeng

Hi jiewmeng ,
i had exactly the same error and successfully  passed it and i will tell you what i  did


remove this line of code
[code=php] spl_autoload_register(array('Doctrine', 'modelsAutoload'));  [/code]


once you did that you will see error exactly same as the image i mentioned fromweek ago
http://www.zendcasts.com/forum/post/440/#p440

to pass it you should  change this to true ;
[code=php] $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, false);[/code]


this will fix your error same as me
let me know if you still getting errors

Offline

#23 2009-12-11 13:18:26

jiewmeng
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

Artsemis wrote:

I assume you know from reading my above posts but just incase you don't, a quick fix for this is to just move the library to your project's library folder. I'd still love to know the reason for this though.

Are you on Windows by chance?

yes, i read your post. and i have moved the library to proj's library folder and it works. i am on windows too. it seems like a strange issue ...

#24 2009-12-11 13:20:02

jiewmeng
Guest

Re: ZC38 – Introducing Doctrine 1.2 Integration

tawfekov wrote:

@jiewmeng

Hi jiewmeng ,
i had exactly the same error and successfully  passed it and i will tell you what i  did


remove this line of code
[code=php] spl_autoload_register(array('Doctrine', 'modelsAutoload'));  [/code]


once you did that you will see error exactly same as the image i mentioned fromweek ago
http://www.zendcasts.com/forum/post/440/#p440

to pass it you should  change this to true ;
[code=php] $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, false);[/code]


this will fix your error same as me
let me know if you still getting errors

does this change anything with regards to functionality? eg. no autoloading for somethings?

#25 2009-12-11 13:33:34

tawfekov
Administrator
From: www
Registered: 2009-08-06
Posts: 25
Website

Re: ZC38 – Introducing Doctrine 1.2 Integration

yes
i had to change the namespace of each model class in order to get it working for example
[code=php]
class User extends BaseUser
{

}
[/code]
i change it to
[code=php]
class Model_User extends Model_generated_BaseUser
{

}
[/code]


for example this query
[code=php]
Doctrine_Query::CREATE()->from("user")->execute();
[/code]

would be like
[code=php]
Doctrine_Query::CREATE()->from("Model_user")->execute();
[/code]


don't forget to change to proper namespace of each generated class
and
you will need to change the namespace each time you run the doctrine cli for example [build all reload ]

but i still face error and trying to solve it but regarding to this episode it working 100% in my pc

this was tested in doctrine 1.1.5 and its working so
I tried already test my code with 1.2.1 and its working 100% smile

Last edited by tawfekov (2009-12-11 13:57:40)

Offline

Board footer

Powered by FluxBB