I am investigating Doctrine 2 for PHP and I am having trouble finding good resources for how to map using the PHPDriver. There is extensive documentation on using Annotations in comments for the Doctrine mappings, however, I would rather have my mapping definitions in PHP and NOT in comments (for obvious reasons).
I could use some examples mainly with association mapping. The documentation using Annotations doesn't really help me much. Using those I seem to be left guessing through trial and error on how to do it with the PHPDriver.
Does anyone know of a good reference off hand? Any help would be greatly appreciated.
If you haven't seen it already, there is a section on PHPDriver in the Doctrine manual now:
PHP Mapping
The manual gets updated quite frequently, so keep checking back for new topics. You may also want to consult the API documentation directly:
API Documentation
The general terminology and conventions used by all the mapping drivers are the same, so the basic ideas should translate. It's probably syntax that will trip you up.
I would also recommend you look into the XML driver as well. With a good IDE, you can get validation and auto-completion via the xsd file, which makes it easier. Writing out PHP mappings by hand personally strikes me as a bit labor intensive.
The PHP Drivers were rather low priority for now and until we add the programmatic builder that simplifies the PHP mapping it will probably keep being neglected vs annotations/xml/yaml.
However you will find a good example in the unittests (tests/Doctrine/Tests/ORM/Mapping) somehwere.
Related
I have been using ezSQL for the last few years but feel it is outdated. Though I like the simplicity and I like the file based caching ability with json, for small result sets that is.
So starting a new project I was looking for suggestions on a better mysql class for php. I know the db will only be mysql so portability is not a requirement. I read about mysqli extension, pdo etc but just dont know which one would be best for my situation. The site does a lot more reads than writes, though there are times where there are a lot of writes in the admin tool to the db. I looked at doctrine but dont know if that is too "bloated" for what I need. Hopefully this isnt to vague. Any suggestions?
EDIT
The site isnt small, I would consider it a high traffic site with a lot of db queries.
What don't you like about ezSQL? I often wish there was something like it for other protocols/languages I encounter. Every syntax should be written like ezSQL, in my opinion.. It describes the operation to be performed, in as few words as is possible, in the clearest and most logical order. Do you actually have performance problems, or are you just worried that something better has come along? I agree that ezSQL is rarely mentioned, but I have yet to find anything that matches it's simplicity, conciseness, and function...
From what I know of ezSQL (via it's wordpress pendant) I would consider Doctrine as well as too much for the moment because it's a complete data mapper for the database whereas you might be more looking to how to move away from your recent use of ezSQL which I think is a good idea.
Bascially you might be interested in a data-access abstraction layer. That could be PDO as it's build in into PHP. Even if you don't need to change the database server, it will give you defined interfaces how to query and access the data.
As you build the site from scratch, I can suggest you consider using some lightweight framework. A good introduction in my eyes is When Flat PHP meets Symfony which shows how a webapp can generally benefit from patterns and a flexible design.
From experience:
Doctrine - very easy to use I love doctrine query language - I never had to do initial setup though so im not sure how hard it is. It has very good community and lots of tutorials.
Propel - used for a bit. Does the job, very similar to doctrine. However, the documentation is very crap and community is very slack. I found that when I didn't know something it was quite hard to find an answer and often I had to post on Google forums.
Note: If you are starting from scratch you might want to look at some of the frameworks such as symfony+doctrine is a good combination, makes development a lot easier.
Links:
- http://www.doctrine-project.org/
- http://www.propelorm.org/
I am trying to code the process of creating Zend_Form objects using Doctrine2 ClassMetadatas so that form generation for my client database application is easier.
I needed somewhere to store the element labels, descriptions, and etc. using custom phpdoc tags seemed a good idea. But it seems ClassMetadataInfo doesn't provide 'annotations' outside what it's expecting.
Thats sensible I guess, but im struggling to find a way to get it to parse the entire phpdoc block.
I've looked at using getReflectionClass() and parsing it myself, but id loose out on the great caching. The Doctrine2 parser works really well for what it does, but I can't make heads or tails of it!
Being able to use the phpdoc short description as an element label, and long description as the description would be rather handy for my form<>model friendships.
Anyone else pondered on this?
Dotrine2 provides both an AnnotationDriver (which is used by the ORM) and an AnnotationReader (used by the driver and which provides more abstract method).
Take a look at the source
Doctrine\ORM\Mapping\Driver\AnnotationDriver
Doctrine\Common\Annotations\AnnotationReader
In case anyone finds this answer: The latest version of Doctrine as of now includes a more advanced annotation reader, including caching mechanisms (which you probably want). Take a look at the doctrine documentation for some nice examples of usage.
Okay, I'm fed up with phpDocumentator. It consumes way much more memory (1.4 GB) and time (5 minutes on 2.6GHz Core 2 Duo) than I'm willing to give it. Does there exist some really compatible program to generate documentation for PHP code?
I've tried PHPDoctor, which looks nice, but it has some missing features. However it is probably closest to be replacement from all solutions I've tried so far.
I've also tried PhpDocGen, but it just bails out with some Perl errors, which I'm too lazy to study.
Doxygen also does not seem to play well with our sources.
PS: The documentation would be for phpMyAdmin, a little bit outdated documentation is here.
Update: The code contains lot of functions and not only objects, what is the biggest problem with Doxygen, because it fails to organize them.
We switched from phpDocumentor to Doxygen because the former did not support PHP 5.3 features like namespaces and closures. phpDocumentor's last update was in 2008.
The major #-attributes are the same in Doxygen, because they both do JavaDoc-style comments. It is quite configurable, too.
You can also try DocBlox; it is a new kid on the block (pun intended) but already supports many of the features that phpDocumentor provides and many of the newer things (like namespaces).
Please try it out at https://www.github.com/mvriel/Docblox
Doxygen is pretty complex, and it's supposed to support PHP 5.3 features. However, it didn't work exactly how I wanted it and I realized it's a big program, but not designed for PHP; so I needed something lighter, simpler and preferably PHP exclusive, because let's face it, PHP does not have quite the common programming language syntax and look.
I know you mentioned it, but I chose PHPDoctor in the end. It does indeed lack some features, but it's simple and fast, exactly what I was looking for.
Let us know what you chose and what were the reasons.
Also something to mention is that I found the PHPDoctor interface to be quite simple and easy to style (with few CSS changes). I was able to create quite a clean page for my docs.
I've been using Natural Docs, works well for me
Aside from the PEAR repository, which I find often has quite messy code with a lot of it using old or deprecated methods and techniques, I was wondering if there was a great place to find simple (and not so simple) PHP examples of some generic functions and good pieces that people have written.
A good example would be the PEAR spreadsheet module I used a while back. The thing worked but it was written quite messily and if I remember correctly, in PHP 4.
I'd like to find something with well written and well documented code that I can refer to and see exactly how people are doing things and why they are doing things that way.
The PHP manual's as good a place to start as any, I've particularly found some of the comments on there helpful.
There's also The PHP Resource Index, which is mostly a jumping off point to other projects' websites.
Don't forget to look at questions tagged PHP on stackoverflow.
Have a look at PHP Classes. They have a large selection of classes, all with user ratings.
There's also Zend Framework spesific ZF Snippets site
Github is another good resource, more often than not it is polished code that is updated frequently.
Getting classes from other people is all well and good but keep upgrade-ability in mind, if i'm using someone else's class and need to tweek it i always extend from it.
I have inherited an old crusty PHP application, and I'd like to refactor it into something a little nicer to deal with, but in a gradual manner. In perl's CPAN, there is a series of classes around Class::DBI that allow you to use database rows as the basis for objects in your code, with the library generating accessor methods etc as appropriate, but also allowing you to add additional methods.
Does anyone know of something like this for PHP? Especially something that doesn't require wholesale adoption of a "framework"... bonus points if it works in PHP4 too, but to be honest, I'd love to have another reason to ditch that. :-)
It's now defunct but phpdbi is possibly worth a look. If you're willing to let go of some of your caveats (the framework one), I've found that Doctrine is a pretty neat way of accessing DBs in PHP. Worth investigating anyway.
Class::DBI is an ORM (Object Relational Mapper) for perl. Searching for "PHP ORM" on google gives some good results, including Doctrin, which I've had good luck with. I'd start there and work your way up.
I'm trying to get more feedback on my own projects, so I'll suggest my take on ORM: ORMer
Usage examples are here
You can phase it in, it doesn't require you to adopt MVC, and it requires very little setup.
The right thing to is to access the database via an abstraction layer in a way such if you change your RDBMS or how you implemented that access, you only have to modify this layer while all the rest of your application remains untouched.
To do this, to free your application from knowing how to deal with the database, your abstraction layer for DB access must be implemented by a framework such as ADODB.
All the files related to this layer must be located in a sub directory:
/ado
In this directories you'll put all of your .php.inc files which contains general methods to access the database.
How about MDB2 from pear?
It provides a common API for all
supported RDBMS. The main difference
to most other DB abstraction packages
is that MDB2 goes much further to
ensure portability.
Btw: #GaryF what are those strange title attributes your links have ? Did you add them or are they added by SO ?