R
R
Raccoon Raccoons2016-01-18 00:18:59
Yii
Raccoon Raccoons, 2016-01-18 00:18:59

Whether there is a sense to study, write on ActiveRecord?

Started learning frameworks.
I was the first to take Yii2, tell me please, is there any point in ActiveRecord ... does anyone really use it?
At first I thought that this is a very cool thing that simplifies life.
But when I get to Join and complex queries, some kind of hell begins (
Still a student, is there any point in this AR, from the point of view of employment?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
R
romy4, 2016-01-18
@romy4

This is a very controversial thing. It can be used on simple queries, but if it comes to normal selections, then AR is aside and only native sql.

S
Sergey, 2016-01-18
Protko @Fesor

does anyone really use it?

More than enough, let's say. Like any fashionable technology, it is often used where it is not necessary and how it is not necessary.
It's just worth understanding that you are not limited to specific tools. Even if half of your project uses AR, for complex selections, you can do without it, simplifying your life.

I
index0h, 2016-01-18
@index0h

Well, if it were my will, I would throw out all the constructors and write to $connection-> createCommand

What too much is not healthy.
It makes sense to write native SQL where it is required.
True, only the constructor is not so often used. You asked about AR here, the query builder is a completely different story.
In the same Yii2, your SQL will look like this:
If you don't use it out of principle, you will. If you do not use where AR imposes too high costs, they will not.
Use it, and actively. I would really recommend Entity-Repository instead of AR, but the latter also has the right to life.

N
Nikita, 2016-01-18
@bitver

You are confusing ActiveRecord with QueryBuilder. AR is a design pattern that implements data access.
Complex queries can be written using pure SQL and using AR, for example from the docks:

// returns all inactive customers
$sql = 'SELECT * FROM customer WHERE status=:status';
$customers = Customer::findBySql($sql, [':status' => Customer::STATUS_INACTIVE])->all();

PS JOINs are very conveniently formed through dependencies (Relations), after which data access (hello AR) is performed in a completely logical way by the OOP when an entity is accessed as an object. From here, the dependent object also has the opportunity to call its methods, which is very convenient in every sense.

E
Enot, 2016-01-18
Enotov

Well, if it were my will, I would throw out all the constructors and write to $connection->createCommand. I'm
just interested in questions from the point of view of the subsequent interview and teamwork, as a junior
. As for me, 2 lines of a native query are more convenient to read than 10 lines of a constructor.
I'm just interested in the moment, will they be beaten on the hands for not using AR?
Queries like this:
select * from products
LEFT JOIN products_categories ON products.id = products_categories.id_products
LEFT JOIN categories ON products_categories.id_categories = categories.id
Is this considered a complex query?
It’s just that I’ll write this in 10 seconds, and on AR it’s 10 times further to design.

A
Andrey, 2016-01-18
@VladimirAndreev

AR speeds up development.
for example, you have a complex system, well, let it be the sale of cars, in which the site, api for mobile platforms, some other crap.
You choose them (cars) in dozens of places.
And then the task comes to you: add the "spam" flag to the machines and ignore everything marked with this flag in all-all-all select requests.
On the native, you will add this condition in dozens of places.
in AR you will fix one place...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question