Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shaoyangyu opened this issue Mar 27, 2013 · 3 comments
Open

Comments

@shaoyangyu
Copy link

HI, Lunaru

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

@gouvermxt
Copy link

I have the same doubt.

@shaoyangyu
Copy link
Author

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
Copy link
Author

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-> $belong_to=array('ClsA','ClsB',...)
below methods are genereated automaticlly:
$this->getSchool();//return school
$this->setSchool($school);//return $this
/
}
class School extends BaseMongoRecord
{
protected $has_many=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
*/
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants