phptarsier-0.1.1pre-alpha release!

Release Name: phptarsier 0.1.1pre-alpha

Notes:
This release was delayed in schedule due to some projects that I have to do first. Sorry for those who have anticipated this early.

This has been one of the major milestones in phptarsier releases. There has been some major changes from the previous release. Future releases will include the implementation of phpUnit test suite, other database implementations, script and code generators and anything possible.

Enjoy this release.


Changes:
* introduction of Html class to organize Html functionalities such as text_fields, buttons, forms, etc.
* introducton of Ajax class that encapsulates the functionality of the prototype javascript library (http://prototype.conio.net)
* Effect class that implements the javascript functionality of script.aculo.us library (http://script.aculo.us)
* implementation of has_and_belongs_to_many in ActiveRecord
* implementation of belongs_to in ActiveRecord
* a change from "public static" to just "public" in the table_name() and primary_key() functions of ActiveRecord due to some implementation problems with PHP in static variables and functions
* minor bug fixes
Posted by jbeduya | 1 comment(s)

How ActiveRecord Works in phptarsier

It's Ruby on Rails, almost...

One of the great features about Ruby on Rails is its data model implementation in the form of ActiveRecord. It's really impressive the way it was implemented which really makes it promising. There has been some attempts to port that feature into PHP but I was not able to see an implementation that looks more like it.

In my attempt to create phptarsier, it was one of the things that I have to make sure it works closer to ActiveRecord in Rails.

Database Configuration

Before you run your application, you need to edit the config/config.php file for you to get started. This will setup the database connection for you and all your models will have the same connection as you instantiate them.

Data Model

Now you have setup your database connection, you need to setup your models for your database. Models are like representations of database tables. For example, if you have a table name "blogs", you would define your model like the following:

<?php
class Blog extends ActiveRecordBase {
public function table_name() {
return "blogs";
}

public function primary_key() {
return "id";
}
}
?>
As a general rule for phptarsier models, you have to name the above file as app/models/Blog.php. The file should represent the model, always keep that in mind. If we create a model, we need to extend the functionality of ActiveRecordBase class to enable our model to perform the way we want it to do. All the methods and properties in ActiveRecordBase class is now inherited by our model. You might have notice that I have implemented two methods, namely: table_name() and primary_key(). Actually, this overrides the same methods implemented in our base class. Why I suggest you to create your model like this, is to gain a little time. It is important for me, but if its not a great deal for you, you may not declare the two methods. With the above implementation, the table name and primary key are automatically returned and the object will not have to gather its information by querying the database.

Given that class, you can now perform the basic CRUD(create-retrieve-update-delete) operations. The following are example implementations.

CREATE an Entry

<?php
...
$Blog = new Blog();
$Blog->create(array(
'title' => 'My first blog',
'author' => 'jbeduya',
'intro_text' => 'An introduction to my first blog.',
'body' => 'The content of my first blog! Wow!'
));
...
?>
UPDATE an Entry
<?php
...
$Blog = new Blog();
$blog = $Blog->find(1);
$blog->title = "My First Blog's new title";
$blog->save();
...
?>
The $Blog->find(1) is then converted to some SQL statement that looks like this: SELECT * FROM blogs WHERE id=1. With the given query it returns an object that has all the fields for the blogs table. The next statement, updates the title to some other value and then updates the database with the changes. That's how easy it is.

DELETE an Entry
<?php
...
$Blog = new Blog();
$Blog->find(1)->delete();
...
?>
The above simply tells the object to query this row and delete it. That simple.



Model Relationships

We are doing fine and we can now create models for each and every table in our database. But what if we want to make some relationships our models. As with my example, a blog has comments. First we need to create our comment model that will look like the following:

<?php
class Comment extends ActiveRecordBase {
public function table_name() {
return "comments";
}

public function primary_key() {
return "id";
}
}
?>
How are we gonna make that relationship, that every blog has many comments? It is simple and it requires only a few lines of codes. We need to update our Blog model to tell it has many comments. Our new Blog model should have the following codes added:
<?php
....
class Blog extends ActiveRecordBase {
public function __construct() {
$this->has_many('comments', array(
t_class_name => 'Comment',
t_foreign_key => 'blog_id',
t_reference_key => 'id'
));
}
...
}
?>
The above code simply tells that each row of Blog has many "comments", as specified in the first parameter. The first parameter seves as the relationship to the model. The second parameter which is an array of some contants with values are additional parameters required to properly associate the given relationship. If we have to read it, blog has many comments, to determine its comments you instantiate the class named 'Comment' and check if the value for blog_id in 'Comment' is the same as my id. With that specified, you can simply perform the following query.
<?php
...
$Blog = new Blog();
$blog = $Blog->find(1);
$comments = $blog->comments->find_all();
while ($comment = $comments->each()) {
echo $comment->comment; // displays the comment itself.
}
...
?>
The "comments" property is automatically known by the class because of the relationship that we specified. The above statment simply queries the database for all the comments of the given blog.

find_by_<field(s)> and find_all_by_<field(s)>

One of the new implementations of ActiveRecordBase is the use of find_by and find_all_by clause.

<?php
...
$Blog = new Blog();
$blog = $Blog->find_by_title('How ActiveRecord Works in phptarsier');
echo $blog->author;
...
?>
That should pretty simple. It queries the blogs table using the title field. It could even take additional fields by adding more fields and separating it with an _and_.

<?php
...
$Blog = new Blog();
$blog = $Blog->find_by_title_and_author('How ActiveRecord Works in phptarsier', 'jbeduya');
echo $blog->author;
...
?>
Isn't that neat? How about the following:

<?php
...
$Blog = new Blog();
$blog = $Blog->find_all_by_author('jbeduya', array(t_condition => 'title'));
echo $blog->author;
...
?>


Phew...

A new release is full-packed with new features:

  • pjs (a phptarsier version of rjs)
  • ajax and effects
  • components
  • etc...
I am currently working for its site and sample applications. Just wait till it comes out, you can email me if you want an early copy of it. By the way I have changed its License from GPL to MIT.

Enjoy.

phptarsier - a development framework

A Development Framework

phptarsier.about

Welcome to phptarsier!

This is a quick overview on how phptarsier framework works. You will be guided through the features as well as the structure for the framework. The information presented within this guide might not be enough for you to get started. You may want to look at Ruby on Rails as a reference. I really tried to make this framework to look like Ruby on Rails.


As a developer, I really appreciate Ruby as a language and Rails as a framework. It gives you an idea that you can improve what you do everyday and I think that is what it give to its developers. As a PHP Developer for quite sometime, I could see how easy it is to develop an application within the Ruby on Rails environment compared to the old-style development under PHP.

But there must be a reason or reasons behind why not everybody would just jump into something great. For me, one of the factors that I have considered and is really an impact for me is its speed. As a web developer, I really want my application to be fast so users won't have wait for sometime while clicking just a single link. And PHP does best with speed, although the symantics for PHP might not be a very well suited to produce such kind of library but I try to make the simplest possible way for it to work.

I tried to look at Rails and I created something that looks like it and I named it "phptarsier". A small framework that gives the basic functionality of a Model View Controller (MVC). Right now I have implemented the basic functionalities for the framework and created a blog sample along with its 0.1.0 release. You can check it out at SourceForge.

phptarsier.structure

The framework has the following structure:
- app
- controllers
- helpers
- models
- views
- default
- layouts
- docs
- config
- lib
- eyes
- actionpack
- activerecord
- tarsier
- public
- css
- img
- js
- scripts
For some developers who are not yet familiar with the structure, I am afraid that I may not be able to discuss this in detail as of now. You can still look for more reference at the Rails site.


phptarsier.structure.app

The app folder contains several folders, namely: controllers, helpers, models, and views. This folder is the heart of your application. You will be coding within this folder.
  • controllers - contains all of the application controllers. Controller will handle most of your application operations.
  • helpers - are classes/functions that can help with the controller. Maybe that is why it's called helper. :) As of 0.1.0, this is not yet implemented.
  • models - are representation of your database connection. Every table in your database will require a model if you wanted to do something with it.
  • views - contains the rendering for your application. Contains html codes, usually.


phptarsier.structure.config

You will be needing to edit the files within this folder. For database connections, edit config.php and router.php for routing configuration. These are the only files that you need to edit for you to get started. Never mind all the other folders not mentioned for now.

Get the sample blog and experiment with it. You can make questions, comments, suggestions and/or feature request at jbeduya@gmail.com. I don't accept solicitations though but I accept donations. :) Have a nice php coding.