Shaikh Sonny Aman’s Blog

previously www.mailtoaman.com

PHP Active record with relational foreign key support

Posted on | March 22, 2008 |

Many a time i wished  I could find a php active record or active model that will support relational table. Failure on this, I wrote my own and named it as ActiveRelationalRecord.

Say,  I have two tables  posts(idposts,title,body) and comments(idcomments,comment,date). Now, using Active relational record I can do the following:

$arr = new ActiveRelationalRecord();
//$arr->init(’posts’,$_POST);
$arr->init(’posts’,array(’idposts’=>’12′));
$arr->addRelation(’comments’);
//$arr->addRelation(’anotherrelationaltable’);

echo $arr->title ;
echo $arr->body;
$comms = $arr->comment;
foreach($comms as $com)
{

echo $com;
echo ‘<br/>’;

}

// $arcom = new ActiveRelationalRecord();
//$idcomms = $arr->idcomments;
//foreach($idcomms as $idcom)
//{

//$arcom->init(’comments’,array(’idcomments’=>$idcom));
//echo $arcom->date;
//echo ‘<br/>’;

//}

Or, say you have two tables, user(idusers,name,password) and userdetails(iduserdetails,email,telephone) .
So, ARR can help in the following way:

$arr = new ActiveRelationalRecord();
$arr->init(’users’,array(’idusers’=>’34′));
$arr->addRelation(’userdetails’);

echo $arr->name;
echo $arr->email;

Finally, I don’t like the idea that an ActiveRecord or Model should have functions like find which will return multiple
record. For such utility , there should be some resultset type class.

Lastly, in future I will update the relational query to associate with a class.

Any more idea plz ?

Comments

6 Responses to “PHP Active record with relational foreign key support”

  1. anupom
    March 23rd, 2008 @ 2:03 am

    Have you tried CakePHP’s ActiveRecord? If you have time you can check that out - it’s just awesome. Also, AdoDB has a PHP implementation of ActiveRecord, AFAIK.

  2. Shaikh Sonny Aman
    March 23rd, 2008 @ 7:13 am

    Thank you for your comment. Why I cannot help making wheels again ! :P

  3. Lyubomir Petrov
    March 29th, 2008 @ 3:17 am

    Use php’s magic functions this will help u alot, also implementing Iterator for results sets will r0x :)
    This is how i define standart ActiveRecord implementation in PHP :) (btw (e)RoR sux)

  4. Shaikh Sonny Aman
    March 29th, 2008 @ 6:48 am

    Lyubomir, nice idea ! I’m gonna implement an iterator soon !
    Btw, I am using the php magic functions :) But, planning to call stored procedures by overriding __call. Need to think a bit more if it will be really any good help. Any idea ?

    Thanks !

  5. skilzzz133
    April 6th, 2008 @ 8:27 am

    I like everything positive. I’m on this website since the first day of its existence and I have never seen anything negative on it. This website is made by people, who think positive and who have many things to tell others. I love this website, I always get positive emotions from it and I can stay here the whole days and nights!

  6. gogomast23
    April 9th, 2008 @ 2:43 am

    Oh man! Are you all still here? I thought this is not an interesting topic anymore! Visit other sources!

Leave a Reply