0

I have two tables. applicants and applicant_accounts.

applicants
 ->applicant_id   int(11) PK
 ->name           varchar(45)

applicant_accounts 
 ->account_id     int(11) PK
 ->applicant_id   int(11) 
 ->userName       varchar(45)

I've put this code code in my Applicants_Model it does'nt worked.

protected $has_one = array('applicant_account' => array('model' => 'Applicant_Account', 'foreign_key' => 'applicant_id'));

I tried to rename the primary keys into "id" and achieved my desired result by putting this code in my Applicants_Model.

protected $has_one = array('applicant_account');

And then accessed data from it by this

$applicant = ORM::factory('applicant', 1);
echo $applicant->name.' ----> '.$applicant->applicant_account->userName;

Please help me.I need to know how to have has_one relationship within two tables with primary keys not named as "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(for displaying purposes only)

$applicants = ORM::factory('applicant', 1);
echo $applicant->name.' | '.$applicant->applicant_account->userName.'<br>';
Alex Coroza
  • 1,747
  • 3
  • 23
  • 39
  • Oh. just a reminder. Im using kohana 2.3 and I saw my solution from kohana 3.2 so Im really not sure if it will work ahhaha – Alex Coroza Jul 04 '14 at 08:55

1 Answers1

0

Well my supervisor arrived here lately and he solved my problem. He changed the codes in the Applicant_Model into this:

<?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'];
    }

}

I dont really know what exactly he did but he said that he used a function inside the ORM.php and then used it to override the name of the foreign key. I hope this answer will help others.

Alex Coroza
  • 1,747
  • 3
  • 23
  • 39