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
Related
I need que make a select query from a table outside my user. I can make the select query from toad for example but in cakephp i get this error:
Missing Database Table Error: Table cursos for model Curso was not
found in datasource ot.
I understand the error occurs because the table doesn't exists directly in my user.
The question is: is there any way to get the data in this situation??
look what I understand the problem you have 2 databases and users want to use one for work:
Multiple Database connection issue CakePHP
in your database.php You can configure 2 connections
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'test',
'password' => 'test1',
'database' => 'test_portal',
'prefix' => ''
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'dfffd_23',
'password' => 'dsfsd324',
'database' => 'testdbuser',
'prefix' => ''
//'encoding' => 'utf8',
);
}
now in your model you have to specify which of the settings or connections want used for so you can select the users that you want.
class Example extends AppModel {
public $useDbConfig = 'test';
}
you can see this in the documentation
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 Have a GetStudents function in Agent model. where I fetch the students related to the agents.
But I want to show more fields of students from another table
Eg
**Agent table** (agent has students)
agent_id
**student table**
STUDENTID pkey
agent_id
**relation table** (this table creates relation between student and household)
StudentID HOUSEHOLDID
**HOUSEHOLD TABLE**
HouseHOLDID Householdname
I want to fetch householdname when I fetch all student details.
really looking bazzare to me as I am newbie to YII
MY function in agent model.
public static function getStudents($id) {
$relationships = Relationship::model()->findAll('type = :type AND source = :agent_id', array(':type' => 2, ':agent_id' => $id));
//$relationships=sort($relationships);
// Generate relationship array
//print_r($relationships);
foreach ($relationships as $relationship) {
$in[] = $relationship->destination;
}
// Generate db condition
$criteria = new CDbCriteria;
if (! isset($in) || ! is_array($in) || sizeOf($in) == 0) {
$in[] = -999;
}
$criteria->addInCondition('StudentID', $in, 'OR');
return new CActiveDataProvider('Student', array(
'criteria' => $criteria,
'sort'=>array('defaultOrder'=>array(
'StudentID'=>CSort::SORT_DESC,
)),
));
}
My code to fetch data in by passing ID into it
<?php $this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'attributes' => array(
array(
'label' => 'Agent Details',
'type' => 'raw',
'value' => '',
'cssClass' => 'heading',
),
'agent_id',
'user.email',
'first_name',
'last_name',
'company',
'phone',
),
)); ?>
Any help would be greatly appreicated.
Thanks
Ab
First you should make relation with student to relation table in relations array in relation model like this ..
'student'=>array(self::BELONGS_TO, 'Student', 'StudentID'), //after belongs to student is model class name
'household'=>array(self::BELONGS_TO, 'HOUSEHOLD', 'HOUSEHOLDID'),//after belongs to HOUSEHOLD is model class name
And then you can fetch all record with active record like this...
$relationships = Relationship::model()->with('student','household')->findAll('type = :type AND source = :agent_id', array(':type' => 2, ':agent_id' => $id));
I'm trying to use Lithiums list option with find() along with SQL's DISTINCT.
I should get an array populated with values, instead, I'm getting an empty array.
This does make sense since I'm passing in the distinct fields as one string instead of an array of elements but I don't know how else to use DISTINCT in Lithium.
Some direction would be greatly appreciated.
It may be valentines day but Lithium is not showing me too much love today :)
Model:
class ZipCodes extends \app\extensions\data\Model {
protected $_meta = array(
'key' => 'zip_code_id',
'title' => 'state_name'
);
protected $_schema = array(
'state_name' => array('type' => 'varchar'),
'StateFIPS' => array('type' => 'varchar')
//there are more fields in my table but I haven't defined the
//rest in my model
);
}
The add method in my controller
public function add()
{
$zipcodes = Zipcodes::find('list', array(
'fields' => array('DISTINCT state_name'),
'order' => 'state_name ASC',
'conditions' => array('state_name' => array('!=' => ''))
)
);
return compact('zipcodes');
}
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.