How can I use a like query and limit to the first character only for example my code below works however it does it for all characters for example if an item was called end it would appear in e, n & d rather than just e the starting character/letter.
Current Query/Code
public function actionAlphabet($id)
{
$this->layout = 'directory';
$query = Directory::find()
->where(['LIKE', 'name', $id]);
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
You should simply try :
$query = Directory::find()
->where(['LIKE', 'name', $id.'%', false]);
Sometimes, you may want to add the percentage characters to the matching value by yourself, you may supply a third operand false to do so. For example,
['like', 'name', '%tester', false]
will generate nameLIKE '%tester'
.
Read more : http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where()-detail
EDIT: my mistake, updated thanks to Latikov Dmitry comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With