I am new to yii1. I would like to convert this below query to yii2. May I know the difference between condition and param in yii2?
$criteria = new CDbCriteria();
$criteria->select = 'amount, name, level';
$criteria->condition = "account_type = :account_type";
$criteria->params=(array(':account_type'=>'credit'));
$result = $this->model()->find($criteria);
I have tried the below sample. But it's not giving me the same return as yii1 query.
$result = Model::find()
->select(['amount, name, level'])
->where(['account_type' => $account_type, 'account_type' => 'credit'])
->one();
Try this:
$result = Model::find()
->select(['amount, name, level'])
->where(['account_type' => 'credit'])
->one();
Related
Hi I am new to Zend Framework. In normal php I know how to write queries. But in Zend Framework this is totally different.
I have written the query with left join and it is working fine but the thing is I want to get count of the joined table id. I will show my controller code:
controller:
$adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$projectTable = new TableGateway('blog', $adapter);
$rowset = $projectTable->select(
function(Select $select) {
$select->join(
array('b' => 'blog_answers'),
"b.question_id = blog.id",
array('new'=>COUNT('b.question_id'),'left')
);
}
);
while running this i am getting can you please clarify this how get the count.
HI this is the correct Query i got.
`
$adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$projectTable = new TableGateway('blog', $adapter);
$rowset = $projectTable->select(function(Select $select) {
$select->columns(array(
'*',
'num' => new Expression('COUNT(blog_answers.question_id)')
));
$select->join('blog_answers', 'blog.id = blog_answers.question_id', array(), 'left');
$select->group('blog.id');
});`
Am performing a find() in yii2 I understand that there can be andwhere but what of orWher
I have tried
$query = Tblpr::find()->where(['PRID'=>2])->andwhere(['new'=>1])->all();
How can i implement orWhere
Using Where OR
$query = Tblpr::find();
$query->andFilterWhere(['or',
['PRID',2],
['new',1]
])->all();
OR
$query = Tblpr::find()->select('*')
->orWhere(['PRID'=>2,'new'=>1])->all();
You can also use createCommand
$query = (new \yii\db\Query())
->select('*')
->from('Tblpr') // put your table name here
->where(['PRID'=>[2]])
->orWhere(['new'=>[1]]);
$command = $query->createCommand();
print_r ($command->sql);die;
The following should work for a query with ->where() and ->orWhere()
$query = Tblpr::find()
->where(['PRID' => 2])
->orWhere(['attribute' => 'value'])
->all();
I am learning Yii2, when I do listing with custom query I am getting error:
The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.
I am getting data from query but when I put in dataprovider it is giving me error. Here is my code:
public function search($params) {
//$query = User::find();
$query = new \yii\db\Query;
$query = $query->select(['user.*','tbl_region.id','tbl_region.regionName'])
->from('user')
->join('LEFT JOIN','tbl_men_reg_info','tbl_men_reg_info.userID = user.id')
->join('LEFT JOIN','tbl_women_info','tbl_women_info.userID = user.id')
->join('LEFT JOIN','tbl_region','tbl_men_reg_info.region = tbl_region.id');
$command = $query->createCommand();
$query = $command->queryAll();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
}
Can anyone please tell me how to do listing with custom query?
The error is kind of clear, you need to pass instance of yii\db\Query to data provider, so remove these lines:
$command = $query->createCommand();
$query = $command->queryAll();
and it should work.
Note that you also need to use SqlDataProvider instead of ActiveDataProvider, since models are not involved.
Also modifying variable with different type and meaning is considered bad practice:
$query = $command->queryAll();
It's not $query at this point, it's better to rename it for example as $rows or $results.
This fails when I use $currentUser->id in my CollectionController.php
$criteria = new CDbCriteria();
$criteria->condition = 'tbl_userId=:tbl_userId';
$criteria->params = array(':tbl_userId' =>$currentUser->id);
$dataProvider=new CActiveDataProvider('Collection', array('criteria'=>$criteria));
But if I just plug a literal in the params array my query comes back correct.
$criteria = new CDbCriteria();
$criteria->condition = 'tbl_userId=:tbl_userId';
$criteria->params = array(':tbl_userId' => 12);
$dataProvider=new CActiveDataProvider('Collection', array('criteria'=>$criteria));
What's the quickest Yii-way to verify my var? $currentUser->id
I'm new to PHP / Yii... Thanks for your help.
echo out $currentUSer->id from the controller. Don't execute the query that is failing and the view should render with what $currentUser->id is. Nothing Yii style, but it's a quick way to find out what if anything the $currentUser is returning
I have two tables menus and lang_menus. My Menus model is as follows :
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(
'menulanguages'=>array(self::HAS_MANY, 'MenuLangs', 'menuId'),
);
}
...
public function getMenus(){
$criteria = new CDbCriteria();
$criteria->condition = "t.clientId = ".Yii::app()->user->clientId." AND menulanguages.languageId = ".Yii::app()->user->userlanguage;
$count = Menus::model()->with('menulanguages')->count($criteria);
$pages=new CPagination($count);
//Results per page
$pages->pageSize=10;
$pages->applyLimit($criteria);
$menus = Menus::model()->with('menulanguages')->findAll($criteria);
return array('menus' => $menus, 'paging' => $pages);
}
This is throwing the error Unknown column 'menulanguages.languageId'.
The error is in the line $menus = Menus::model()->with('menulanguages')->findAll($criteria);.
Surprisingly I am getting the value of the variable $count correctly.
On looking at the log I can see that the SQL query that is running for the findAll query is :
SELECT `t`.`id` AS `t0_c0`, `t`.`clientId` AS `t0_c1`, `t`.`restaurantId` AS `t0_c2` FROM `posif_menus` `t` WHERE (t.clientId = 1 AND menulanguages.languageId = 2) LIMIT 10
which means the join has not taken place. Whereas proper join query is running for the count value. Am I doing something wrong ?
Please help.
Just use together() of CActiveRecord or together property of CDbCriteria:
$menus = Menus::model()->with('menulanguages')->together()->findAll($criteria);
or:
$criteria = new CDbCriteria();
$criteria->condition = "t.clientId = ".Yii::app()->user->clientId." AND menulanguages.languageId = ".Yii::app()->user->userlanguage;
$criteria->together=true;
Try this.. apply joining table in cdbcriteria..
public function getMenus(){
$criteria = new CDbCriteria();
$criteria->condition = "t.clientId = ".Yii::app()->user->clientId." AND menulanguages.languageId = ".Yii::app()->user->userlanguage;
$criteria->with = array('menulanguages');
$count = Menus::model()->count($criteria);
$pages=new CPagination($count);
//Results per page
$pages->pageSize=10;
$pages->applyLimit($criteria);
$menus = Menus::model()->findAll($criteria);
return array('menus' => $menus, 'paging' => $pages);
}