PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] Yii filter search working for one relation but not for other

 March 08, 2022     activerecord, cgridview, yii     No comments   

Issue

I am using Yii GridView filter and my data comes from Purcahses table. That tables realted to 1 .vendors 2 .clients Below are my relations in Purchases Model

 'vendor' => array(self::BELONGS_TO, 'Vendors', 'Vendor'),
 'client' => array(self::BELONGS_TO, 'Clientsinfo', 'clientID'),

and i am using that relations in my admin view , Below is code of admin.php file

$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'purchases-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
    array(
        'name' => 'Vendor',
        'value' =>'$data->vendor->VendorName',
    ),
    array(
        'name' => 'clientID',
         'value' =>'$data->client->Company'
    ),

    array(
        'name' => 'PurchaseType',
        'value' => 'Purchasetype::model()->findByPk($data->PurchaseType)->PurchaseType',
        'filter' => '',
    ),

),
));

and lastly here is model search function where is use with and together to get data from other tables

public function search() {
    $criteria = new CDbCriteria;
    $criteria->together = true;
    $criteria->with = array('vendor');
    $criteria->with = array('client');
    $criteria->compare('PurchaseType', $this->PurchaseType);
    $criteria->compare('vendor.VendorName', $this->Vendor, true);
    $criteria->compare('client.Company', $this->clientID, true);
   return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

Getting data from client successufully , but when i use vendor fitter it throw me error below

CDbException

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'vendor.VendorName' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT t.PurchaseID ) FROM purchases t LEFT OUTER JOIN clientsinfo client ON (t.clientID=client.ClInfoID ) WHERE ((Vendor=:ycp0) AND (vendor.VendorName LIKE :ycp1)) (C:\xampp\htdocs\laurenparker\framework \db\CDbCommand.php:543)

#0 C:\xampp\htdocs\laurenparker\framework\db\CDbCommand.php(433): CDbCommand-

queryInternal('fetchColumn', 0, Array)


Solution

Youre overriding relations in criteria. Change this:

$criteria->with = array('vendor');
$criteria->with = array('client');

To this:

$criteria->with = array('vendor', 'client');


Answered By - Yupik
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing