There are two things that seem to be popular nowadays and I was wondering what are the pros and cons of using something like this: http://codeigniter.com/user_guide/database/active_record.html ?
Another thing is ORM (Doctrine for instance). What are the benefits of using these?
ActiveRecord is a pattern common in ORMs. Doctrine is an ORM which uses an ActiveRecord'ish style.
Some benefits of using tools like Doctrine:
Database independence: The code should be easy to port to different DBs. For example, I often test using SQLite and use MySQL or Postgre in production with no changes in code.
They reduce the amount of code you have to write: A large part of application code deals with communicating with the database. An ORM takes care of most of that, so you can concentrate on writing the actual app.
Of course, they don't come without disadvantages:
Doctrine is heavy so it is slower than using straight SQL
ORMs can be complex, adding some weight to what you have to learn, and they can sometimes be difficult to understand for inexperienced programmers
You can take a look at these questions though they're not exactly PHP specific:
Are there good reasons not to use an ORM?
Using an ORM or plain SQL?
I tried to keep it light-weight and understandable. Even comes with it's own Mootools based Class Generator :)
http://www.schizofreend.nl/Pork.dbObject/
check it out :)
Related
I am re-factoring a codeigniter project that uses database extensively (right now that layer uses PDO and generated queries but it became unreadable so needs re-factoring), and trying to figure out what's the best way to go. I am interested in ease of development, but more importantly - performance, but I couldn't find out useful comparisons of performance:
CI's Active record, NotORM and ORMs (currently I am looking at GAS and Datamapper, but open to other suggestions) that can be integrated with CI .
I started looking at DataMapper, but then found a post claiming it is twice as slow as the CI Active Record, and that seems to me like a deal-breaker - I am ok with a bit of overhead for extra flexibility, code reuse and readability, but would rather with a really fast bad code than find out I significantly slowed my pages loading time for that.
I am looking for something like http://www.techempower.com/benchmarks/ , but for ORMs and other DB access layers and not PHP frameworks.
Actually there's no good answer here.
If you need extremely good performance, then use PDO. You write plain SQL queries, so you have 100% control.
If you want to introduce some tool to ease the way you write SQL, maybe you can have a look at any SQL-fluent-api library, that can abstract you "a bit":
select('X')->from('Y')->where('Z')->limit(10);
A bit clearer, maybe :). It'll probably also generate compatible-queries with many RDBMS (MySQL, Oracle, PostgreSQL...).
None of the above alternatives is an ORM. If you need it, of course there's a penalty on performance (and we can say it's always "big"). Good and modern ORMs usually allow you also to cache results, or even the generated SQL to avoid part of the overhead.
Anyway, the performance is degraded, of course. For each query, the ORM has to transform the resultset to your objects (and all the relations), which is (on the other hand) very cool :D. And you lose control over what the ORM is doing internally (sometimes, and if you don't know the ORM).
There's no good answer here, it depends on your use-case.
If you decide to use an ORM, have a look at Doctrine2. You can have a look at how to integrate it with CodeIgniter here: http://doctrine-orm.readthedocs.org/en/latest/cookbook/integrating-with-codeigniter.html
I am in the process of picking a PHP framework for a web application I am starting. I have never really used a framework in the past but with this project there is a great need.
I have been debating between the usual suspects; CakePHP, Zend Framework and Symfony. I have been going back and forth about which framework will work best for me and this project. I am leaning towards CakePHP but I am still researching.
My question is not what framework is best. I know there is no real answer to that and there are tons of posts related to this subject. My question is related to the Model and ORM. I have read a lot about ORMs being slow and I am concerned about speed. I am very comfortable writing SQL and in the past have tried to keep all of my database interactions in stored procedures.
I am looking for some feedback about using CakePHP's ORM or Doctrine with Zend or Symfony as apposed to keeping everything in stored procedures. I know stored procedures are going to be faster but what else will I loose if I do not use an ORM? I understand that an ORM will give me database abstraction but in my mind that just helps people who do not write SQL. I also know that I do not know enough about ORMs.
If anyone can give me some feedback about this and which framework might be best based on using or not using an ORM.
Thanks for any help in advance.
0) Bang for effort
The key advantage with an ORM is that you don't spend as much time dealing with the persistence layer. A pre-written ORM ( I have worked with EclipseLink) will provide a ton of things you probably won't get in custom written stored procs. I think it's worth thinking about how much time you want to spend writing your persistence layer.
1) Caching
All the major ORMs provide multi-level distributed caches. Combined with Named/Predefined queries you can get SQL queries that don't actually have to go to the database. This can give you excellent performance.
2) Abstraction
ORMs allow you to define your table layout in one location and then they manage all the painful mapping between columns/tables and objects. Some will allow you to remap column, table and schema names without changing any code at all. If you work with people who like to change things around this can really simplify things.
3) Speed
Some ORMs can have bad performance, but it really is based on how you use it. I find that you tend to end up over-querying for things. On the other hand, you get things like built-in query profilers. You can write custom SQL for queries if you find you aren't getting the performance you need.
Mark Robinson gives a great response. I'm just going to back up what he says by giving our experience with Doctrine2.
I chose to use Doctrine2 as our ORM with Zend Framework a little while back. Our project is still being developed, but choosing D2 has been a decision we've not regretted one bit. Whilst you still need to give a lot of thought to your data architecture, D2 gives you the flexibility to be able to modify that model at a later date if needs be. It allowed us to try things out quickly in the early stages and the room to grow and change later when we decided that things weren't quite right - it happens.
In relation to Mark's point about abstraction. One of the other things I love about D2 is that we're working with plain old PHP objects. Don't underestimate the power of being able to think in terms of objects - both for the people responsible for modelling the data and the developers who work with the data - it'll make your life easier, trust me. Also, having inline documentation of the ORM mapping (if you choose the docblock approach) is nice.
Right, performance. As Mark says, there are ways and means to speed things up - but there's always going to be some overhead. Whenever you introduce another software layer, there'll be some performance hit, but it's a tradeoff. For us, the tradeoff - the advantages of using the ORM vs performance - is worth it. We'd spend more time debugging code and not getting things done without the ORM.
Anyway, D2 can help you with caching for queries, results and metadata. Whilst you probably just want an array cache during development, it's great that the facility for things like APC, memcache etc. is there when you go to test and deploy. You could even develop your own if you're brave.
http://www.doctrine-project.org/docs/orm/2.0/en/reference/caching.html
Hope that helps, I've probably missed stuff, but if you have any questions just fire them in and I'll do my best.
A framework implements mainly three kinds of features :
the flow between "getting a request" and "rendering a page". That's were you put things like MVC, router, etc...
the way to manage your model and it's persistence. That's where you see acronyms like ORM, DBAL, DAO
Components. Features, often working also standalone. Like Xml parsing, i18n handling, pdf generation...
When you choose your framework, it in facts means that you choose 1). It's the thing you will certainly have to stick with, it's the flow of your application. 2) and 3) ? You can integrate those you prefer. As an example, i'm on Zend Framework with most of it's components, but use Doctrine ORM 2 and Symfony's Dependency injection. A friend of mine is on Symfony 2, uses Doctrine ORM too, but does it's pdf generation and mail management with Zend's related components.
The other thing you need to know if that currently there is a "second generation" of php frameworks/orm's, (re)written to take advantage of the new php 5.3 features, and/or to solve the general performance/coupling issues they (nearly) all had. Some of them are production ready, some are still under development :
Doctrine ORM 2 (production ready)
Symfony 2 (production ready)
CakePhp 2 (in RC 2 currently, but by the time your project is ready it should be stable)
Zend Framework 2 (still under active development, but normally not for so long)
FLOW 3 (beta2, should be ready soon too)
For the ORM part, i'll recommend using one, especially Doctrine's. #Mark and #iainp999 explained perfectly why.
ORMs are for programmers who don't "grock" SQL!
Yes ORMs make it easier (or at least require less lines of code) to write simple CRUD stuff, but when you get to more complex requirements its like trying to write SQL with a piece of wet spaghetti from ten feet away.
So stick with SQL.
Its worth looking at something like "SQLMap" which is ORM starting from the "R"elational side of the mapping (most try to map an "O"bject on to a table). This will allow you to write the SQL yourself and generate the appropriate "helper" classes to easily access the results in your program.
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/
After working a while with Codeigniter, I am totally in love with the ActiveRecord way of writing mySQL queries. Now, when working on another non-codeigniter project I've been recommended to use an ORM to handle the DB mapping which sounds great, but after looking into Propel & Doctrine, I have become quite afraid of the additional configuration files containing the database structure, more or less.
Why does these ORM:s define the database structure and what advantage has it over an non-defined ORM like the one that's bundled with codeIgniter?
FYI, there was a thread about this a while ago. The gist of it is that ORMs (ActiveRecord is an common ORM pattern) help enable database independence and quicker access to common queries. Downside is that ORMs are "heavier" than straight SQL. For most apps, 90% of your data access is pretty standardized so ORMs help with time to market.
ORM and Active Record Pattern in PHP?
I'm interested in learning more about design practices in PHP for Database Abstraction & Factory methods. For background, my site is a common-interest social networking community currently in beta mode.
Currently, I've started moving my old code for object retrieval to factory methods. However, I do feel like I'm limiting myself by keeping a lot of SQL table names and structure separated in each function/method.
Questions:
Is there a reason to use PDO (or similar) if I dont anticipate
switching databases?
Can PDO interface with the MySqli prepared statements I
currently use?
Will it help me separate table names from each method? (If no, what
other design patterns might I want
to research?)
Will it slow down my site once I have a significantly large member base?
Nettuts recently had an article on some of the benefits to using PDO.
As for your question regarding abstraction, you might look into Object Relational Mapping (ORM) which lets you access database records the same way you would native php objects. Most of my experience has been with the Kohana framework, and I like how their ORM works, but many of the popular php frameworks should have good examples of database abstraction methods. I find that reading where someone else did it the "right" way helps me out.
I would like to second Rookwood on looking at ORM. I have had good experciences with Doctrine. Building something yourself would be a waste of your time in moste cases.
I would recommend using PEAR or PDO.. why?
1) It's tested and used by tens of thousands of programmers. If you need another programmer to help you, there is a good chance they know PDO. There is basically zero chance they will know your API.
2) You don't think you will change databases.... BUT, what if a version change or something like that happens that forces you to migrate?