Yii2 how can I use a like query for first character only

con322

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,
    ]);
} 
soju

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 name LIKE '%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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How Can i use yii2 query along with the normal query

From Dev

How can I set connection for Query builder in yii2?

From Dev

How can I get sed to only match to the first occurrence of a character?

From Dev

Like Query not working when I Use "?" character

From Dev

Like Query not working when I Use "?" character

From Dev

Yii2 How can i set the db config in new Query without use all(), one() or other execution method?

From Dev

How can I use the Like Operator with a Parameter in a SQLite query?

From Dev

how can I use like query in ruby with sinatra?

From Dev

how can I use like query in ruby with sinatra?

From Dev

How can I do something like 'categoryname/seoname' in yii2?

From Dev

How to create & group OR LIKE query with multiple columns in Yii2?

From Dev

How can I print the address of first character?

From Dev

How can I select only the first occurance of a specific character using regex?

From Dev

How can I use a simple Dropdown list in the search box of GridView::widget, Yii2?

From Dev

How can I use a simple Dropdown list with filtering in the search box of GridView::widget, Yii2?

From Dev

Yii2 How can i use activequery for unique validators in my model

From Dev

Yii2: How to use integer only in rules

From Dev

Yii2: How to use integer only in rules

From Dev

In yii2 query builder how to use in operator with where clause

From Dev

How to use query caching in yii2 ActiveRecord

From Dev

How to use not equal to inside a Yii2 query

From Dev

How to use query caching in yii2 ActiveRecord

From Dev

Yii2: How do I write the below Active Query?

From Dev

How can I use a '#' character in a git command?

From Dev

Yii2 Basic - How I use common function functionality in yii2 action

From Dev

How can I change the results of a field in a SQL query to only include characters up to the first underscore?

From Dev

yii2:- how can i manage angularjs post request in yii2

From Dev

yii2:- how can i manage angularjs post request in yii2

From Dev

How can I use something like tail -f for a long taking process that only writes to a logfile in batch?

Related Related

  1. 1

    How Can i use yii2 query along with the normal query

  2. 2

    How can I set connection for Query builder in yii2?

  3. 3

    How can I get sed to only match to the first occurrence of a character?

  4. 4

    Like Query not working when I Use "?" character

  5. 5

    Like Query not working when I Use "?" character

  6. 6

    Yii2 How can i set the db config in new Query without use all(), one() or other execution method?

  7. 7

    How can I use the Like Operator with a Parameter in a SQLite query?

  8. 8

    how can I use like query in ruby with sinatra?

  9. 9

    how can I use like query in ruby with sinatra?

  10. 10

    How can I do something like 'categoryname/seoname' in yii2?

  11. 11

    How to create & group OR LIKE query with multiple columns in Yii2?

  12. 12

    How can I print the address of first character?

  13. 13

    How can I select only the first occurance of a specific character using regex?

  14. 14

    How can I use a simple Dropdown list in the search box of GridView::widget, Yii2?

  15. 15

    How can I use a simple Dropdown list with filtering in the search box of GridView::widget, Yii2?

  16. 16

    Yii2 How can i use activequery for unique validators in my model

  17. 17

    Yii2: How to use integer only in rules

  18. 18

    Yii2: How to use integer only in rules

  19. 19

    In yii2 query builder how to use in operator with where clause

  20. 20

    How to use query caching in yii2 ActiveRecord

  21. 21

    How to use not equal to inside a Yii2 query

  22. 22

    How to use query caching in yii2 ActiveRecord

  23. 23

    Yii2: How do I write the below Active Query?

  24. 24

    How can I use a '#' character in a git command?

  25. 25

    Yii2 Basic - How I use common function functionality in yii2 action

  26. 26

    How can I change the results of a field in a SQL query to only include characters up to the first underscore?

  27. 27

    yii2:- how can i manage angularjs post request in yii2

  28. 28

    yii2:- how can i manage angularjs post request in yii2

  29. 29

    How can I use something like tail -f for a long taking process that only writes to a logfile in batch?

HotTag

Archive