2013-02-12 101 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。非常感谢每一个