Skip to content

Commit

Permalink
Apply fix review.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jun 26, 2024
1 parent 21132cc commit 46e7a87
Showing 1 changed file with 103 additions and 3 deletions.
106 changes: 103 additions & 3 deletions framework/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,110 @@ for both A and B.
Upgrade from Yii 2.0.50
-----------------------

* Minimum PHP version requirement is now PHP `7.3.0`.
* Added tests for `MariaDB`, correcting the behavior for `JSON` column type, now they work the same for `MySQL` and
`MariaDb`.
* Correcting the behavior for `JSON` column type in `MariaDb`.

Example usage of `JSON` column type in `db`:

```php
<?php

use yii\db\Schema;

$db = Yii::$app->db;
$command = $db->createCommand();

// Create a table with a JSON column
$command->createTable(
'products',
[
'id' => Schema::TYPE_PK,
'details' => Schema::TYPE_JSON,
],
)->execute();

// Insert a new product
$command->insert(
'products',
[
'details' => [
'name' => 'apple',
'price' => 100,
'color' => 'blue',
'size' => 'small',
],
],
)->execute();

$records = $db->createCommand('SELECT * FROM product')->queryAll();
```

Example usage of `JSON` column type in `ActiveRecord`:

```php
<?php

namespace App\Model;

use yii\db\ActiveRecord;

class ProductModel extends ActiveRecord
{
public static function tableName()
{
return 'products';
}

public function rules()
{
return [
[['details'], 'safe'],
];
}
}
```

```php
<?php

use App\Model\ProductModel;

// Create a new product
$product = new ProductModel();

// Set the product details
$product->details = [
'name' => 'windows',
'color' => 'red',
'price' => 200,
'size' => 'large',
];

// Save the product
$product->save();

// Read the first product
$product = ProductModel::findOne(1);

// Get the product details
$details = $product->details;

echo 'Name: ' . $details['name'];
echo 'Color: ' . $details['color'];
echo 'Size: ' . $details['size'];

// Read all products with color red
$products = ProductModel::find()
->where(new \yii\db\Expression('JSON_EXTRACT(details, "$.color") = :color', [':color' => 'red']))
->all();

// Loop through all products
foreach ($products as $product) {
$details = $product->details;
echo 'Name: ' . $details['name'];
echo 'Color: ' . $details['color'];
echo 'Size: ' . $details['size'];
}
```

Upgrade from Yii 2.0.48
-----------------------
Expand Down

0 comments on commit 46e7a87

Please sign in to comment.