U
U
Urvin2016-05-24 21:32:33
Yii
Urvin, 2016-05-24 21:32:33

Yii2 - how to process and output data?

Hello! Fate sent me to deal with yii2, which I did not know before, and the farther into the forest, the more terrible it becomes. The sea, kmk, incredible inheritances cause more disgust than the desire to use this framework.
Question 1. I have goods stored in the database, the goods have some final set of properties. These are three tables:

products:
-id
-name
-user /*принадлежность товара пользователю, см. ниже*/

properties
-id
-name

productproperties:
-id
-product
-property
-value

Initially, through Gii, I generated ActiveRecords for each of the tables, in the admin interface I also have CRUD handlers for the Products entities. Those. I can change the name =)
What should I do to display a property set on the create/view/edit pages? Obviously, the controller should not take data from ActiveRecord, but something more sophisticated. But what?
Question 2. Each user has their own products. Again the same story - generated in Gii CRUD. In the admin panel in the GridView, I want to see only "my" (logged in user) products. Is it okay to put a condition on user = \Yii::$app->user->identity->id in Products::find()?
Drink please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2016-05-24
@matios

1. Describe relays for AR models and display data in the form. It's not at all clear what the problem is. Read more here:
www.yiiframework.com/doc-2.0/guide-db-active-recor...
2. Yes, that's fine. But it's better to create a method that will search by user_id

public function getByUserId($id) {
    $this->where('user_id = ' . (int) $id);
}

Products::find()->getByUserId(Yii::$app->user->id)->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question