Skip Navigation LinksHome > Categories > Code from a Category

In PHP 5.3 ActiveRecord appeared similar to Ruby on Rails

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1769
Add Date: 03/18/2012
This article describes how to use ActiveRecord written by Jacques Fuentes and Kien La, using the new features PHP 5.3, which is practically identical to the opportunity to ActiveRecord Ruby on Rails.Our main goal in this project - to enable the PHP-developers to create large-scale projects with greater flexibility.

I hope the use of this product will push the PHP community to further the realization of all the wonderful benefits of Ruby on Rails. Okay, enough to spin around and around, let's move on to the fun!

Overview of ActiveRecord

Let me accept the fact that we tried to preserve the similarity between our detyaschem and ActiveRecord based on Ruby on rails, to avoid the headaches and improve efficiency of the programmer. Keeping these similarities, we have tried to recreate many of the functions.
Here is a list of these functions:

  • The search methods
  • Methods of dynamic search
  • Methods of recording
  • Relations
  • Verification
  • Callbacks
  • Serialization
  • Support for different drivers
  • Other Options
  • Also, there are other possibilities as namespaces, additional drivers, the transaction (ie, what we would like early) and we will add more in the future, but first I think is not bad. We hope to launch a site with documentation and source code to place on the service source code for 2-3x week.
    Do not forget to check for updates, soon to appear.


    Installation is simple and linear. There are only two types of configurations that you can choose:

    • Installing the sample directory auto_load
    • Setting up connections to your database

    • Examples:

      Once you set these two parameters your work is finished. ActiveRecord takes care of you and takes the rest of the work itself.
      It does not need to carry out any additional work which the schemes of your yaml / xml files.
      Also requests to the database to obtain information and cache files will be no extra call for one scheme.

      The search methods

      ActiveRecord supports several methods by which you can find any records on the primary key, or build your own with a set of options, such as: sorting, limit choice group.

      Methods of dynamic search

      ActiveRecord from RoR expands use of search functions, allowing you to dynamically create a method based on attribute names. This means you can easily make a request "find_by_attribute_name" without an explicit definition of the class. We have made this possible by using the magic method in PHP 5.3: __ callStatic ().

      Methods of recording

      Which makes sense to have an object that encapsulates the record from the database, if you can not do anything about it?


      Association - the difficult part of ActiveRecord. They use the same options as in RoR.


      The name speaks for itself.
      Before the save / update / remove for each one, which you created, and will be applied only after verification of successful passage of the model will be refunded.
      Otherwise, you will see an error message and be able to go back to fix it.

      The callbacks

      The callbacks allow you to control your model before / after an event for its existence. You can choose the methods that will be called as a callback before or after other methods applied in the model.
      Unfortunately, even PHP 5.3 has a limitation - you can not we used the callback in the form of static methods, they must be dynamic.


      Support for different drivers

      Currently there is support for MySQL (via mysqli) and sqlite3. However, we hope to introduce a greater number of drivers in the project, which should help in dealing with such a PostgresSQL database and Oracle. Soedienie / adapter - designed so that there will be no difficulty in creating a larger number of drivers, when it is needed.

      Other Options

      When declaring a model you can also specify primary_key and table_name.
      Protected / accessible definitions are available so that you can cope with many common problems.
      Attributes can be aliases to help you get access to them through the different names.


      As I mentioned earlier, as soon as the code will be available on the services of source code.
      We are also working on creating a site with tutorials and documentation to the code. I will report new successes.
      Thanks for reading!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.