我有两张桌子。申请人和申请人_cacounts。
applicants
->applicant_id int(11) PK
->name varchar(45)
applicant_accounts
->account_id int(11) PK
->applicant_id int(11)
->userName varchar(45)
我已将此代码编码放入我的Applicants_Model中,但它确实无效。
protected $has_one = array('applicant_account' => array('model' => 'Applicant_Account', 'foreign_key' => 'applicant_id'));
我尝试将主键重命名为" id" ,并通过将此代码放入我的Applicants_Model中来实现我想要的结果。
protected $has_one = array('applicant_account');
然后通过此
从中访问数据$applicant = ORM::factory('applicant', 1);
echo $applicant->name.' ----> '.$applicant->applicant_account->userName;
请帮助我。我需要知道如何在两个表中建立has_one关系,主键未命名为" id"
Applicant_Model
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Applicant_Model extends ORM {
protected $has_one = array('applicant_account' => array('model' => 'Applicant', 'foreign_key' => 'applicant_id'));
protected $primary_key = 'applicant_id';
}
Applicant_Account_Model
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Applicant_Account_Model extends ORM {
protected $primary_key = 'account_id';
}
testing.php(仅用于显示目的)
$applicants = ORM::factory('applicant', 1);
echo $applicant->name.' | '.$applicant->applicant_account->userName.'<br>';
答案 0 :(得分:0)
我的主管最近到了这里,他解决了我的问题。他将 Applicant_Model 中的代码更改为:
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Applicant_Model extends ORM {
protected $primary_key = 'applicant_id';
protected $has_one = array('applicant_account');
public function foreign_key() {
$foreign_keys = array('applicant_account' => 'applicant_id');
return $foreign_keys['applicant_account'];
}
}
我真的不知道他到底做了什么,但他说他在ORM.php中使用了一个函数,然后用它来覆盖外键的名字。我希望这个答案可以帮助别人。