select relations from MANY to MANY in yii framework using Criteria - php

I have a table Policy which links to another table Currency and the relation is as below:
'policyCurrency' => array(self::MANY_MANY, 'Currency', 'policy_currencies(PolicyId, CurrencyId)’),
I am also using the search function provided in the model to get the data and I want to select the Currency which resides in the related Currency Table as part of the search. Below is my search function.
public function search(){
$criteria = new CDbCriteria;
$criteria->compare('PolicyId', $this->PolicyId);
$criteria->compare('Name', $this->Name, true);
$criteria->compare('Amount', $this->Amount, true);
return new CActiveDataProvider($this, array( 'criteria' => $criteria));
}
When using the search getData:
$result = Policy->search()->getData();
The result returned is in the format of:
PolicyId = 1
Name = 'Test'
Amount = '20'
etc etc etc
I want the data related to the relations which is currency in this case be returned as part of the select like this (appending one after the other separated by a ',' since this is a MANY to MANY relation):
CurrencyId = 1,2,3
What I have tried:
Trying to use the
$criteria->with = array('policyCurrency');
$criteria->together = true;
but i am confused about how to use select to select all records from the first table Policy in my case and all related CurrencyIds seperated by a ',' from the related model.
Thanks

You need to add the relation name to the with attribute so it does a JOIN with Currency and gets all its related data:
public function search(){
$criteria = new CDbCriteria;
$criteria->with = array('policyCurrency');
For more info, check CDBCriteria documentation.

Related

Yii Get data from another model using Relation

I am trying to filter a table in my view using the search function in my model.
I have two models which i need to get data from model Evaluation to another model EvaluationDetails.
i have this in my EvaluationDetails model
public function relations() {
return array(
'eval' => array(self::BELONGS_TO, 'Evaluation', 'eval_id'),
);
}
i also have this in my search function
public function search($employee = '', $search_date_start = '', $search_date_end = '', $search = '') {
$criteria = new CDbCriteria;
$criteria->with = array('eval' => array('together' => true));
$criteria->compare('employee_id', $this->employee_id);
$criteria->compare('remarks', $this->remarks, true);
$criteria->compare('eval_id', $this->eval_id);
$criteria->compare('eval.evaluatee', $this->evaluatee_search);
$criteria->addSearchCondition('eval.evaluatee', $search);
if ($employee != '')
$criteria->compare('employee_id', $employee->company_id);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
I am trying to filter a table where in the user will search for a name, pass the value in $search which is then used in the search function in my EvaluationDetails model
With this, i am getting an error.
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]:
Integrity constraint violation: 1052 Column 'employee_id' in where
clause is ambiguous. The SQL statement executed was: SELECT
COUNT(DISTINCT t.id) FROM trx_evaluation_details t LEFT OUTER
JOIN trx_evaluation eval ON (t.eval_id=eval.id) WHERE
((eval.evaluatee LIKE :ycp0) AND (employee_id=:ycp1))
what seems to be the problem with my code. please help..
Seeing from the error message, it looks like both EvaluationDetails and Evaluation have a field call "employee_id" (i am not sure)
But if so, you need to replace the
$criteria->compare('employee_id', $employee->company_id);
with
$criteria->compare('t.employee_id', $employee->company_id);
To explicitly tell yii the field is from EvaluationDetails table.
BTW, u are using $employee->company_id (should be $employee->employee_id ?)

Advanced search is not functional for two fields

I am stuck with an issue regarding searching functionality.I am using PHP and Yii framework. I have a search option in my grid view which has 5 fields with drop down menu, 3 field in the search form are working only two fields are not working....I don't know why..I have written a search function in my model.
Model:
public function searchByManager($id)
{
$User =new User;
$criteria=new CDbCriteria;
$criteria->with=array('user');
$criteria->condition = "user.supervisor_id = $id";
$criteria->compare('user_id',$this->user_id);
$criteria->compare('job_title_id',$this->job_title_id);
$criteria->compare('cv_type',$this->cv_type,false);
$criteria->compare('review_status',$this->review_status,true);
$criteria->compare('is_current',$this->is_current,true);
$criteria->addInCondition("is_current", array("yes"));
//$criteria->condition = "cv_upload.is_current = yes";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'upload_date DESC'
)
));
}
user_id and job_title_id these two fields are not working...when I search by them it shows an error something like this:
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]:
Integrity constraint violation
Can anyone please help me out.

yii Criteria only one row in result

I want to show some Calculated Values in CGridView, but it only shows ONE row in CGridView
The function:
public $Name;
public $ calculatedSum;
public function listSum()
{
$criteria = new CDbCriteria();
$criteria->select = 'Name as Name,'
. 'sum((wert1-wert2-wert3-wert4)*24) AS calculatedSum,'
return new CActiveDataProvider ( $this, array (
'criteria' => $criteria));
}
When I delete the AS calculatedSum part of the query it shows all results, but with the AS calculatedSum part it only shows one result in CGridView. But I need the AS calculatedSum to show the calculated Field in the view.
Anybody have an idea for a solution ?
$criteria->select = 'Name as Name,'
. '((wert1-wert2-wert3-wert4)*24) AS calculatedSum,'
Don't use the sum function. That sums up all columns, and without group by (which also isn't needed) will always cause you to have just one row. All you need is simple arithmetic, as you have inside the parenthesis.

How to get the corresponding data in drop down from database in yii

I have a field in my form 'update_job' named as 'job_category',which is a dropdownlist and has been stored in main table as category_id,and the table is related to another table 'category'.
My first question is how to write the joint query to get the category as well:I have written code to get all data and it works fine:but how to write a join query to get the category as well?
//code//
public function actionDisplayJob()
{
if (isset($_GET['id'])) {
$id = $_GET['id'];
}
$model = DisplayJob::model()->find(array(
'select' => array('posted_by', 'title', 'key_skills'), "condition" => "id=$id"
));
$params = array('model' => $model);
$this->render('update', $params);
}
Second,What should I do to keep data selected in dropdown list from database while editng the data?
You can set relations in your model. Assuming you have done this, you can use 'with' to join the related model (tabel):
$model = DisplayJob::model()->with('category')->find(array(
'select' => array('posted_by', 'title', 'key_skills'), "condition" => "id=$id"
));
More information about using relations you can find here:
http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Table "tbl_category" does not have a column named "select"?

How to run 'OR' condition in model()->findByAttributes()?
This is my code:
$criteria->condition='url_title_en=:x OR url_title_da=:x';
$criteria->params=array(':x'=>$_GET['purl_title']);
$parentcat = Category::model()->findByAttributes($criteria);
But I'm getting this error:
Table "tbl_category" does not have a column named "select".
You can use OR like this
$criteria=new CDbCriteria();
$criteria->select='category_id';
$criteria->condition='url_title_en=:x';
$criteria->addCondition('url_title_da=:x','OR');
$criteria->params=array(':x'=>$_GET['purl_title']);
In above code addCondition appends the extra condition with OR operator. Now If you expect multiple records then you can use
$parentcat = Category::model()->findAll($criteria);
If you expect single record the you can use
$parentcat = Category::model()->find($criteria);
What you are using here that you are passing criteria to function FindByAttributes() which finds records with respect to attributes passed to it. Now you are passing it criteria and it finds first word as "select" because criteria translates your query like "select * where category_id=3". Now findByAttributes() take it as an attribute and tries to find it. thats why it shows error that tble_category cant find select.Here is official documentation of yii http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail
The only problem with your code was that you were using findByAttributes instead of findAll.
$criteria = new CDbCriteria;
$criteria->condition = '`url_title_en` = :uri OR `url_title_da` = :uri';
$criteria->params = array(':uri' => $_GET['purl_title']);
$records = Category::model()->findAll($criteria);
// print all records fetched.
print_r($records);

Categories