【发布时间】:2011-10-01 09:57:42
【问题描述】:
我目前正在开发我的 mvc 框架(在 PHP 中)。我看到 cakePHP 能够使用“belongsTo”和“hasMany”特性。我正在尝试创建自己的功能,但遇到了问题。
假设我们有约会表(id、where、date、year_id)和年份表(id、year)。我知道这个例子可以用一张桌子来完成,但这只是一个例子。
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;
}
}
}
基本上这里发生的是我进入了无限循环。有什么建议我该如何解决?如果我需要更多解释,就直说吧。
谢谢:)。
【问题讨论】:
-
你为什么要重新发明轮子?教义开箱即用!
-
@Francesco:这并不是因为您不想知道其他人也不想知道它是如何工作的。我自己构建了无数个框架来满足我的需求,因为它们中的每个人都不适合我的。这里的 Vizualni 可能也是如此……
标签: php cakephp activerecord