Pure SQL, Active Yii, or Mixed

Trying to figure out the advantages or disadvantages of different approaches. Actually, they are not looking for opinions, but rather that these different approaches provide or limit.

Yii claims that using their ARs makes it easier to program the database, but I'm more worried about the database being β€œhooked” with my code. I obviously know that they work together, but mapping is causing some concern. I want all the restrictions and limitations to exist when working with the database and wonder if it is possible to avoid pure SQL in general to limit performance.

+4
source share
2 answers

ActiveRecord is good for any system with a fairly straightforward ORM, where Model == Table, with a ratio of 1 to 1. In other words, if all your data objects fit into separate tables (possibly with a few additional tables associated with them), ActiveRecord is for you . And you can usually create your own software, so it is!

But you are right, it "interferes" with the database implementation and the code is a bit. It is assumed that Model == Table, which means that the database implementation is NOT separate from your code. ActiveRecord is slightly reduced if you have complex models stored in multiple tables. But this is more likely an argument involved. I will not go into it. Other templates, such as the Data Mapper , give you more flexibility, but they require more work for the source code.

ActiveRecords sets the value of it-and-forget-it. So easy and fast. There is no tedious coding of getters and setters, just beautiful PHP objects created from your tables. I recently created a fairly large application with Yii, and I like the speed and simplicity of ActiveRecord. I am currently working on an application in Zend, and although I can see how their method gives you more control, I miss the ease of AR .;)

+5
source

Yii ActiveRecord definitely makes programming easier with regards to the database, as of course, any decent implementation of ActiveRecord does.

Display on db

Regarding the mapping between the database and your models, there is no problem that your code will β€œcapture” your database. The only thing you need to constantly support is the following:

/** * @return string the associated database table name */ public function tableName() { return 'my_table'; } 

Seeing that the table names will not change frequently does not have practical significance.

There are other parts of your ActiveRecord models that should stay in sync with your circuit:

  • Phpdoc comments describing model class properties
  • Other public functions that implement various bits of functionality, such as validation rules [ public function rules() ], external model relations [ public function relations() ], attribute labels [ public function attributeLabels() ], etc.

However, you can still use the models and the database itself, even if they do not reflect your current database schema - just that the corresponding bits of functionality will no longer work.

These pieces of data do not apply the limitations of your database, they simply help you, allowing you to access functions in a more intuitive way. Actual restrictions are still implemented in the database.

Performance

Yii implements a query builder that you can use to generate SQL from freely expressed expressions (your models use the same mechanism when directly querying). The builder generates SQL from freely expressed expressions, and the result will not be slower than if you were executing equivalent SQL directly.

Of course, using the query builder itself will cause some overhead, but this is a compromise that has been largely chosen for all modern programming languages ​​(LINQ will be the most massive example here), because it's worth it.

In any case, you can write SQL queries CDbCommand using CDbCommand . I see no reason for this, but there is always an option if you need it. Therefore, there is no chance that you will remain unsatisfactory in ActiveRecord, no matter what you need to do.

+3
source

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


All Articles