Kohana - has_many and belongs_to not working properly - php

I am using Kohana 3.3 and am trying to establish relationship on my ORM tables using has_many and belongs_to . I have a users table which is defined as below
I have another table userjobs defined as below. The userjobs have a foreign key referencing user_id from `users.
In the class Model_User (extends Model_Auth_User), I have defined the relationship like
protected $_has_many = array(
'user_tokens' => array('model' => 'user_token'),
'roles' => array('model' => 'role', 'through'=> 'roles_users'),
'jobs' => array('model' => 'Userjob','foreign_key' => 'user_id' ),
);
and in the class Model_Userjob (extends ORM), the following statement is written
protected $_belongs_to = array(
'user' => array('model' => 'user')
);
However, when i try to access the jobs like below, it is throwing an exception which says that jobs is not defined in Model_User
Auth::instance()->get_user()->jobs;
I had tried dumped values of the get_user() it is not showing jobs in the protected $_has_many array.
I even tried removing user_tokens and roles from the $_has_many, but still has_many array still holds the entry for roles and user_tokens when -i printed values of get_user.

I am not sure, but I think that you should set relationship in user model like this:
protected $_has_many = array(
'user_tokens' => array('model' => 'user_token'),
'roles' => array('model' => 'role', 'through'=> 'roles_users')
);
protected $_belongs_to = array(
'jobs' => array('model' => 'userjob','foreign_key' => 'user_id' )
);
and in job model:
protected $_has_one = array(
'user' => array('model' => 'userjob', 'foreign_key' => 'user_id')
);

You need to add '->find_all()' at the end of that line. Otherwise jobs will be seen as a col.
Auth::instance()->get_user()->jobs->find_all();

Related

Kohana ORM - Empty Result on User Rights

Im building a user management system based on 2 pillars.
1. Roles
Each user have multiple roles.
2. Rights
Every role consists of one or more rights.
Users and roles are working fine in my attempt:
class Model_Auth_User extends ORM {
protected $_has_many = array(
'roles' => array('model' => 'Role', 'through' => 'roles_users'),
);...}
class Model_Auth_Role extends ORM {
protected $_has_many = array(
'users' => array('model' => 'User','through' => 'roles_users'),
);...}
I am trying to add some Roles and Rights the same way:
class Model_Auth_Role extends ORM {
protected $_has_many = array(
'users' => array('model' => 'User','through' => 'roles_users'),
'rights' => array('model' => 'Right','through' => 'role_rights'),
);
class Model_Auth_Right extends ORM {
protected $_has_many = array(
'roles' => array('model' => 'Role','through' => 'role_rights'),
);
The access to the roles works fine like this:
$roles = $user->roles->find_all(); //works fine
However, the access to the Rights of a Role always delivers an empty result:
$rights = $user->roles->rights->find_all();
Because $user->roles i collection
<?php
$rights = [];
foreach($user->roles->find_all() as $role){
$rights[] = $role->rights->find_all();
}

Kohana 3.3 ORM relations

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..
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

Kohana ORM - Incorrect table name

I have a weird problem with the Kohana (3.2) ORM query builder and i can't figure out what is wrong. I get "Incorrect table name" exception:
Database_Exception [ 1103 ]: Incorrect table name '' [ SELECT ``.* FROM `` JOIN `user_plugins` ON (`user_plugins`.`plugin_id` = ``.`id`) WHERE `user_plugins`.`user_id` = '9' ]
As you can see the table is empty in the query.
Controller:
$user = ORM::factory('user', Auth::instance()->get_user()->id);
if ($user->loaded() )
{
$result = $user->plugin->find_all();
}
User model:
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array( 'through' => 'user_plugins'),
);
...
user_plugin Model
class Model_user_plugin extends ORM
{
protected $_belongs_to = array(
'plugin' => array(),
'user' => array()
);
...
plugin Model
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('through' => 'user_plugins')
);
...
Anyone got any idea what could be wrong here?
Any help is very appreciated!
This is how User Model should be
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array('model' => 'plugin', 'through' => 'user_plugins'),
);
...
This is how Plugin Model should be
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('model' => 'user', 'through' => 'user_plugins')
);
You don't need user_plugin Model at all, the "user_plugins" in both models refers to the table name, not the model name. Just make sure you have the table with user_plugins that have following fields,
id, user_id, plugin_id
I hope this helps.
The $_has_many, by convention, must always use plural names, unless you specify the name in $_object_name in the target model. So it should be:
class Model_Plugin extends ORM
{
protected $_has_many = array(
'users' => array('through' => 'users_plugins')
);
//...
class Model_User extends ORM
{
protected $_has_many = array(
'plugins' => array('through' => 'users_plugins')
);
//...
class Model_user_plugin extends ORM
{
protected $_belongs_to = array(
'plugin' => array(),
'user' => array()
);
//...

Kohana has_many relationship

I'm using kohana 3.2 and I need help with has_many relationship. The table is written empty data...
So, my User_education model look like: http://gyazo.com/218139e52d85718c0d47bb802f0856fe User_personal model : http://gyazo.com/49fd4ab4fb7506cf8b7c608733a70365
and controller: http://gyazo.com/7d13dd3901870d7ad3d62c09e90a9c14 but fields in database still empty
You should specify the foreign key in your models:
class Model_User_Personal extends ORM
{
protected $_has_many = array(
'educations' => array(
'model' => 'user_education',
'foreign_key' => 'user_personal_id',
),
);
}
The same foreign key should be set in Model_User_Education:
class Model_User_Education extends ORM
{
protected $_belongs_to = array(
'user_personal' => array(
'model' => 'user_personal',
'foreign_key' => 'user_personal_id',
),
);
}

SQL join with Kohana ORM

I'm using Kohana 3.0.6 with ORM.
I have a model named "truck" and in his table there's a column with the id of his maker ("maker"). Then I have the "maker" model with the id and the name in his table.
I'm trying to do a simple LEFT-JOIN when I display the listing of the trucks so I can get directly the names of their maker.
Here is my "truck" model:
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_Truck extends ORM {
// Database settings
protected $_db = 'default';
protected $_table_name = 'trucks';
protected $_primary_key = 'id';
//Tried adding this but doesn't seems to work
protected $_has_one = array('maker' => array('model' => 'maker') );
// Table fields
protected $_table_columns = array(
'id' => array('data_type' => 'int', 'is_nullable' => FALSE),
'serial' => array('data_type' => 'string', 'is_nullable' => FALSE),
'maker' => array('data_type' => 'string', 'is_nullable' => FALSE),
'model' => array('data_type' => 'string', 'is_nullable' => FALSE),
'year' => array('data_type' => 'int', 'is_nullable' => FALSE),
);
}
As you can see, I'm using this line to add the "has_one", though I've also seen the "with()" call somewhere but couldn't make it work proprely (doc is a bit lacking, especially for version 3.x.x).
protected $_has_one = array('maker' => array('model' => 'maker') );
Here's the line I'm using in the view to output the maker name (something along those lines):
foreach ($trucks as $t) {
echo($t->maker->name);
}
Do you have the column named truck_id in the makers table?

Categories