D
D
DragonChris2017-06-16 12:52:02
MySQL
DragonChris, 2017-06-16 12:52:02

How, in my case, to do a search in a CRUD application on yii2?

I write CRUD Applications. I want to do field search with join.
My code in the model

public function getIdVacancy() {
                return $this->status->id_vacancy;
            }

And my code is in search model, it works
$query->joinWith(['status' => function ($q) {
                $q->where('status.id_vacancy LIKE "%' . $this->idVacancy . '%"');
            }]);

This is my value query in model(problem)
public function getVacancyNumber()
        {
            return $this->status->vacancy->code;
        }

I don't know how to make a query to search for this field in the search model.
I wrote this, but it doesn't work. I can't figure out how to do it
<code>    $query->joinWith(['status' => function ($q) {
                   $vacancy_id = \backend\models\Vacancy::find()->where( 'title LIKE "%' . $this->titleVacancy . '%"')->one()->id;
                    $q->where('status.id_vacancy LIKE "%' . $vacancy_id . '%"');
                }]);</code>

Tell me at least where to think)
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-06-16
@DragonChris

Is the relation Status declared in the model? + Is the Vacancy relation declared in the Status model?
If yes, then you can do this:

$query->joinWith(['status s', 'status.vacancy v'])->where([
    'like',
    's.id_vacancy',
    $this->idVacancy
])->andWhere([
    'like',
    'v.title',
    $this->titleVacancy
]);

By the way:
1. Do you store identifiers as strings? or where like here?
2. $q->where('status.id_vacancy LIKE "%' . $vacancy_id . '%"'); - Congratulations! Have you just made a hole in the security of the site, have you heard about SQL-injection?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question