@@ -51,6 +51,114 @@ if you want to upgrade from version A to version C and there is
51
51
version B between A and C, you need to follow the instructions
52
52
for both A and B.
53
53
54
+ Upgrade from Yii 2.0.50
55
+ -----------------------
56
+
57
+ * Correcting the behavior for ` JSON ` column type in ` MariaDb ` .
58
+
59
+ Example usage of ` JSON ` column type in ` db ` :
60
+
61
+ ``` php
62
+ <?php
63
+
64
+ use yii\db\Schema;
65
+
66
+ $db = Yii::$app->db;
67
+ $command = $db->createCommand();
68
+
69
+ // Create a table with a JSON column
70
+ $command->createTable(
71
+ 'products',
72
+ [
73
+ 'id' => Schema::TYPE_PK,
74
+ 'details' => Schema::TYPE_JSON,
75
+ ],
76
+ )->execute();
77
+
78
+ // Insert a new product
79
+ $command->insert(
80
+ 'products',
81
+ [
82
+ 'details' => [
83
+ 'name' => 'apple',
84
+ 'price' => 100,
85
+ 'color' => 'blue',
86
+ 'size' => 'small',
87
+ ],
88
+ ],
89
+ )->execute();
90
+
91
+ // Read all products
92
+ $records = $db->createCommand('SELECT * FROM products')->queryAll();
93
+ ```
94
+
95
+ Example usage of ` JSON ` column type in ` ActiveRecord ` :
96
+
97
+ ``` php
98
+ <?php
99
+
100
+ namespace app\model;
101
+
102
+ use yii\db\ActiveRecord;
103
+
104
+ class ProductModel extends ActiveRecord
105
+ {
106
+ public static function tableName()
107
+ {
108
+ return 'products';
109
+ }
110
+
111
+ public function rules()
112
+ {
113
+ return [
114
+ [['details'], 'safe'],
115
+ ];
116
+ }
117
+ }
118
+ ```
119
+
120
+ ``` php
121
+ <?php
122
+
123
+ use app\model\ProductModel;
124
+
125
+ // Create a new product
126
+ $product = new ProductModel();
127
+
128
+ // Set the product details
129
+ $product->details = [
130
+ 'name' => 'windows',
131
+ 'color' => 'red',
132
+ 'price' => 200,
133
+ 'size' => 'large',
134
+ ];
135
+
136
+ // Save the product
137
+ $product->save();
138
+
139
+ // Read the first product
140
+ $product = ProductModel::findOne(1);
141
+
142
+ // Get the product details
143
+ $details = $product->details;
144
+
145
+ echo 'Name: ' . $details['name'];
146
+ echo 'Color: ' . $details['color'];
147
+ echo 'Size: ' . $details['size'];
148
+
149
+ // Read all products with color red
150
+ $products = ProductModel::find()
151
+ ->where(new \yii\db\Expression('JSON_EXTRACT(details, "$.color") = :color', [':color' => 'red']))
152
+ ->all();
153
+
154
+ // Loop through all products
155
+ foreach ($products as $product) {
156
+ $details = $product->details;
157
+ echo 'Name: ' . $details['name'];
158
+ echo 'Color: ' . $details['color'];
159
+ echo 'Size: ' . $details['size'];
160
+ }
161
+ ```
54
162
55
163
Upgrade from Yii 2.0.48
56
164
-----------------------
0 commit comments