0
votes

i'm starting with Kohana 3.3 ORM trying to apply it to an existing internal project.

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'); (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

$this->ruoli->find_all()
and
$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..
1
All classes have identical name. - Michal M
No was a copy and paste error, fixed! Sorry :) - Marco Capoferri
Before I go and answer I have a feeling you chose wrong relations between Models. Can you clarify this for me then. I assume a user can have a single role and a single session, is this correct? - Michal M
@MichalM think about utente as an user, sessione as a working session, ruolo as a company role and ruoloutente as a relationship table between users and sessions. Every user can be involved in more than one working session per time and every user has at least one role and maximum 10 roles. - Marco Capoferri
@Marco: Don't do "SOLVED". Instead, create an answer yourself and accept it. - webbiedave

1 Answers

0
votes

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. Many thanks to every one