“This is the 16th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Today I received a demand that I need to investigate the YII2 framework and make a comparison with Laravel, so as to determine the technology selection.

I have used the YII framework before, but in version 1.1. The current version of YII2 is version 2.0. It is a rewritten version, which is a sea change from version 1.1.

Now I would like to introduce the results of today’s research:

Introduction of YII2

Yii is currently available in two major versions: 1.1 and 2.0. Version 1.1 is an older version of the previous generation and is now in maintenance. Version 2.0 is a complete rewrite, using the latest technologies and protocols, including dependency package manager Composer, PHP code specification PSR, namespaces, Traits, and more.

YII2 System requirements

Yii 2.0 requires PHP 5.4.0 or above.

Using Yii requires a basic understanding of object-oriented programming (OOP) because Yii is a pure object-oriented framework. Yii 2.0 also makes use of recent FEATURES of PHP, such as namespaces and traits

YII2 characteristics

This is compared to version 1.1 of YII

  1. Query generator and ActiveRecord are available for both relational and NoSQL databases
  2. Fully embrace Composer, package management tool

The key point

Object configuration

The Object class introduces a way to unify Object configuration. All Object subclasses should declare their constructors (if necessary) to properly configure themselves:

class MyClass extends \yii\base\BaseObject { public function __construct($param1, $param2, $config = []) { // ... Parent ::__construct($config); } public function init() { parent::init(); / /... Initialization process after the configuration takes effect}}Copy the code

In the above example, the last parameter to the constructor must be passed in a configuration array containing a series of key-value pairs that are used to initialize the related properties at the end of the method. You can override the init() method to do some initialization that needs to happen after the configuration takes effect.

You can create and configure new objects using configuration arrays by following the following conventions:

$object = Yii::createObject([
    'class' => 'MyClass',
    'property1' => 'abc',
    'property2' => 'cde',
], [$param1, $param2]);
Copy the code

I was so happy to see this, because I had a project that did the same. The last argument to the constructor is set to an array, supporting the passing of extended fields. I didn’t expect that my project experience was consistent with the framework designer of YII2.

For those of you who are interested, check out my document to see how interesting the design is that the last parameter mentioned above must be passed in a configuration array.

Performance optimization reflection: reduce DB query, rational use of member variables; Flexible use of extended fields

Query Builder query Builder

YII’s query builder is far more complex to use than laravel’s Eloquent, and is ideal for situations where you can query SQL directly without building a Model layer.

$query = new \yii\db\Query(); 
$query->select('id, name') ->from('user') ->limit(10); 
$command = $query->createCommand();
$sql = $command->sql;
$rows = $command->queryAll();
Copy the code

Active record

And yII’s Active Record is basically the same as Laravel’s Eloquent.

Both are simple to use, both support associative lookup, and both support specifying return types: Laravel supports returning collections or arrays; Yii can return objects or arrays (yII uses asArray() to return arrays to reduce CPU and memory usage).

The CDbCriteria class in YII 1.1 was replaced by yII \db\ActiveQuery in YII 2. This class inherits from yII \db\Query and therefore all Query generation methods. To begin assembling a query, call the yii\db\ActiveRecord::find() method:

$customers = Customer::find() -> WHERE (['status' => $active]) ->orderBy(' ID ') ->all(); $customers = Customer::find() ->orderBy(' ID ') ->all();Copy the code

To declare an association, you simply define a getter method to return an ActiveQuery object. The property name defined by the getter method represents the associated table name. For example, the following code declares a relationship named Orders (the relationship must be declared in the relations() method in 1.1) :

class Customer extends \yii\db\ActiveRecord { public function getOrders() { return $this->hasMany('Order', ['customer_id' => 'id']); }}Copy the code

You can now access the orders of a user in the associated table by calling $customer-> Orders. You can also perform a real-time associative query with specified criteria using the following code:

$orders = $customer->getOrders()->andWhere('status=1')->all();
Copy the code

Yii 2.0 and 1.1 work differently when greed loads a relationship. Specifically, in 1.1, a JOIN statement is used to query both primary and associated table records. In Yii 2.0, two SQL statements without joins are used: the first statement retrieves primary table records, and the second statement queries associated table records after primary key filtering through primary table records.

When generating queries that return a large number of records, the asArray() method can be chained to return the query result as an array rather than an ActiveRecord object, which can significantly reduce the CPU time and memory consumed by a large number of record reads. Such as:

$customers = Customer::find()->asArray()->all();
Copy the code

conclusion

Overall, the YII2 is a big change from the YII 1.1 version. It’s not very different from the Laravel in terms of how it handles data, especially: The Active Record is very similar to the Eloquent.

The last

Thanks for reading and welcome to like, favorites,coin(attention)!!