Vizualni
Vizualni

Reputation: 381

How to make model "hasMany" and "belongsTo"? (Same like cakePHP)

I am currently working on my mvc framework (in PHP). I saw that cakePHP has ability to use "belongsTo" and "hasMany" features. I am trying to create my own feature and I hit a problem.

Lets say that we have appointment table (id, where, date, year_id) and year table (id, year). I know this example could be done with one table without a problem, but it's just an example.

class appointment_model extends model{
    public $_belongs_to= array('table'=>'year','field_connector'=>'year_id');
    public $one_value;
}

......

class year_model extends model{
    public $_has_many= array('table'=>'appointment','field_connector'=>'year_id');
    public $many_values;
}

....

class model{

private function select($query=null){
    if(!$query)die("Query cannot be null. Line num: " . __LINE__);
    $results = $this->_db->mysql_select($query);
    if(count($results)==1){
        $one = $this->initialize($results[0]);
        $this->_do_for_other_tables($one);
        return $one;
    }elseif(count($results)>1){
        $returns = array();
        foreach($results as $result){
            $one = $this->initialize($result);
            $returns[] = $this->_do_for_other_tables($one);     
        }
        return $returns;
    }else{
        return null;
    }

}


private function _do_for_other_tables(&$one, $rec=1){


    if(property_exists($this, 'many_values')){
        if(!empty($one->many_values))return;
        $many = functions::load_model($this->_has_many["table"]);
        $res = $many->find_by($this->_has_many["field_connector"], $one->id);
        $one->many_values = $res;
    }
    elseif(property_exists($this, 'one_value')){
        if(!empty($one->_belongs_to))return;
        $only_one = functions::load_model($this->_belongs_to["table"]);
        $field = $this->_belongs_to['field_connector'];
        $res = $only_one->find_by('id', $one->$field);
        $one->one_value = $res;
    }

}

}

Basically what happens here is that I enter into infinite loop. Any suggestion how can I fix that? If I need to explain it more, just say it.

Thanks :).

Upvotes: 0

Views: 681

Answers (1)

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

You problem is you treat the properties "hasMany", "belongsTo", "hasOne", "etc" as simple properties instead of treating them as 2way relationships. What i've done in many of my own frameworks is to initialize these properties by hand by the programmer but when the class initializes, you check these properties and generate a single object that links the two together.

Create a class like a System_DB_Relationship object and you place the same copy in the two initialized models. If you don't instanciate your models as Factory design pattern, you can use the static $relationship model to store your relationships too. I tend to go for Factory/Active record so it's better to manage these. Now depending on the logic of your model base class, you may want to place a flag in the relationship to say, "hey, this relationship has been used" so you don't pass into it again.

Another way is to always work in a descending fashion. That is, disregard the hasMany when it is found and create only a proxy method to load the child elements but load all the belongsTo so that the parent is loaded by default. Note though that this methods can become dangerous if you have a large set of classes, so what i implement in my frameworks is usually a loading parameters that says, load X levels of relationships. And when you want the hasMany, you just use the __call() to intercept a call to "$myobject->subobjects()" and just load them on the fly.

There are tons of ways to do it, just take your time to trace your code by hand, look at what doesn't make sense, try out stuff and you'll get to something eventually. But as Francesco said above, it's rarely useful to reinvent the wheel unless you work on really old projects that can't be transformed at once. Take this work you are doing as an opportunity to get better and know how it works under the hood...

Upvotes: 1

Related Questions