How to get more data from a relationship table in Yii - yii

I have 3 tables:
numbers (id, name)
food (id, name)
numbers_food (number_id, food_id, price_per_ad, price_per_ch)
How I can to get a price_per_ad and price_per_ch data for each food_id from numbers_food relationship table?

Just define relationships in models.
In NumberFoodModel:
'food' => array(self::BELONGS_TO, 'Food', 'food_id'),
In FoodModel:
'number_food' => array(self::HAS_MANY, 'NumberFood', 'food_id'),
Now in your code just use
Food::model()->with('number_food')->findByPk($id)

Related

Insert many-to-many relations in one transaction using Korma

I have 2 models with many-to-many relationship like the next:
(declare organisations)
(defentity users
(many-to-many organisations :users_organisations
{:lfk :user_id, :rfk :organisation_id}))
(defentity organisations
(many-to-many users :users_organisations
{:lfk :organisation_id, :rfk :user_id}))
(defentity users-organisations
(table :users_organisations))
I want to insert rows into both models and make a relationship record in the same transaction
(transaction
(let [usr (insert users (values {:email "john#mail.me"}))
org (insert organisations (values {:title "My Co."}))]
(insert users-organisation
(values {:user_id (:id usr)
:organisation_id (:id org)}))))
Unfortunately, it doesn't work this way. None of insertions into users or organisations work. Is it possible to do something like that in Korma?
What I try to achieve is something like the next Postgresql statement:
WITH new_user AS (
INSERT INTO users (email)
VALUES ('john#mail.me')
RETURNING id
), new_org AS (
INSERT INTO organisations (title)
VALUES ('My Co.')
RETURNING id
)
INSERT INTO users_organisations (user_id, organisation_id)
VALUES ((SELECT id FROM new_user), (SELECT id FROM new_org))
RETURNING *;
Thanks in advance for any help!

Cakephp join condition error

I have two tables employees and posting.
employees with fields id,name
AND
postings with fields id,employee_id,status
Now to retrieve all the employees(with posting details) whose Posting.status = 1, I wrote in PostingsController.php's view() action:
$cond = array('Posting.status'=>1);
$e = $this->Employee->find('all',array('conditions' => $cond));
$this->set('emp',$e);
In Employee.php model var $hasMany = 'Posting';
In Posting.php model var $belongsTo = 'Employee';
But the above code generates an error like :
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column
'Posting.status' in 'where clause'
Generated SQL:
SQL Query: SELECT `Employee`.`id`, `Employee`.`name` FROM `cakelab`.`employees` AS `Employee` WHERE `Posting`.`status` = 1
I am new to CakePhp joins, please guide me.
Cake doesn't join the tables when the relation type is hasMany. Rather it performs a query on the first table (employees) and with the id of the employee it performs a second query on postings table to retrieve all the postings related to that employee
you can manually join the tables but I suggest querying on postings table and then group by Employee
$this->Posting ->find(
'all',
array(
'fields' => array('Employee.id', 'Employee.name', 'Posting.status' ),
'conditions' => $cond,
'group' => array('Employee.id')
)
);

how to make relation between two foreign key values of two tables in Yii

I have the database just like this
==== cis_policy_registration====
id
policy_id
email_id
policy_start_date
==== cis_policy_family_details===
id
policy_id
is_main_applicant
firstname
lastname
gender
Now how to make the relation between models with the policy_id (both tables),
I want to take firstname and lastname in registration model and check is main applicant
that must list in CGridView
who can solve this problem
thanks in advance
The relation between these two tables should be handled from the "main" model (Policy), so in Policy model class you should have:
'policy_reg' => array(self::HAS_ONE, 'PolicyRegistration', 'policy_id'),
'policy_details' => array(self::HAS_ONE, 'PolicyDetails', 'policy_id'),
And then:
$policy = Policy::model()->with(array('policy_details'))->findByPk($pk);
$policy->policy_details->is_main_applicant;
...
And in CGridView you can print the relational value like this (after sending CActiveDataProvider object from Policy model):
'policy_details.firstname'
or
array(
'name'=>'Firstname',
'value'=>'$data->policy_details->firstname',
),

CGridView to display data from two tables?

I'd like to set up a CGridView. I'm looking to display data from 2 different tables in one view. The data is from a table called student, and another table called employee.
employee is refereed by student(reg_no)
student table
reg_no (primary key)
s_name
dept
f_name
employee table
e_no(primary key)
e_name
desig
salary
reg_no(foreign key)
I want to display the (reg_no,s_name,f_name) from student and (design,salary) from employee table in a single grid view, can anyone please give an idea or any tutorial,
First make sure that your models and relationships are defined properly. Check the models to make sure that relationships are there.
e.g. you shall have an entry like this in your employee model;
function relations() {
return array(
'reg_no'=>array( self::BELONGS_TO, 'Student', 'reg_no' ),
);
}
Use CActiveDataProvider in the CGridView instead of an array as the data source.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->searchEmplyees(),
.......
.......
Add another search cirteria to your model that returns the data as a CActiveDataProvider.
public function seachEmployees()
{
$criteria=new CDbCriteria;
$criteria->alias = 'i';
$criteria->compare('id',$this->id);
.......
.......
$criteria->join= 'JOIN 'your table name' d ON (i.id=d.id)';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'order_in_sna ASC',
),
));
}
I have just typed this code in for your reference so you understand how Yii is handling all of this for you. This is another good example, again from the Yii Framework site.

FluentNHibernate one-to-one Mapping

I have 3 tables:
Table: Company, Columns: Id, Name
Table: User, Columns: Id, Name, CompanyId
Table: CompanyOwnerInfo, Column: Id, CompanyId, OwnerName.....
CompanyId column in both User and CompanyOwnerInfo tables are mapping to Company.Id
My question is how to create a One-to-One mapping for User and CompanyOwnerInfo table without Company table involved? because when I use User object to getting CompanyOwnerInfo there is no necessary to join the Company table?
public UserMap()
{
References(x => x.Company, "CompanyId");
References(x => x.CompanyOwnerInfo, "CompanyId")
.PropertyRef(compOwner => compOwner.Company)
.Fetch.Join()
.Not.LazyLoad() // if needed
.ReadOnly(); // important otherwise you have Exception on save because both References want to write to the same column
}