📜 ⬆️ ⬇️

Pending Yii2 2.0.4

As you probably guessed, I want to talk about Yii2 and what awaits newbies in a startup who chose this framework. It's no secret that many frameworks use symfony components in order not to reinvent the new bike. Many, but not Yii, but at the same time Yii borrowed a lot from symfony and absorbed the best from Ruby on Rails. On github in issues, you can often see a suggestion for expanding the functionality taken from these frameworks. In this article, I would like to describe exactly those moments in which the Yii developers do not agree with the developers of other frameworks.

ActiveRecord


AR is a model that represents 1 entry in the repository, but few people know that in Yii the finder also hides behind the static find methods and is called ActiveQuery.

ActiveQuery


Consider the example of User :: find () -> andWhere (['username' => 'ok']) -> one ().

At first glance, everything is logical, apart from the fact that User :: findByUsername ('ok') is used in the box in the advanced template. But if in the first case we can get rid of the User class, passing, for example, to the constructor $ query = User :: find () -> andWhere (/ * something else * /), then in the "boxed" version you need to add a condition extend the User class and override the work logic in the findByUsername method.
')
It is worth noting that ActiveQuery is tied to a model class, and if you want to add a default scope, then you need to expand from the model and block the above-mentioned find method:

public static function find() { return parent::find()->andWhere(['status' => 'active']); } 

To impose a default condition on ActiveQuery, we need to extend ActiveRecord.

Rules or validation


The guide on Ruby on Rails on this account says the following:

1.1 Why Use Validations?

Validations are saved to your database.

yiisoft / core-developers do not think so, and believe that you only need to validate incoming data from the user in the form, and everything else is a problem in the hands of the programmer, i.e. you have to catch the pdo exception yourself, if something suddenly went wrong - don't wait for validation errors. Therefore, in the box the minimum data is checked before writing to the database. Perhaps this is due to savings in performance:
- all validators are created immediately when the model is initialized, regardless of the script;
- render input type = text (on matches).

By the way, about performance. Tests posted on the Internet were performed with a minimum number of Yii code, that is, now the picture will clearly be different for the worse.

Safety or performance - everyone decides for himself. In the box advanced app - performance.

But even choosing security, you can safely forget about “boxed behaviors for attributes” that are tied to the beforeInsert and beforeUpdate events, and there are only one beforeValidate events, or you need to upgrade the value property with checking $ event-> sender-> isNewRecord.

Also, when choosing security, you should forget about the link method, which should link two models together by foreign key. This is due to the fact that the link method saves the model after the "re-routing" and this behavior is inevitable, and the saving goes without validation , therefore the above-mentioned behaviors for attributes will not be worked out and we will get the exception pdo that the field is required to be filled. We cannot validate before the link method, because for foreign keys there is a validator exist that will fail, or we must do selective validation, and it is necessary to pass a white attribute list, and when adding a new attribute to the model, be ready to shovel a bunch of code.

Safety or buns out of the box - you decide.

ActiveRelation


Here we will talk about the connection between models or the concept of a module in Yii.
As is known, the module is a self-sufficient unit of application. Consider an example: by connecting 2 User and BankAccount modules, we must link the User and UserAccount models together. I will leave this to you as homework, as the developers themselves cannot yet give an answer to this. That is why it seems to me, User and Rbac are in the yii box, and not in the form of extensions. For now, let's take a look at what symfony has come up with for a long time.

Those who seriously decide to do the “homework”, it is necessary to take into account that the models cannot be configured for a number of reasons, one of them is to create the model as keyed .

If you decide to "homework", try to solve it again, but not blocking both models from the modules.

Let's say amicably, modules are unnecessary.

On this article will be considered complete. In the appendage below there will be a few more bonuses, but first I want to say that I have been developing on this framework for more than 5 years (I started with yii 1.x) and I like it with my ability to configure components, but in yii2 more and more is tied to Closure, which does not allow to configure as flexibly as we would like. Perhaps this is due to the fact that there are many Laravel fans in the community? I would also like to thank the developers of yii2 for their contribution and work, the first pancake is always a lump, the second is better.

Bonuses


- In all programming languages, when calling an event, you can pass parameters to it, in Yii, parameters must be passed when the handler is hung ;
- It happens and Vika teaches "bydlokodit";
- Mystic aliases (https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseFileHelper.php#L135).

Love Yii, help the community. If your issue is immediately wrapped up - do not worry. It is possible that someone later will create a similar one and will not wrap it (there were also such cases). All clean code, clear comments in it and no errors in the tests.

PS I know that some of the developers of yii visit this resource, I would like to apologize to them. Maybe I lied somewhere - I will be glad to any comments below.

UPD: The proposed version of zelenin habrahabr.ru/post/254179/#comment_8348781 has the right to life and even quite a worker, as for me this strange behavior of the container di. But thanks to this behavior, I think it will turn out to make friends 2 models from different modules - later I will write about the results.

Source: https://habr.com/ru/post/254179/


All Articles