I trying to make many-to-many relation between 2 models: Users_Role and Users_Right
class Model_Users_Role extends ORM{
protected $_has_many = array(
'rights' => array(
'model' => 'users_right',
'through' => 'users_roles_rights',
),
);
}
class Model_Users_Right extends ORM{
protected $_has_many = array(
'roles' => array(
'model' => 'users_role',
'through' => 'users_roles_rights',
),
);
}
I'm trying to do this:
$role = ORM::factory('users_role', 1);
$right = ORM::factory('users_right', 1);
$right->add('roles', $role);
Error:
Database_Exception [ 1054 ]: Unknown column 'role_id' in 'field list' [ INSERT INTO `users_roles_rights` (`users_right_id`, `role_id`) VALUES ('1', '1') ]
I tryed to make it on the other side:
$role = ORM::factory('users_role', 1);
$right = ORM::factory('users_right', 1);
$role->add('rights', $right);
New error:
Database_Exception [ 1054 ]: Unknown column 'right_id' in 'field list' [ INSERT INTO `users_roles_rights` (`users_role_id`, `right_id`) VALUES ('1', '1') ]
I expected ORM to use users_role_id and users_right_id field names in pivot table, but it uses wrong name of far key? Where I have made a mistake?
Check out where the default values are set.
Try this:
class Model_Users_Role extends ORM{
protected $_has_many = array(
'rights' => array(
'model' => 'users_right',
'far_key' => 'users_right_id',
'through' => 'users_roles_rights',
),
);
}
class Model_Users_Right extends ORM{
protected $_has_many = array(
'roles' => array(
'model' => 'users_role',
'far_key' => 'users_role_id',
'through' => 'users_roles_rights',
),
);
}
Kohana does not check that the relationship does not already exist.
Either enforce this in the database table with a composite unique key on the two foreign key columns, or make your code check that the relationship does not already exist before adding.
Related
The Database Tables:
project_master (id, project_name)
task_master (id, task_name, project_id)
Relationship in the TaskMaster Model:
TaskMaster.php
class TaskMaster extends CActiveRecord
{
/**
* #return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'ProjectsRpl' => array(self::BELONGS_TO, 'Projects', 'project_id'),
);
}
}
Following GroupGridView view file:
Task.php
$this->widget('ext.groupgridview.GroupGridView', array(
'id' => 'Customer-grid',
'dataProvider' => $modelCustomer->searchCustomer(),
//'mergeColumns' => 'project_id',
'extraRowColumns' => array('ProjectsRpl.project_name'),
'extraRowPos' => 'above',
'afterAjaxUpdate' => 'function(){}',
'columns'=>$columns,
));
GroupGridView reference site.
Getting the following errors:
CException: Column or attribute "ProjectsRpl.project_name" not found!
Only one change in Task.php file.
$this->widget('ext.groupgridview.GroupGridView', array(
'id' => 'Customer-grid',
'dataProvider' => $model->search(),
//'mergeColumns' => 'project_id',
'extraRowColumns' => array('project_id'),
'extraRowPos' => 'above',
'extraRowExpression' => '"<b style=\"color: black\">".$data->ProjectsRpl->project_name."</b>"',
'afterAjaxUpdate' => 'function(){}',
'ajaxUrl' => Yii::app()->createUrl('customer/index'),
'ajaxUpdate' => true,
'enablePagination' => true,
"summaryText" => true,
'enableSorting' => FALSE,
'columns'=>$columns,
));
I have an phpactiverecord Model "Category". This Model has a self-referential has_many association, and two has_one associations to another Model.
class Category extends Model {
static $table_name = 'categories';
static $has_many = array(
array(
'child_categories',
'class_name' => 'Category',
'primary_key' => 'categories_id',
'foreign_key' => 'parent_id'
)
);
static $has_one = array(
array(
'german_description',
'class_name' => 'CategoryDescription',
'primary_key' => 'categories_id',
'foreign_key' => 'categories_id',
'conditions' => 'language_id = 2'
),
array(
'english_description',
'class_name' => 'CategoryDescription',
'primary_key' => 'categories_id',
'foreign_key' => 'categories_id',
'conditions' => 'language_id = 1'
)
);
}
I can eager-load the has_one associations when I find Category, this works without any problem:
$category = ActiveRecord\Category::find(
$category_id,
array(
'include' => array( 'german_description', 'english_description' )
)
);
My problem is: When I iterate through all of a category's child-categories, and access the child-categories' has-one associations...
foreach( $category->child_categories as $child_category ){
echo $child_category->german_description->categories_name;
}
..then a new query is run every time I do so, becauce the child-category's has-one associations are not eager loaded.
My question is: Is it possible to configure access a categories child-categories in a way that eager-loads the child-categories' has-one associations?
I am trying to build a simple relation between two tables.
Unfortunately, the ORM searches for another column from the parent model:
<?php
class Model_Hreflang extends ORM {
[...]
protected $_table_columns = array(
'id' => NULL,
'domain_id' => NULL,
'country_code_id' => NULL,
'language_code_id' => NULL
);
protected $_has_many = array(
'cc' =>
array(
'model'=> 'Country2',
**'WHAT TO PUT HERE'** => 'country_code_id',
'foreign_key' => 'country_code'),
}
When trying to get a related record to the first table, the query is made like
"hreflang".id = "country2".country_code
and I need to change it, for example to
"hreflang".country_code_id = "country2".country_code
Anyone managed to do it by the ORM?
Thanks!
Anton
I think you schould set it by belongs to
protected $_belongs_to = array('cc' => array('model' => 'Country2', 'foreign_key' => 'country_code'));
read more here: http://kohanaframework.org/3.0/guide/orm/relationships
Agent:
agent_id (primary key)
User:
f_id (foreign key)
type
I have created relation in this way
public function relations() {
return array(
'user' => array(self::HAS_ONE, 'Users', 'f_id'),
);
}
But I want to add more conditions like join only if type=3 in User table.
thanks.
There is no error like 'Property "CHasOneRelation.0" is not defined' if you use this:
public function relations()
{
return array(
'user' => array(
self::HAS_ONE,
'Users',
'f_id',
'on' => 'user.ref_type = :type',
'params' => array(':type' => 3))
);
}
See this link: http://www.yiiframework.com/forum/index.php/topic/10185-using-relations-and-conditions/
add the condition on your relation
public function relations() {
return array(
'user' => array(self::HAS_ONE, 'Users', 'f_id', array(
'condition' => 'user.type = :type',
'params' => array(':type'=>3)
)),
);
}
http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-options
You should create a function to get user rather use lazy-load which would use more query even you do not use this relation.
public function getUser(){
return Users::model()->find(array(
'condition'=>'type = :type',
'params' => array(':type'=>3)
));
}
By using this you could use cache function to cache query that relation does not support.
public function getUser(){
return Users::model()->cache(1000)->find(array(
'condition'=>'type = :type',
'params' => array(':type'=>3)
));
}
I don't know how to approach this.
Lets say I have 3 models, A, B, and C.
Model A has many C, Model B has many C, C belongs to A and B.
I get all of B.
$getBs=ORM::factory('B')->find_all();
I display A, B, C.
foreach($getBs as $getB)
{
echo $getB->b_category_title;
foreach($getB->C->find_all() as $getC)
{
echo $getC->c_title;
echo $getA->a_author; //Problem part
}
}
I do not know how to access and connect Model A to Model C when displaying information for Model C.
Edit
To get working code, I change Model A - C to Model One - Three.
Using biakaveron example of _load_with, I get the following error:
Database_Exception [ 1054 ]: Unknown column 'three.id_c' in 'on clause' [ SELECT `ones`.`a_id` AS `ones:a_id`, `ones`.`a_author` AS `ones:a_author`, `three`.* FROM `threes` AS `three` JOIN `twos_threes` ON (`twos_threes`.`id_c` = `three`.`c_id`) LEFT JOIN `ones` AS `ones` ON (`ones`.`a_id` = `three`.`id_c`) WHERE `twos_threes`.`id_b` = '1' ]
Models:
class Model_One extends ORM {
protected $_primary_key = 'a_id';
protected $_has_many = array(
'threes'=> array(
'model' => 'three',
'through' => 'ones_threes',
'far_key' => 'id_c',
'foreign_key' => 'id_a'
),
);
}
class Model_Two extends ORM {
protected $_primary_key = 'b_id';
protected $_has_many = array(
'threes'=> array(
'model' => 'three',
'through' => 'twos_threes',
'far_key' => 'id_c',
'foreign_key' => 'id_b'
),
);
}
class Model_Three extends ORM {
protected $_primary_key = 'c_id';
protected $_belongs_to = array(
'ones'=> array(
'model' => 'one',
'through' => 'ones_threes',
'far_key' => 'id_a',
'foreign_key' => 'id_c'
),
'twos'=> array(
'model' => 'two',
'through' => 'twos_threes',
'far_key' => 'id_b',
'foreign_key' => 'id_c'
),
);
protected $_load_with = array('ones');
}
Why is it looking for three.id_c?
C belongs to A and B.
foreach($getBs as $getB)
{
echo $getB->b_category_title;
foreach($getB->C->find_all() as $getC)
{
echo $getC->c_title;
echo $getC->A->a_author;
}
}
PS. Just a note. You can load both C and A objects using $_load_with property:
class Model_C extends ORM {
// ...
protected $_load_with = array('A');
// ...
}