M
M
michaelromanov902017-02-11 21:36:31
Yii
michaelromanov90, 2017-02-11 21:36:31

How to make a request in Yii2 to the database?

Good afternoon.
I have a request (I need it in the builder, not in ActiveRecord)

return (new \yii\db\Query())
            ->select(['*'])
            ->from('profiles')
            ->where([
                'age' => $age
            ])
            ->andFilterWhere(['like', 'name', $name ])
            ->andFilterWhere(['like', 's_name', $s_name ])
            ->orderBy('id desc')->all();

For example, I will look for Frodo Baggins for 21 years, then everything will be ok!
But if the request is baggins frodo 21, then the result will be null.
those. Roughly speaking, a surname can come instead of a given name. I have not 2 fields in the form, but one (the user himself chooses in which order to write (first name or last name))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-02-11
@michaelromanov90

Good evening.
When you created a model, if you created it through gii, then you also had to create a model for searching.
Create this model and see how it's done.
Use andFilterWhere() like this:

$query->andFilterWhere(['like', 'username', $this->username])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question