Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make model "hasMany" and "belongsTo"? (Same like cakePHP)
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <pre><code>class appointment_model extends model{ public $_belongs_to= array('table'=&gt;'year','field_connector'=&gt;'year_id'); public $one_value; } </code></pre> <p>......</p> <pre><code>class year_model extends model{ public $_has_many= array('table'=&gt;'appointment','field_connector'=&gt;'year_id'); public $many_values; } </code></pre> <p>....</p> <pre><code>class model{ private function select($query=null){ if(!$query)die("Query cannot be null. Line num: " . __LINE__); $results = $this-&gt;_db-&gt;mysql_select($query); if(count($results)==1){ $one = $this-&gt;initialize($results[0]); $this-&gt;_do_for_other_tables($one); return $one; }elseif(count($results)&gt;1){ $returns = array(); foreach($results as $result){ $one = $this-&gt;initialize($result); $returns[] = $this-&gt;_do_for_other_tables($one); } return $returns; }else{ return null; } } private function _do_for_other_tables(&amp;$one, $rec=1){ if(property_exists($this, 'many_values')){ if(!empty($one-&gt;many_values))return; $many = functions::load_model($this-&gt;_has_many["table"]); $res = $many-&gt;find_by($this-&gt;_has_many["field_connector"], $one-&gt;id); $one-&gt;many_values = $res; } elseif(property_exists($this, 'one_value')){ if(!empty($one-&gt;_belongs_to))return; $only_one = functions::load_model($this-&gt;_belongs_to["table"]); $field = $this-&gt;_belongs_to['field_connector']; $res = $only_one-&gt;find_by('id', $one-&gt;$field); $one-&gt;one_value = $res; } } } </code></pre> <p>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.</p> <p>Thanks :).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload