Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:29:17+00:00 2026-05-25T22:29:17+00:00

I am currently working on my mvc framework (in PHP). I saw that cakePHP

  • 0

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 :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:29:17+00:00Added an answer on May 25, 2026 at 10:29 pm

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my own hand-rolled PHP MVC framework for some projects that I'm working
I am currently working on my first asp.net mvc application. I am trying to
I'm currently working on my own PHP Framework, and I need some help figuring
I'm creating a PHP web application framework (MVC). I'd rather not use external libraries
I'm working on an app (based on Spring as DI and MVC framework) that
I am currently working on a php framework, which is in some cases structured
I am currently working on my own implementation of a MVC framework. I would
I'm currently working on a PHP project using CodeIgniter as my framework. I took
I'm currently working on a legacy eCommerce system front-end that has alot of duplicate
I am currently working on Spring MVC web app and trying to hook up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.