lunaru / MongoRecord

MongoRecord is a simple, easy to set-up Mongo ORM for PHP with ActiveRecord-like features.
MIT License
106 stars 24 forks source link

If MongoRecord support relationships? like belong_to or has_many. #7

Open shaoyangyu opened 11 years ago

shaoyangyu commented 11 years ago

HI, Lunaru

Not sure if MongoRecord support the relationships of records? like has_many or belong_to?

gouvermxt commented 11 years ago

I have the same doubt.

shaoyangyu commented 11 years ago

update , currently I declare functions to impl has_many or belong_to, like below code, class PicPackageRecord extends BaseMongoRecord {

    /**
     *   @return Friend
     */
       function getFriend()
       {
                 $friendrecord=FriendRecord::findOne(array('_id'=>$this->getFriendId()));

                  return $friendrecord;
       }
    /**
     *   @param Friend 
     */
       function setFriend($friendrecord)
       {
          $this->setFriendId($friendrecord->getID());
       }

}

BUT I guess BaseMongoRecord can provide some relational functions, subclass can call it in constructor to define relationships. :) Then sublcass can generate get/set methods automaticllly. it is rough idea, does it work ?

class FriendRecord extends BaseMongoRecord { function constructor() { parent::construct(); parent::has_many(array(PicPackageRecord));
} } class PicPackage extends BaseMongoRecord { function constructor() { parent::construct(); parent::belong_to(array(FriendRecord));
} }

shaoyangyu commented 11 years ago

HI, Lunaru

I tried to provide relation support fo MongoRecord as below , more detail, can check https://github.com/shaoyangyu/MongoRecord/blob/master/sample/easysample.php :

class Student extends BaseMongoRecord { protected $belong_to=array('School'); /* can add more targets with-> $belongto=array('ClsA','ClsB',...) below methods are genereated automaticlly: $this->getSchool();//return school $this->setSchool($school);//return $this / } class School extends BaseMongoRecord { protected $hasmany=array('Student'); /* Can add more targets with-> $has_many=array('ClsA','ClsB',...) below methods are genereated automaticlly: $this->getStudents();//return MongoRecordIterator $this->setStudents($stuarray);//parm is array of students,return $this $this->createStudent();//new student and return it,return student $this->addStudent($stu);//parm is student instante,return $this $this->removeStudent($stu);//parm is student instant,return $this / }