Do I need a model in yii2 in this case?

Gave the test job on yii2. So far everything seems clear, as previously studied Laravel. But I, for one, do not want to use Active Record. I found it easier to write native SQL queries using PDO. It turns out that in this case, I don't need? And can not take the job, if I say that I prefer using SQL queries, as in Active Record you can get confused?
June 8th 19 at 17:05
4 answers
June 8th 19 at 17:07
If you refuse and AR models, then the data will be validated either by themselves or won't at all. Plus I don't quite understand how you can get lost in AR. One object - one row in the table.
If I interview someone said that would work using PDO, then immediately went on in search of work.
and how to protect against sql injection? in pdo I know. - Gust.Rath commented on June 8th 19 at 17:10
first , I deliberately mentioned validation. Second, models in yii are able to test the field for compliance with the specified rules. In the third, yii, just like in PDO automatically escapes variables. - daron commented on June 8th 19 at 17:13
that is, there is no need to write, type :name? All automatically screened? Just when I watched the video, where the narrator said that just to write the query is unsafe, and he used a colon before the field name from the table - Gust.Rath commented on June 8th 19 at 17:16
you do not write SQL when working with AR. Find the model, update the attribute, call save() and change is already in the database. - daron commented on June 8th 19 at 17:19
The documentation is all there:
Besides just AR uses PDO and already has protection. I think that not less than will make You handles. But again at the docks all described. Can disassemble an AR class and see themselves, as there are queries and how safe they are. - Henderson_Beat commented on June 8th 19 at 17:22
June 8th 19 at 17:09
I don't think you'll take to the post Yii2 programmer, if you everywhere instead of the native AR of the models you will use your own classes with direct queries via PDO (why?).
This is how to replace a computerized wheel of a race car F-1 steering wheel from the ship and expect that you will be ineligible to compete.
June 8th 19 at 17:11
It turns out that in this case, I don't need?

The model is an integral part of the MCV. If you are asked to do in yii, we expect the presence of MCV.

But I, for one, do not want to use Active Record.

Your right, unable even to put yii, and next to write all without using it. A lot of projects I've seen. But if it is Your project. And if you have a customer and it requires yii, then he probably expects to see AR. So the question is not at the address You the customer ask. Especially if it's a test. Then checked Your knowledge of yii. So in this case AR I think need to use. As with other methods and objects framework-a.

since Active Record can be confusing

The only argument to abandon the AR's performance. The rest, especially "can be confusing" is a valid reason not to hire You. As he made for simplicity and readability. And confusion can only be due to ignorance.
It is necessary first to study AR, and then to abandon it if necessary. But for tests, if you want to show knowledge of pdo, it is better to implement both and show the difference. That would be a plus. If you do without AR because You don't know the answer of the employer is obvious.
I just see that many criticize AR type starts to slow and suggest to use pure SQL. But the employer, probably, it is necessary to show the knowledge and have to do it through AR. - Gust.Rath commented on June 8th 19 at 17:14
I just see that many are criticizing AR

Who? Where do You find them? I usually see those who praise or those who do not understand.

type starts to slow

The PLO, in principle, slower, pdo, too, is not for speed did. When you direct hands losses are minimal, and the convenience and readability of the code is incomparable

suggest pure SQL

Then pure php without any framework s there any. And it is better for si to write. After all, php is also not the fastest JAP. - daron commented on June 8th 19 at 17:17
AR criticize, but criticize AR for not speed — it is on the contrary with a quick pattern for ORM - Gust.Rath commented on June 8th 19 at 17:20
the fastest pattern, but not faster than pure pdo. We do not compare with other patern. The author wants to throw AR, for the sake of pure pdo - daron commented on June 8th 19 at 17:23
Yes, you are right :) but still not at the speed criticize the AR — my main idea :) - Henderson_Beat commented on June 8th 19 at 17:26
Yes, You're right. Although I use and enjoy. - Gust.Rath commented on June 8th 19 at 17:29
, well, it's true happy nice brain and work thing
it goes sideways with a rather individual situations - Henderson_Beat commented on June 8th 19 at 17:32
June 8th 19 at 17:13
Explain popularly:
  • is PDO
  • above it there is a wrapper (that is already in use PDO anyway) called DAO
  • over DAO wrap Active Record that uses all that is needed for the previous 2 layers and automatiseret much for you — meaning you do not need to escape everything and Bendit also does not need anything, everything is already there under the hood

Active Record
For normal use — AR plenty if you want to do complicated selections or you begin to clean your model, you can always go down and gash your query through DAO (it's also stui PDO, but with a more convenient interface)

To balobolit suggest to read the manuals from the team Yii2:

I want to note that Active Record is considered to be the most simple to study, actually in the world of web development is ruled a ball Active Record and Data Mapper(Doctrine) (excluding net queries)... is to examine and feel both for 2-3 days each, and to understand what, where and how... that will give you bonuses in Yii2 AR are actually not separated from the project...
Active Record uses Ruby on Rails (from there and came to the PHP world), used in Yii2 and Laravel (Eloquent is built on this pattern), Data Mapper is used mostly in the world of Symfony and Laravel again

The ability to do net queries is appreciated much more than the ability to work with AR — so feel free to go to work or do them in the test... adequate team lead will cast a spell.

Find more questions by tags Yii