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',
),
);
}
Related
I have 3 tree like connected tables. Their schemas as follows:
Member{
//Some column
}
Transactions{
member_id :: foreign key of member table
//Some other column
}
TransactionItems{
transaction_id :: foreign key of Transaction table
//Some other column
}
I define models like this:
class Members extends AppModel {
public $primaryKey = 'id';
public $hasOne = array(
'Transactions' => array(
'className' => 'Transactions',
'foreignKey' => 'member_id',
'dependent' => true
)
);
}
class Transactions extends AppModel {
public $primaryKey = 'id';
public $belongTo = array('Members');
public $hasOne = array(
'TransactionItems' => array(
'className' => 'TransactionItems',
'foreignKey' => 'transaction_id',
'dependent' => true
)
);
}
class TransactionItems extends AppModel {
public $primaryKey = 'id';
public $belongTo = array('Transactions');
public $belongsTo = array(
'Transactions' => array(
'className' => 'Transactions',
'foreignKey' => 'transaction_id'
)
);
}
I have a Data Array which I want to save into database. My scheme is:
Array(
[Members] = [],//Array
[Transactions] = [],//Array
[TransactionItems] = []//Array
)
The problem is that whenever I run $this->Members->saveAll($data). It save data in Member and Transactions table. But do not create data in TransactionItems table. I want to save in all 3 tables at a time.
Any help would be grateful.
Second level (and above) associations must be nested, ie the data structure needs to be:
array(
'Members' => array(),
'Transactions' => array(
'TransactionItems' => array()
)
)
A bit awkward, but that's how it works in 2.x. You can always refer to the structure that is being returned when reading data, it needs to be the same when saving it.
Furthermore you must set the deep option to true in order to be able to save second level and above associations (by default only first level associations are being saved):
$this->Members->saveAll($data, array('deep' => true));
See also
Cookbook > Models > Saving Your Data > Model::saveAssociated()
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();
I'm Using cakePHP 2.3.8
I have two tables: application, computer_application. The relationship is one application hasMany computer_application, foreign key is application_id
in my ComputerApplication model:
class ComputerApplication extends AppModel{
public $name = "ComputerApplication";
public $useTable = "computer_application";
var $belongsTo = array(
'Computer' => array(
'className' => 'Computer',
'foreignKey' => 'computer_id',
'dependent' => true
),
'Application' => array(
'className' => 'Application',
'foreignKey' => 'application_id',
'dependent' => true
)
);
}
In my ComputerApplication controller. HERE I INITIALIZE THE POPULATION OF DROPDOWN in **add** function
public function add($id=null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('computerApplications',
$this->ComputerApplication->Application->find('list',
array('fields' => array('description') ) ) );
}
Now In my Add View
echo $this->Form->create("computerApplication");
echo $this->Form->input('application_id',array('empty'=>''));
echo $this->Form->end('Save Post');
My problem is that it won't populate the select input. This is the first time I used 2 words in table [computer_application] using cake since I don't have problem populating other table with just one word. Just help me identify which I need to tweak for it to populate.
$this->set('applications', ...
and not
$this->set('computerApplications', ...
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
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()
);
//...