Ruby style relational tables in PHP - php

I know ruby (on rails) uses lots of "magic", but I use PHP, is there not a way of implementing the rails-like:
class Player < ActiveRecord::Base
has_many :cards
end
in PHP it would make so many peoples lives so much easier. Are there perhaps frameworks/ORM's that provide similar functionality etc?

The term you're looking for is an "Object Relational Mapper" (ORM). The one you cite is a component of the Rails framework, called ActiveRecord.
PHP ActiveRecord is one project that attempts to provide this, though the last release was in July 2010. I haven't actually used it; I just switched to Rails. :)

in CakePHP there are relationships such as hasMany, belongsTo, hasOne, and hasAndBelongsToMany

PHP doesn't have a ORM by itself, but there are several fairly usable ORM projects for it; I'm acquainted with Propel, and it's rather good (used e.g. in the Symfony framework, which resembles Ruby somewhat).

Related

There is a php ORM which is similar to Rails ActiveRecord?

I'm designing an e-commerce database for a school project that require usage of cakePHP.
The ORM « attempt » of cake (yeah, it's not really an ORM) doesn't fit my needs.
I tried doctrine 2, I'm not convinced by the complex DSL and some crazy behaviors.
What I'm searching is an ORM that make associations effort-less and use conventions like we can do it with Rails ActiveRecord.
Because of complexity of an e-commerce database, I can't test everything and advices on doing that in PHP will be appreciated
Just wanted to share our experience with PHP ActiveRecord.
We are using it in production on a commercial web app for 2 years. We chose it for its unmatched simplicity and because it uses PHP5.3 features to provide nice RoR-like methods.
For 2 years we never had any significant issues with the library. We update it from time to time to keep up to date and pretty happy with it.
PHP ActiveRecord won't suite everybody since, well, it's ActiveRecord, not true ORM. But as you are specifically looking for RoR-like ActiveRecord solution in PHP, look no further.
Laravel's Eloquent ORM is nice, it can take a little bit of work to get it standalone but it's worth it.
You may wish to search for composer ActiveRecord packages, there are dozens of them:
One of them which says it was inspired by RoR: https://github.com/jpfuentes2/php-activerecord
There are dozens of them: https://packagist.org/search/?q=activerecord
Or even try ruby on rails in your search https://packagist.org/search/?q=activerecord%20ruby%20on%20rails
Try at least a bit of your own research before asking questions, as well as showing what you have found and tried.

PHP ORM frameworks with features similar to ADO.NET Entity Framework Code First?

I'm coming from .NET world back to PHP for some side projects. I am comfortable with PHP as a language, but am kind of lost in many PHP frameworks available today. Back in the days I did PHP we just wrote SQL queries, so I have no idea what is possible with PHP today in terms of ORM, therefore the question.
I got used to creating my database models using ADO.NET Entity Framework Code First and I like this approach, so I am looking for a PHP ORM framework with similar set of features.
If I understand you correctly, you are searching a framework with something like AR and CRUD.
I think almost every more famous PHP framework have this options.
Anyway, I am using Yii framework and I can say it's one of the best options, but you can surf a little to see which framework can fit your requirements.
You can create DB with table relations etc, and the generator will create your models + relations for the Active Record. Also CRUD generator can create and the View/Controller part.
You should review current PHP ORM frameworks, you may find something that they offer that the ADO.Net framework does not.
What is the easiest to use ORM framework for PHP?
This stack question is very detailed around your question: Good PHP ORM Library?.
Everyone has an opinion, some better than others.
have a look at Doctrine or Propel ORM

Which PHP ORM works best with Zend Framework?

Well, seeing as I'm dissatisfied with Zend_Db_Table after being spoiled by LINQ, I'm looking to get started learning an ORM with PHP. General consensus seems to be that Doctrine and Propel are the only good ones for serious use -- and whatever my opinion, I'd like to use something at least moderately popular so that people in the future can look at this app I'm working on without having an head explosion :P
I'm currently leaning towards Propel because it's documentation seems to be a bit more complete, and it supports the nested set model (also called "modified preorder tree transversal model") right out of the box. However, I like Doctrine's use of namespaces and other PHP 5.3 features, and it seems to be a bit more popular.
From those who have used either ORM with Zend Framework, which meshes better with the existing framework (if either)? What kind of issues should I watch out for using either framework with Zend?
I can't speak for Propel but there are a lot of good integration resources for ZF and Doctrine. See Zend Framework 1.11 with Doctrine 2 Integration
Well Doctrine 1.2 supports Nested Set... much better than propel did in < 1.3. I havent used propel 1.4/5 so i cant comment on its current state. I also havent used Doctrine 2... ive been sticking with 1.2 since mot of my projects dont use php 5.3...
Overall I liked Propel better... I prefer generated classes to ones configured dynamically at runtime. Also keep i mind Propel is an active record implementation while Doctrine 2 uses the Data Mapper pattern.

ORM in Django vs. PHP Doctrine

I am a PHP guy. In PHP I mainly use Doctrine ORM to deal with database issues. I am considering move to Python + Django recently. I know Python but don't have experience with Django. Can anyone who has good knowledge of both Doctrine and ORM in Django give me a comparison of features of these two ORM implementations?
I am a rare person who had to switch from Django 1.4 to Symfony 2.1 so I had to use Doctrine 2 instead of current Django ORM.
Maybe Doctrine can do many things but let me tell you that it is a nightmare for me to use it coming from Django.
I'm bored with the verbosity of php/Symfony/Doctrine ...
Also I never needed something that Django's ORM didn't manage already (maybe projects not big enough to reach the limits).
Simply compare the description of data between both orms (including setters & getters)...
The Django ORM does everything I want it to do. I've found it to be easier to use and more straight-forward than Doctrine. Sorry that I can't help with an in-depth comparison.
If you find the Django ORM is missing a feature you'd like to have, check out SQL Alchemy. It can easily be used with Django.
Ive used Doctrine over a 2 year project that ended 1.5 years ago, since then i've been doing mostly Django.
I prefer Djangos ORM over Doctrine any day, more features, more consistency, faster and shinier.
Django isn't just an orm. It is a web framework like symfony. The form framework in symfony is modeled on django for example. It's orm part is more like doctrine 2 I think, but I haven't played with either much.

Zend Framework: How to construct a simple "Data Mapper" model?

I'm building an application in the Zend Framework, but I'd like to implement a "Data Mapper" style ORM layer, constructing Model classes that only include the specific pieces of data they need to represent the domain concept (regardless of which tables those fields happen to belong to). Since my DB structure is highly normalized, the benefits gained from an Active Record ORM would be superficial.
So how would you implement a very simple, straightforward data mapper ORM layer in the Zend Framework?
I believe there's no true "Data Mapper" style ORM for PHP yet. If you want a true "Data Mapper", I think you might be out of luck.
There are 2 popular ORM in PHP, Propel and Doctrine. They are both more on the ActiveRecord side.
Doctrine in my own opinion is the go. At the moment, it's stable release 1.2 is not integrated with ZF yet. e.g. you cannot use the doctrine command line script to generate model classes for a modular ZF application setup.
However if you are running a single module ZF application, doctrine can be integrated pretty well. As #ArneRie pointed out, ZendCasts has got some really good videos. I learned a lot from it.
If you are interested, I also derived from it and made one of my own approach. You can find my blog post about this topic at http://blog.elinkmedia.net.au/2009/12/03/zf-doctrine-and-unit-tests/. You can download the source code of my sample app from github too.
Doctrine 2, which is currently in alpha (beta due 1 March 2010), is a data mapper orm inspired by JPA/Hibernate. Zend Framework has shelved their own Zend Entity component in favour of integrating Doctrine 2 with Zend Framework.
Depending on your timescales, you might want to look at Doctrine 2.
i would suggest to use Doctrine with the Zend Framework if you want to use an ORM. There are good Tutorials and Screencasts around with a lot of information.
Doctrine 1.2 with Zend Framework Screencast
The implementation details for creating an ORM can be quite extensive, and I lack the experience to give any meaningful recommendations beyond suggesting already existing projects, so I will suggest one instead.
The Data Mapper pattern is not very popular in the PHP world (in favour of Active Record, and certainly much of that favour is attributed to the fawning of Ruby on Rails in the PHP framework community), although there exists one notable project -- and it looks promising.
Outlet ORM follows the Data Mapper pattern, although I am not sure if it would be sufficient for your needs. I have so far had no problems with it.

Categories