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.
Related
I have a problem with Laravel 7 Eloquent. I have 2 tables joined by many to many relation. Table user is connected to PaymentModule by pivot table PaymentModuleUser. I need to use where on pivot table on statement is_active = 1. When i use toSQL() in my model SQL returns good results but when i check my Eloquent Query in Controller, data that i revicive ignore my wherePivot method (return all data from table ignoring is_active = 1 field subscribers = null and get me this user.. I must do it if my pivotWhere = null dont show this user). Could you point me where i get wrong with my code?
My UserTable model:
public function subscriber(){
return $this->belongsToMany(PaymentsModule::class, 'payment_module_user', 'user_id')->withPivot('is_active');
}
MyController:
$users = User::with(['subscriber'=>function($query)
{
$query->wherePivot('is_active','=', 1);
}])->get();
print_r($users);
Try with
$users = User::with('subscriber' => function($query) {
$query->where('payment_module_user.is_active', 1);
})->get();
print_r($users);
The pivot table is already joined by eloquent, so just start using it
Found it here.
In your controller, try using this
$query->wherePivot('is_active', 1);
Instead of,
$query->wherePivot('is_active','=', 1);
I have three tables. I want to display all data from cms_planner table together with Topic Name from cms_topic table. To achieve that, I need to go through the cms_subject table.
I want to use belongsToMany but I already have cms_subject table that holds the foreign key for cms_planner and the foreign key for cms_topic. The table name does not represent pivot key.
I also want to use hasManyThrough but it doesn't work. I'm thinking to inverse the hasManyThrough.
How can I achieve that?
1. CmsPlanner
i. planner_id
ii. subject_id
iii. date_start
2. CmsSubject
i. subject_id
ii. topic_id
3. CmsTopic
i. topic_id
ii. topic name
In CmsPlanner model
public function subject(){
return $this->hasManyThrough(
'App\CmsTopic',
'App\CmsSubject',
'topic_id', 'topic_id', 'planner_id');}
In CmsPlanner controller
CmsPlanner::with('subject')->get();
Add this relation on CmsSubject
public function cmsTopic()
{
return $this->belongsTo('App\Models\CmsTopic', 'topic_id', 'topic_id');
}
then add following relation on CmsPlanner
public function cmsSubject()
{
return $this->belongsTo('App\Models\CmsSubject', 'subject_id', 'subject_id');
}
to get data
$cms_planner = CmsPlanner::with('CmsSubject')->where('id', $planner_id)->get();
'user' => $this->business->user
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')
)
);
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',
),
I have 2 tables
student_job_log->id,job_id,student_id,created_dt
student_info->id,user_id,first_name,last_name
I have made the relations in model(studentJobLog)
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(
'student_info'=>array(self::BELONGS_TO,'StudentInfo','id','joinType'=>'LEFT JOIN',
'select'=>'first_name, last_name')),
);
}
I got the error in this
what is the problem.
something is wrong in my relations that i dont know :(
I want the first_name and last_name from student_info plz help me...:(
Foreign key must be the one from you current model that is mapped to the StudentInfo
return array(
'student_info'=>array(self::BELONGS_TO,'StudentInfo','student_id',),
);
now use it like
echo $job->student_info->first_name . ', ' . $job->student_info->last_name;