Unit Testing Doctrine 2 Entities
Description
Integrate Doctrine 2 into your Zend Framework project. Drive with tests first!
Tags
doctrine, doctrine2, tdd, Unit Testing
Integrate Doctrine 2 into your Zend Framework project. Drive with tests first!
doctrine, doctrine2, tdd, Unit Testing
Hi,
for all of you who have problems with “class not found” while using the EntityManager. Instead of using the raw classname you have to use the Namespace.
For example:
$doctrine = Zend_Registry::get(‘doctrine’);
$entityManager = $doctrine->getEntityManager();
$user = $entityManager->find(‘EN\Entity\Users”, 1);
Which will fetch the user with the ID 1. EN is my namespace. Change it accordingly.
I hope it helps.
Thanks for the great video. I was wondering if its possible to auto generate entities from a database using doctrine2 cli. I believe this would save a lot of time as we have a very extensive db designed in mysql.
@Sunny it is possible. You need to use doctrine’s command line tool for this.
e.g. php doctrine.php orm:convert-mapping –from-database /path/to/your/entities
For more information you should use google. orm:convert-mapping is the option you have to look for.
@AlexM
Thanks AlexM. It works!
I’m new with ZF and Doctrine. How to use Doctrine Query Language (DQL)?
Thanks for the walkthrough!
Wanna ask quick question, I tried it with the latest version of doctrine (so I only copy Bisna from NOLASnowball and use latest doctrine 2.1 for the Doctrine and move up Symfony folder from the Doctrine folder) and when I tried the steps in the video (around minute 22-24) when dumping sql to check if the create table works, I instead get an AnnotationException error.
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation “@Table” in class ZC\Entity\User does not exist, or could not be auto-loaded.
orm:schema-tool:create [--dump-sql]
Any ideas?
@Hendra
Any solution please???
Got the same error
@Henry
I solved the prob, but forgot how/what I did. But I do remember I have it solved after visiting these websites:
http://groups.google.com/group/doctrine-user/browse_thread/thread/a2a02b8576d211d9/22ea0fa5a529e6c7?pli=1
https://github.com/guilhermeblanco/ZendFramework1-Doctrine2
http://www.kurttest.com/zfa/bisna.html
Hope that helps!
@Hendra
I solved it too and found the same links
The real problem was with “Bisna” glue, fortunately there is updated version on guilhermeblanco github, like you sad is here: https://github.com/guilhermeblanco/ZendFramework1-Doctrine2
Anyone having the same issue should check links posted by Hendra!
Thanks Hendra and thanks Jon for great tutorial..
Jon,
would it be possible to provide an updated version of this podcast?
NOLASnowball seems to be incompatible w/ Doctrine 2.1+.
Thanks!
Hi, Jon
Thx for the excellent tutorial!
I did the same, but unittest cannot delete file in spite of I’m using windows. File is not just for reading and I can delete it manually.
Console shows me following result:
ZC\Entity\UserTest::testCanCreateUser
unlink(D:\[path]/../tests/data/test.db): Permi
ssion denied.
Have you ever had this problem?
Hi SAnthony,
I’m getting the same problem using Wamp. chmod is 0777. Did you ever find a solution?
Cheers,
Paul.
For all who has the same problem as Hendra, Henry and me: I found the soulution in line
resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationFiles[]
in application.ini. Good Luck!
I have setup a project on Windows according to your video, but when PHPUnit runs, I get “Permission Denied” error when the application tries to drop the SQLite schema .db file. Is there any way to overcome this problem?
Thank you , great job.
Thanks for this screencast, Jon. I have learned a lot and am looking forward to using doctrine in future projects.
I think I have also found a solution to the Permission Denied error on Windows when unlink is called in the dropSchema() method. The SchemaTool class has a method dropDatabase() which removes the data from the sqlite database file, but does not actually delete the file.
To use this solution, edit ModelTestCase to remove the dropSchema() method and all calls to it, then replace the setUp() method with the following:
public function setUp()
{
global $application;
$application->bootstrap();
$this->doctrineContainer = Zend_Registry::get(‘doctrine’);
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->doctrineContainer->getEntityManager());
$tool->dropDatabase();
$tool->createSchema($this->getClassMetas(APPLICATION_PATH . ‘/../library/App/Entity’, ‘App\Entity\\’));
parent::setUp();
}