2013-02-12 154 views
0

我以Kohana 3.3 ORM開始嘗試將其應用於現有的內部項目。Kohana 3.3 ORM關係

該項目正在使用中,所以我無法更改架構的名稱。目前的模式定義如下:

Table: utente 
idUtente VARCHAR PK 
nome VARCHAR 
// other fields 

Table: sessione 
idSessione SERIAL PK 
idUtente VARCHAR (FK to utente.idUtente) 
// other fields 

Table: ruolo 
idRuolo SERIAL PK 
nome VARCHAR 
//other fields 

Table: ruoloutente 
idRuolo PK (FK to ruolo.idRuolo) 
idUtente PK (FK to utente.idUtente) 
scadenza DATETIME 
// other fields 

現在我定義的自定義表名和自定義主鍵名稱爲模型,如果我使用ORM ::工廠(「Utente」,「馬」);(或任何其他模型)一切都很好。

class Model_Utente extends ORM { 
protected $_table_name ='utente'; 
protected $_primary_key ='idUtente'; 
protected $_has_many = 
    array(
     'ruoli' => array(
      'model' => 'Ruolo', 
      'far_key' => 'idRuolo', 
      'foreign_key' => 'idUtente', 
      'through' => 'ruoloutente', 
     ), 
     'sessioni' => array(
      'model' => 'Sessione', 
      'far_key' => 'idSessione', 
      'foreign_key' => 'idUtente', 
     ), 
    ); 
// class logic here 
} 

class Model_Ruolo extends ORM { 
protected $_table_name ='ruolo'; 
protected $_primary_key ='idRuolo'; 
protected $_has_many = 
    array(
     'utenti' => array(
      'model' => 'Utente', 
      'far_key' => 'idUtente', 
      'foreign_key' => 'idRuolo', 
      'through' => 'ruoloutente', 
     ), 
    ); 
// class logic here 
} 

class Model_Sessione extends ORM { 
protected $_table_name ='sessione'; 
protected $_primary_key ='idSessione'; 
protected $_belongs_to = 
    array(
     'utente' => array(
      'model' => 'Utente', 
      'far_key' => 'idUtente', 
      'foreign_key' => 'idUtente', 
     ), 
    ); 
// class logic here 
} 

現在從Utente的一個實例我執行

$this->ruoli->find_all()
$this->sessioni->find_all()
但我獲得在兩個空模型.. 生成的查詢是正確的兩個發現,查詢直接在SQL執行返回4個結果上ruoli和兩個在sessioni結果..

+0

所有類具有相同的名稱。 – 2013-02-12 12:15:33

+0

否是複製和粘貼錯誤,修復!對不起:) – 2013-02-12 12:16:38

+0

在我去和回答我有一種感覺,你選擇模型之間的錯誤關係。那麼你能爲我澄清一下嗎?我假設用戶可以有一個角色和一個會話,這是正確的嗎? – 2013-02-12 12:31:08

回答

0

找到了解決辦法

我的問題是,我推測,找到()和find_all()方法也將perisist於呼叫對象的查詢結果,而不是隻返回將R esult。非常感謝每一個