简体   繁体   English

Kohana 3.3 ORM关系

[英]Kohana 3.3 ORM relations

i'm starting with Kohana 3.3 ORM trying to apply it to an existing internal project. 我从Kohana 3.3 ORM开始尝试将其应用于现有的内部项目。

The project is in use, so i can't change the schema's names. 该项目正在使用中,因此我无法更改架构的名称。 The current schema definition is the following: 当前的架构定义如下:

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

Now i defined custom table name and custom primary key name into the models and if i use ORM::factory('Utente', 'Marco'); 现在,我在模型中定义了自定义表名和自定义主键名,如果我使用ORM :: factory('Utente','Marco'); (or any other model) everything is going fine. (或任何其他模型)一切正常。

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
}

Now from an instance of Utente i execute 现在从Utente实例执行

$this->ruoli->find_all()
and
 $this->sessioni->find_all() $ this-> sessioni-> find_all() 
but i obtain an empty model on both.. The generated query is correct on both finding, query executed directly in SQL returns 4 results on ruoli and two results on sessioni.. 但是我在两者上都获得了一个空模型。.生成的查询在两个查找中都是正确的,直接在SQL中执行的查询在ruoli上返回4个结果,在sessioni上返回两个结果。

Found a solution 找到了解决方案

My problem was that i supposed that find() and find_all() methods would also perisist in the caller object the query results instead of only return the result. 我的问题是我认为find()和find_all()方法也会在调用者对象中持久存在查询结果,而不是仅返回结果。 Many thanks to every one 非常感谢每一个人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM