I have a table called TPurchasing which contain data about a store's purchasing record.
TPurchasing
id | serial
transaction_time | timestamp without time zone
supplier | character varying(200)
total | double precision
paid | double precision
By using gii feature in yii2, I manage to generate a model called TPurchasingSearch from "CRUD Generator" menu. In the TPurchasingSearch, there is a function called search() which is used by the GridView in the view file.
public function search($params)
{
$query = TPurchasing::find();
$dataProvider = new ActiveDataProvider([
'pagination' => ['pageSize' => 20],
'query' => $query,
'sort' => ['defaultOrder' => ['transaction_time' => SORT_DESC]]
]);
$this->load($params);
if (!$this->validate()) {
$query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere(['is_removed' => 0]);
$query->andFilterWhere(['like', 'supplier_name', $this->supplier_name])
->andFilterWhere(['like', 'payment_type', $this->payment_type])
->andFilterWhere(['>', 'total', 'paid']);
return $dataProvider;
}
Right now I'm trying to display records that is still not totally paid off, which means the "paid" column is smaller than "total" column. How can I do this comparison in the search function? The above code gave me an error:
invalid input syntax for type double precision: "paid"
on this part
->andFilterWhere(['>', 'total', 'paid']);
since it tried to compare total column with string "paid". I might missed it, but I can't seem to find the related answer in Yii2 documentation.
No need to use andFilterWhere() here, you could simply try :
$query->andWhere('total > paid');
Related
I am trying to filter by a calculated column "age". I have tried the following:
public $age;
public function search($params)
{
$query = Profile::find();
$query->select = ['*', 'age' => 'TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE())'];
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere([
'between', 'age', $this->age_min, $this->age_max,
]);
return $dataProvider;
}
But I am getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'age' in 'where
clause' The SQL being executed was: SELECT COUNT(*) FROM profile
WHERE age BETWEEN '41' AND '62'
So I think what's happening is, it is trying to retrieve a count of matching records (for the ListView widget) but it's not taking in to account my custom "select" statement.
Does anybody know how to do this?
Actually you should use:
$query->andFilterWhere([
'between',
new Expression('TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE())'),
$this->age_min,
$this->age_max,
]);
There is no reason for use HAVING here, and it may create problems if you use it on query without GROUP BY clause.
I found the answer. I need to use andFilterHaving() instead of andFilterWhere() if I want to query a calculated column.
In table I have a columns where data like 2016-12-13 10:51:03 which output in gridview. I put datepicker
[
'attribute'=>'order_statusUpdatedAt',
'value'=>'order_statusUpdatedAt',
'format'=>'raw',
'filter'=>DatePicker::widget([
'model'=>$searchModel,
'attribute'=>'order_statusUpdatedAt',
'clientOptions'=>
[
'autoclose'=>true,
'format'=>'yyyy-mm',
]
]),
],
I want to search all lines which have like 2016-12. How could I search for partial matches?
OrderSearch
`public function search($params)
{
$query = Order::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'order_createdAt' => $this->order_createdAt]);
return $dataProvider;
}
After your grid conditions add this bit of code.
$query->andFilterWhere(['like','order_createdAt', $this->order_statusUpdatedAt]);
the third condition should be the attribute where you have specified in your search field attribute. So the search data you entered in the search field will be like the data from your database, i.e. 'order_created' in your case.
Hope this helps you..
please help I dont know how to get relation table when using sqldataprovider. Anyone understand how to use relation model?
$model = new Finalresult();
$searchModel = new FinalresultSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider = new SqlDataProvider([
'sql' => 'SELECT finalresult.bib,
finalresult.series_id,
finalresult.category_id,
GROUP_CONCAT(DISTINCT finalresult.point ORDER BY series.serie_seri_no DESC) AS seriPoint
FROM finalresult, series GROUP BY finalresult.bib',
'key' => 'bib',
]);
I'm trying to get relation table:
'attribute'=>'category_id',
'width'=>'300px',
'value'=>function ($model, $key, $index, $widget) {
return $model->category->category_name;
},
then getting trying to non-object
You can't use relations with SqlDataProvider, because each single result will be presented as array, for example:
[
'id' => 1,
'name' => 'Some name',
'category_id' => 1,
],
For example, you can access category_id as `$model['category_id'].
SqlDataProvider is for very very complex queries, your query can easily be written as ActiveQuery and you can use ActiveDataProvider and get all advantages of that (relations, etc.).
You can find category by id, but it will be lazily loaded that means amount of queries is multiplied by number of rows.
With ActiveDataProvider and relations you can use eager loading and reduce amount of queries. Read more in official docs.
Grid Columns example in documentation
try to change "value" to
'value'=> function($data) {
return $data['category']['category_name'];
}
I am generating related records search query for Gridview use
I get this error :
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dbowner' in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM tbl_iolcalculation LEFT JOIN tbl_iolcalculation patient ON tbl_iolcalculation.patient_id = patient.id WHERE (dbowner=1) AND (dbowner=1)
I have two related models 1) iolcalculation and patient - each iolcalculation has one patient (iolcalculation.patient_id -> patient.id)
The relevant code in my model IolCalculationSearch is :
public function search($params)
{
$query = IolCalculation::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['patient.lastname'] = [
'asc' => ['patient.lastname' => SORT_ASC],
'desc' => ['patient.lastname' => SORT_DESC],
];
$query->joinWith(['patient'=> function($query) { $query->from(['patient'=>'tbl_iolcalculation']); } ]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'patient_id' => $this->patient_id,
'preop_id' => $this->preop_id,
'calculation_date' => $this->calculation_date,
'iol_calculated' => $this->iol_calculated,
The reason this error is generated is that each model has an override to the default Where clause as follows, the reason being that multiple users data needs to be segregated from other users, by the field dbowner:
public static function defaultWhere($query) {
parent::init();
$session = new Session();
$session->open();
$query->andWhere(['t.dbowner' => $session['dbowner']]);
}
this is defined in a base model extending ActiveRecord, and then all working models extend this base model
How Can I resolve this ambiguous reference in the MySQL code?
Thanks in advance
$query->andFilterWhere([
// previous filters
self::tableName() . '.structure_id' => $this->structure_id,
// next filters
]);
I think, that you are searching for table aliases.
(https://github.com/yiisoft/yii2/issues/2377)
Like this, of course you have to change the rest of your code:
$query->joinWith(['patient'=> function($query) { $query->from(['patient p2'=>'tbl_iolcalculation']); } ]);
The only way I can get this to work is to override the default scope find I had set up for most models, so that it includes the actual table name as follows - in my model definition:
public static function find() {
$session = new Session();
$session->open();
return parent::find()->where(['tbl_iolcalculation.dbowner'=> $session['dbowner']]);
}
There may be a more elegant way using aliases, so any advice would be appreciated - would be nice to add aliases to where clauses, and I saw that they are working on this....
I am trying to learn opencart structure, and trying to create a new column under the table product. The new column is "test"
Then I try to retrieve the data under this page index.php?route=checkout/cart (replace price with test column)
catalog\controller\checkout\cart.php
...
$this->data['products'][] = array(
'key' => $product['key'],
'thumb' => $image,
'name' => $product['name'],
'model' => $product['model'],
'option' => $option_data,
'quantity' => $product['quantity'],
'stock' => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')),
'reward' => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''),
'price' => $product['test'], //<-- new column
'total' => $total,
'href' => $this->url->link('product/product', 'product_id=' . $product['product_id']),
'remove' => $this->url->link('checkout/cart', 'remove=' . $product['key'])
);
The problem is I'm not getting any output, and I'm not sure how to work with the model. Which query/function is related with this page ?
The problem is that the $products that are available at cart.php controller are retrieved from the session where they have been stored in previously set structure, so there is no test index and You should get a Notice: undefined index 'test' in .... The $products are retrieved by
foreach ($this->cart->getProducts() as $product) {
//...
}
See /system/library/cart.php and method getProducts() to understand what I am speaking about.
If You would like to use this at catalog/controller/product/category.php or catalog/controller/product/product.php controllers, the code You are trying will work.
If You replace the price within all product lists and product detail, these controllers:
product/
category.php
manufacturer_info.php
product.php
search.php
special.php
module/
bestseller.php
featured.php
latest.php
special.php
with Your value, the final price within cart would be Your test value.