PHP Datamapper And ORM - php

I am starting with PHP DATAMAPPER AND ORM.. I am completely new with it. Though I got knowledge about through google. It really found interesting.
I have few doubts with it.
Can I use Doctrine 2 library/ORM with my Core PHP Project. Or it is only for framework like CodeIgnitor, Zend or else.
Is it really helpful for fast development. Or it may get expanded after some extent.
Can I have any example/ esp. tutorial where I can implement it with CORE PROJECT means some blog or else.
I checked with phpdatamapper.com. I found it easy but it was quite old as not updated since last 3 years. Is it good to implement that ? Any source implement that.
I would really be pleased having source/implementation (simply on blog) on github or else somewhere. Please

ORM like doctrine can be used with every project, it does not need any framework. You can include them in your projects with composer.
About Doctrine, you can use two things depending on what you want to do :
Doctrine\ORM and its whole bundle. It brings you the whole functionality, with its ORM and other cool stufs. If you want to use it, you have to put that line on your composer.json file :
{"require":
{"doctrine/orm": "2.3.2"}
}
Doctrine\DBAL. It is only a abstraction layer without the ORM functionality. It is here to bring you more functionalities to PDO, and build queries without taking care of your data base engine. As it has no ORM it is faster to execute but it does not bring any conception facility.
If I introduce you to these two tools it is because you will have a choice to make. If you want a simple blog site, Doctrine\DBAL is what you want as it will bring no performance issue.
If you want a bigger application with a full architecture, you need Doctrine\ORM as it will help you to build and keep your data base safe.
About tutorials, the documentation is built like one so I think you can get started here for the ORM and here for Doctrine\DBAL

Related

Scalable app design in php

I'm a beginner php developer who is trying to build a social network for my school students. Knowing that the school has over 1000 students who are already active, I must have a plan of expanding / scale the code that I write.
Earlier it was just the LAMP Stack, now the modern web development is way more than that as I see, I'm truly kind of lost in what technologies to use and how to incorporate them to build a scalable app. I'm hoping to divide this application into 3 layers.
Application layer (phalcon,reddis,apache,php)[mvc api centric]
Database layer(mysql)
UI layer - (html/css/js/)
This is where i need help, is this design approach good for a scalable app ? where can i improve ? any explanations, links for further reading will be a highly appreciated.
Welcome to SO. I cannot think of a particular reference guide to direct you to (although the PHP manual is a good place if you end up stuck with how to do something specific). I would suggest reading a bit of several results when you search "Getting started with MVC in PHP" and noting what they agree on. That said, take a look at what I say below (and then ignore it as much as you please ;) ).
Firstly, you are wiser than many in sorting out a scalable design before launching into the project...
I'm excited to see Phalcon in your list there already. However, as DevDonkey suggested, start with something simpler first (Phalcon is very powerful but to really get to grips with it you need a good grasp of PHP, particularly object-orientated programming).
If you are completely new to PHP...
... try building a small app (products table, view/add/edit/delete functionality) and learn the beginnings of the language that way, as this answer suggests. Things will go wrong and you'll discover lots of headaches when you want to change one feature and it affects everything else but that will help you to understand the importance of...
MVC design
From your question I can see you have at least heard of this. This is really where the layers of your application lie:
Model - interactions with the database (retrieving/editing data) are handled through this. So you could have a MYSQL database and then your models provide a nice interface to interact with the data (generally you have one model for each table).
View - this is the last layer, what the user sees. So you will make use of your html/css/js knowledge here. On this topic, unless you really want to do your own css consider using a CSS Framework such as Bootstrap. It will really help speed up making your site look good and there are loads of free templates out there to use with it.
Controller - this is the application logic. The controllers request/manipulate data through the models and then decide what to send to the views for rendering.
Use a framework?
Using a good framework can make your application more reliable and quicker to build. But using a framework without understanding it will be frustrating, slow and possibly result in worse code than if you didn't use one to begin with (as you employ hacks to get around the pieces of the framework you don't understand). My current favourite is Phalcon but as a relative beginner to PHP I would suggest something more like CakePHP although both Laravel and Symfony are also popular.
Summary
Start small, learn, test ideas out and then build up to a bigger project.
Get comfortable using PHP (including OOP style) before using a framework.
Use an MVC framework
The layers you laid out in your question are good, but I would split it slightly differently (considering that MVC is the 3 layers)
Application Layer - controllers, written in PHP, handles logic/manipulation, often the biggest layer
Database Layer - models, written in PHP, you will also need a database which could be in your favourite database language - MySQL ;)
UI Layer - views, possibly written in PHP (depending on the framework) but also HTML, CSS and JS as well as well as a templating language if you wish (e.g. Twig or Volt), essentially a way to make the response from the controller nice for a human
First Project (for CakePHP)
This blog tutorial is a good place to start if you decide to use CakePHP.
Getting started with Phalcon
Phalcon is more powerful/verstile, but to get started with it I feel you have to be a better PHP developer than you do to get started with something like CakePHP. Take your time to understand each new concept with Phalcon, particularly Dependency Injection.
Even having used CakePHP for the past 2 years and being familiar with MVC patterns and PHP, I still worked my way through all 7 of the tutorials in Phalcon.
Having said this, my favourite thing about Phalcon is that it is highly decoupled - so it is fairly easy (after a while) to replace bits of it with your own extensions if it doesn't quite do what you want.
Note about Phalcon: It is not as popular as many other frameworks (although popularity is growing) and so you may have to spend some time digging around when you get stuck. However, the docs are improving all the time and the forum is very active. Unfortunately the number answering questions about it on StackOverflow is still small compared to many other frameworks.

PHP Framework and ORM vs Stored Procedures

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.

Joomla like component architecture.. Starting from scratch

Blockquote
Edited:
http://blog.fedecarg.com/2008/06/28/a-modular-approach-to-web-development/
The above approach is what I'm looking for. So are there php frameworks available which will allow me to create modular structure like above for my code ?
Edited:
Blockquote
I would like to know which is the current best php framework which is 100% modular like joomla component architecture.
In joomla all you have to do is upload a set of files under the "components" directory and you can add "any" kind of complex functionality to joomla.
I want to develop php applications from scratch and I want all of my applications to have the same ease of joomla's component architecture. So Im currently looking for a php framework.
So which is the best way to go about if I want to do the above ?
Should I choose a php framework like codeignitor or zend etc ? but you see, even if is use them, Suppose if I have to create a new function I have to upload files to atleast 3 directories i.e controller, model and view. But you see in joomla all I have to do is upload all the files of course suitably structured into just a single directory called "components" and the rest is taken care of automatically
So what is the best way to go forward ?
Shanthi
Joomla is a CMS system AFAIK so you cant really compare a PHP framework to it.
In terms of modularity Zend Framework has an excellent excellent, simple to implement module structure.
You sat that to add complex functionality to joomla you upload files in the correct structure and joomla does it for you, well as long as you create your module files in the right structure for ZF it will work too......
Your question is same as my question. but it was for 4 months ago.
I tried to find out how can i create such modular system. After some searching I understood that we should have a single directory like 'components' as you said, and put every data of modules in it. but how we should use them ?. Easy !. just create an handler (php file) that control your installation, uninstalling, positioning, settings. In fact i just solve 3 of them. positioning is so hard i think so, i never do it till now.
if you want an example of my explanation, just tell me to create it. I'll send it for you. :)
well i think you are comparing apples to exotic devil fruits or something.
you are adding modules to a cms and exepct the same from a famework.
it doesnt work like this.
the mvc structure you are talking about (model - view - controller) makes a lot of sense and helps you to keep your code organised and to keep up good class coherency.
its a design pattern which has evolved to a de facto standard and has been adopted by almost every framework.
zend framework is probably the best way to go for you.
its layouting engine and stuff is not as fast as one could wish, but it features execellent modularity. you can even partly overload core classes to suit your needs in your "module".
only abstract classes dont work (how should they?), thats why they are almonst never used.
if you really really wanna go with a very flexible excellent system and performance is not the most important point for you, you should get going with magento, which takes the modularity and flexibility even to a higher level by adding an xml structure to zend framework that allows you to organice all your pages, manage your dependencies, rewrite your model componenents etc.
but be a aware that the more you want the more performance you will loose. my personal fav from the frameworks out there is codeigniter, but i designed my own for my needs...
I agree that there is a difference as Joomla et al as CMS's and therefore come in at a higher level of functionality than a framework, but of course with less flexibility.
However you might want to explore Symfony 2 and Lithium (Cake 3?). Both are designed to only use PHP 5.3 and greater and both use the namespace functionality in PHP. I have done less with Lithium but certainly in Symfony 2 everything (well nearly everything is a plugin making the framework modular and much easier to manage.
However you will never be able to compare the two Framework/CMS in exactly the same way as they are both built and designed for different purposes and with varying constraints.
N.B. Neither Symfony 2 or Lithium are production ready at present. Symfony is aiming at March 2011, not clear on Lithium's 1 dot oh date
[edit]
A number of answers here plump for Zend. What needs to be clear is that there are two types of framework (yes ok lot's and lot's) but being simplistic there are Full Stack and Glue.
Glue frameworks like Zend tend to be free and easy allowing you to use some parts and not others as you wish. So although nominally MVC you can easily implement Zend with V and C but not use M. You can write all your database calls manually in the controller etc.
Full Stack such as Symfony mean you pretty well use all of whether you like it or not. On the data side you use and ORM, Doctrine or Propel and a large amount of the framework is controlled from configuration files in YAML/XML. In this respect Symfony is very like Ruby et al. Please note this is not a recommendation or criticism of either.
Joomla in my opinion is gluey (but not a framework) but hey why use something that is 90% built and then rewrite 75% of it? Oh yes because the customer asks and your boss is unaware of the problems, i remember now.

Php framework to slowly refactor an inherited site

I've recently inherited a medium-sized php site which is horribly coded. It violates every best-practices methodology, from MVC to DRY, is vulnerable to SQL-injection and everything in between.
I've visited the other questions and already put everything on a VCS and am considering the framework alternatives. However I'd like your opinions on a framework that lets me slowly migrate from the actual site to a framework controlled one.
Thanks.
Zend Framework would actually be the best choice in my opinion, as it has a great use-at-will structure and it is no full-stack framework like most of the others.
That means that you can start with migrating the model-layer first without having to touch the view or controller part. And even when it comes to the controller part, you could first put everything into controllers without having to rely on the router, so you could still use your old URLs.
I will put my vote in for CakePHP (http://www.cakephp.org). It has the ability to manage everything very nicely.
Template
This will allow you to create the base template / layout for the site. It is the main body of the site. You can store multiple layouts all in the views/layouts directory. You can identify what layout you want to use for any given page within the site.
Static Content
If you have static content pages, they all reside in views/pages. These will load into the layout wherever you put the <?php echo $content_for_layout; ?>.
Custom Code
Many times, you will have custom code that may not fit in the framework. No worries, you can add this to the libs or vendors folders and call the functionality from there.
Speedy Upgrade Via Bake
One of the cool features of cake is the bake feature. Once you have added your schema to the database, you can use bake to have CakePHP write all of the models (with relationships), the controllers (with basic CRUD and admin sections), and the views for each action within the controller.
Cake has been a great fit for all of the projects I have worked on. It keeps the code well organized, has a very active community, and their documentation is very well written and understandable.
UPDATE: For additional information about some sites who use cakephp you can see a sample list here: http://book.cakephp.org/view/510/Sites-in-the-wild
A few notable (high traffic sites) would be:
https://addons.mozilla.org
http://scratch.mit.edu/
Kohana is my framework of choice, but I won't start to wax about its good points, I'm sure it can do anything the others can do.
Faced with the same legacy codebase problem as you described, my response was to take Kohana and disable the request routing, so that you can just use it as an include on a page-by-page basis until you're ready.
The changes are minimal; if you're interested the fork of kohana is up on github
You may need to adjust the php error level settings depending on the kludgey-ness of your codebase ;)
Zend Framework is awesome.
Even better, this excellent post from Chris Abernethy shows how to gradually migrate an existing site from a twisted plate of pasta into a nice MVC structure using ZF.
Take a look at Fat-Free Framework. It allows both procedural and OOP code, so you can have a two-stage approach. If the base code is currently procedural, then you can focus all efforts first on transformation to MVC architecture. That way you can have a proof of concept that all future efforts can be as fruitful as the first phase. Then you can move too strictly-OOP. No other framework will give you this kind of flexibility. And your end-users will not feel any delays, or worse, a culture shock.
You will get as many answers as framework users there are in this place. It's obvious that someone using framework of his choice will advice it to the others.
I opt for symfony.
However, if you care about best practices than both symfony and Zend are a good (and only) choice.
This sounds like a massive undertaking.
I can only recommend CakePHP because that is what I use, but that does not mean another framework would be less or more suitable. They're all pretty much the same when it boils down to it.
Choose on whatever criteria you wish, such as public support/user base (Cake's is massive), feel, name, colours on the webpage, whatever. But my advice is to stick with that choice and ride the learning curve.
All the frameworks mentioned are good, but you will need to rewrite loads of things like the queries. You might not want to use an ORM like Doctrine or Eloquent. Just stick with something like Active Records. Codeigniter, CakePHP and Yii will do just fine.
but it is not going to be an easy task. be warned!

PHP framework of intermediate complexity, in between CodeIgniter and Yii?

Something easy like CI (this means mandatory good, easy, up to date documentation). But also with some more features than CI.
Yii has lots of features, but it is also more complex (and it kind of forces you to have to use lots of it features). That means adding some functionality to your web-app takes three times as long because you have to figure out lots of new small functionalities of Yii.
It's kind of like the CI "gets out of your way" when it needs to, and Yii gets in your way, and if you don't do it its way, it breaks.
Features missing in CI that would be nice to have in this new "intermediate" PHP framework:
Code generation (crud).
Authentication.
Access control.
Layouts.
Widgets.
Easyer / automated pagination (like yii)
easy uri parameters
Where Yii causes me problems:
It's like for every small task there is some inbuilt functionality (this is good), but, YOU HAVE to use the inbuilt functionality, otherwise bad things happen. (CI gets out of your way, but does it too much, Yii helps a lot, but is butting in too much at times, and it forces you to sift through its documentation so that you discover these functions without which you are not able to accomplish a task that would take four time less, in CI, or in a non framework app).
Is there something in between ?
(ASP.NET MVC could be 'it', but I don't know the language, so the effort to learn it would be greater than learning Yii php framework really well, so I am looking for a PHP Framework)
I am a fan of CakePHP. I feel it has the specs you provided. If you want something more cutting edge you can take a look at Lithium
I have found some resources that kindof solve the problem, because they contain examples (Milan Babuškov's sugestion helped focus on "the solution").
Yii playground - examples
Yii cookbook - examples
Yii blog tutorial - more examples
PS. there is also google - I find solution (and examples) the fastest this way - ex: implement + pagination + yii
I've used both CI and Yii (on my own projects if that makes any difference). CI was my first introduction to MVC, and I found it easy going because it let me use any crappy structure and code. I wrote two full sites in CI (www.insolvencynews.com and www.thebigeat.com if you want to see complexity.)
I had a look at CakePHP but got NOWHERE.
Then I moved on to Yii and, like you, I found it pretty tough and rigid. But I then found that it was so powerful and extensible that I was so much more efficient. When I needed to add a few new features to the old CI sites, it was faster to rewrite the entire sites on Yii than to code up the extra features in CI.
I can't recommend a framework in the middle, but I can recommend sticking with Yii. When you say Yii gets in the way, can you give an example? Looking at DB stuff (in ascending order of dependence on Yii):
you can code using PHP's core MySQL functions.
$result = mysql_query($sql);
you can use Yii's DB abstraction layer.
Yii::app()->db->createCommand($sql)->queryAll();
You can use Yii's ActiveRecord:
Takeaway::model()->findAll();
you could try kohana (especially coming from ci)
You should check out the CI community, some of those extensions maybe have been written by someone else (I remember seeing Authentication and Components/Widgets somewhere)
Symfony is worth checking out. I personally don't like it much because they chose Prototype over jQuery for their ajax features, which is really annoying to use when you're used to jQuery.
Lithium might be good to check out too. However, it is php 5.3 only and you need to be really careful that this version of PHP is going to be supported on the server the site will be deployed on.
See this list for good comparisson:
http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks#PHP_2
From a personal point of view, I would go with symfony because of it's
rich features and
great integration with many other already bundeled components (Doctrine, Swift Mailer,..),
good (but at first complex) code generation that produces realy useable code to get you startet quickly,
powerfull use of templating (that will be the point you mention under "layouts)
many different, powerfull plug-ins, including Authentication & Access Control (it also has a plug-in to get jQuery support)
one of the best tutorials that I've seen with a framework
Downside is a
more complex structure,
IMO wired file structure,
a rather messy API documentaion compared to the tutorial
CodeIgniter is a nice framework if you don't want to create big apps but it lacks a great database integretaion and you already mentioned code generation.
im very good in Raw PHP, where the project at hand became too much to handle i decided to move to zend, with too too much complexity i finally moved to YII which really reduced the cost and overhead time for project development and most importantly for me is the simple integration of jquery, widget and advanced-OOP.
You could have a look at Qcodo / Qcubed.
They are both easy to pick up and offer code generation / ORM
Easy way to create forms in an mvc kind of way.
For what its worth, if you're looking for a PHP Framework that is like ASP.NET MVC then I think Prado is the closest thing you will find.

Categories