public static function getValue($json, $key, $isQuoted = true, $exReturn = null) { try { $values = Json::decode($json, true, $isQuoted); if (isset($values[$key])) { return $values[$key]; } else { return null; } } catch (yii\base\InvalidParamException $e) { //Exception thrown by Json::decode return $exReturn; } }
/** Added by pafnow * Generates validation rules for the specified table. * @param \yii\db\TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) { $table_clone = clone $table; $types = []; foreach ($table_clone->columns as $key => $column) { $column_clone = clone $column; $table_clone->columns[$key] = $column_clone; $type_comment = Json::getValue($column->comment, "type", false); if (empty($type_comment)) { continue; } $types[$type_comment][] = $column->name; if (!$column->allowNull && $column->defaultValue === null) { $types['required'][] = $column->name; } //Remove the column from the table so it is not generated again by the parent function unset($table_clone->columns[$key]); } $rules = []; foreach ($types as $type => $columns) { $rules[] = "[['" . implode("', '", $columns) . "'], '{$type}']"; } return array_merge(parent::generateRules($table_clone), $rules); }