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.
Related
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 ?)
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.
I have two entities, Carros and Msg, i am looking to get Carros that have Msg messages
$query = $entityManager->createQuery("
SELECT u
FROM Auto\Entity\Carros u
JOIN Auto\Entity\Msg m WITH m.idautoad=u.idcarros
WHERE u.identidade='".$emailidentidade."'
ORDER BY u.datadoanuncio DESC
");
I'm using the paginator:
// Create the paginator itself
$paginator = new Paginator(
new DoctrinePaginator(new ORMPaginator($query))
);
and i am getting the following errors i have zend 2.3.9 and doctrine 2.4
Arquivo:
C:\websites\auto\vendor\zendframework\zendframework\library\Zend\Paginator\Paginator.php:637
Mensagem: Error producing an iterator
C:\websites\auto\vendor\doctrine\orm\lib\Doctrine\ORM\Tools\Pagination\WhereInWalker.php:85
Mensagem:
Cannot count query which selects two FROM components, cannot make distinction
its generating the error when i try to do this :
foreach ($paginator as $carro)
{}
The error disappears when getting the results like this :
$fi = $query->getResult();
and then
$paginator = new \Zend\Paginator\Paginator(new
\Zend\Paginator\Adapter\ArrayAdapter($fi)
);
In case you have a ManyToOne relationship you can do like this (be sure to see this link before to make your mapping correct : Bidirectionnal ManyToOne relation
public function getInvoices($idProformaGroup, $paginate = false)
{
$query = $this->getEntityManager()->createQueryBuilder();
$query->select('po', 'i')
->from('Invoice\Entity\Proforma', 'po')
->innerJoin('po.idInvoice', 'i')
->andWhere("po.idProformaGroup = :idProformaGroup")
->orderBy('po.idProforma', 'ASC')
->setParameter('idProformaGroup', $idProformaGroup);
if ($paginate) {
$doctrineAdapter = new DoctrineAdapter(new ORMPaginator($query, true));
return new Paginator($doctrineAdapter);
} else {
return $query->getQuery()->getResult();
}
}
Like you see, my join use the foreign key po.idInvoice to join the two tables. And because of this my Paginator don't show me your error.
EDIT : With a param I can decide to paginate or not. Don't use it if you don't need it.
EDIT 2 : From the link to the other question join before Doctrine : Pagination with left Joins The futurereal's answer is the same point I tried to explain to you.
i am not using anymore
new DoctrinePaginator(new ORMPaginator($query))
now i am using \Zend\Paginator\Paginator and its working
$fi = $query->getResult();
$paginator = new \Zend\Paginator\Paginator(new
\Zend\Paginator\Adapter\ArrayAdapter($fi)
);
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.
I have three table product , category , product_category
and in product model I add this relation
return array(
'categories' => array(self::MANY_MANY, 'Category',
'product_category(product_id, category_id)')
);
Now I want to use search by category if category select.
In product actionIndex i add this code
$criteria=new CDbCriteria;
if($_GET['name'])
{
$criteria->with = array('categories');
$criteria->together = true;
$criteria->addCondition('categories.name = :name');
$criteria->params = array(':name'=>$_GET['name']);
}
$dataProvider=new CActiveDataProvider('Product',array(
'pagination'=>array(
'pageSize'=>10,
),
'criteria' => $criteria
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
If i made search, categories loads in eager load, and has no categories other than searching.
How search Product by category, but in $data->categories has all categories, without loads them by second relation in view, how Andrey Vorobyev says?
Sorry for bad english
I found the answer
if i use $criteria->with = array('categories'=>array('select' => false)) and call $data->categories, it do lazy loading and everything is fine.
First, that i think, is make two same relations with different names: one for search, another for show.
Because, when we set condition, we filter post by category, and we can't get all categories.
But you will have one more relation, you can get all categories in lazy load, when you were print posts.