Php 使用YII框架验证数据库中存储的JSON文件中的数据

Php 使用YII框架验证数据库中存储的JSON文件中的数据,php,yii2,Php,Yii2,我试图在我的Yii项目中编写一条规则,以便在存储的JSON上收集数据点时添加一条豁免规则。下面是验证规则的示例Yii脚本 public function rules() { return [ [['animal_id', 'event_type', 'event_date'], 'required'], [['animal_id', 'event_type', 'country_id', 'region_id', 'district_id', 'ward_

我试图在我的Yii项目中编写一条规则,以便在存储的JSON上收集数据点时添加一条豁免规则。下面是验证规则的示例Yii脚本

public function rules()
{
    return [
        [['animal_id', 'event_type', 'event_date'], 'required'],
        [['animal_id', 'event_type', 'country_id', 'region_id', 'district_id', 'ward_id', 'village_id', 'field_agent_id'], 'integer'],
        [['event_date', 'data_collection_date'], 'date', 'format' => 'php:Y-m-d'],
        [['latitude', 'longitude'], 'number'],
        [['map_address', 'uuid'], 'string', 'max' => 255],
        ['event_date', 'validateNoFutureDate'],
        ['event_date', 'unique', 'targetAttribute' => ['animal_id', 'event_type', 'event_date'], 'message' => '{attribute} should be unique per animal', 'except' => [self::SCENARIO_MISTRO_DB_UPLOAD]],
        [['org_id', 'client_id'], 'safe'],
        ['migration_id', 'unique', 'except' => self::SCENARIO_MISTRO_DB_UPLOAD],
        [[self::SEARCH_FIELD], 'safe', 'on' => self::SCENARIO_SEARCH],
    ];
} 
在我的数据库中,我有一个列,其中我将额外变量存储为存储的JSON,如下图所示,所有额外属性都存储在列additional attributes下,该列是存储的JSON

CREATE TABLE `core_animal_event` (
  `id` int NOT NULL AUTO_INCREMENT,
  `animal_id` int NOT NULL,
  `event_type` int NOT NULL,
  `country_id` int NOT NULL,
  `region_id` int DEFAULT NULL,
  `district_id` int DEFAULT NULL,
  `ward_id` int DEFAULT NULL,
  `village_id` int DEFAULT NULL,
  `org_id` int DEFAULT NULL,
  `client_id` int DEFAULT NULL,
  `event_date` date DEFAULT NULL,
  `data_collection_date` date DEFAULT NULL,
  `latitude` decimal(13,8) DEFAULT NULL,
  `longitude` decimal(13,8) DEFAULT NULL,
  `map_address` varchar(255) DEFAULT NULL,
  `latlng` point DEFAULT NULL,
  `uuid` varchar(255) NOT NULL,
  `field_agent_id` int DEFAULT NULL,
  `lactation_id` int DEFAULT NULL COMMENT 'lactation Id/Calving Id for milking record',
  `lactation_number` int DEFAULT NULL COMMENT 'lactation number for calving records',
  `testday_no` int DEFAULT NULL COMMENT 'Test day number for milk record',
  `additional_attributes` json DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `created_by` int DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `updated_by` int DEFAULT NULL,
  `migration_id` varchar(255) DEFAULT NULL COMMENT 'This is the migrationSouce plus primary key from migration source table of the record e.g KLBA_001',
  `odk_form_uuid` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `animal_id` (`animal_id`),
  KEY `event_type` (`event_type`),
  KEY `lactation_id` (`lactation_id`),
  KEY `country_id` (`country_id`,`region_id`,`district_id`,`ward_id`,`village_id`),
  KEY `org_id` (`org_id`,`client_id`),
  KEY `event_date` (`event_date`),
  KEY `data_collection_date` (`data_collection_date`),
  CONSTRAINT `core_animal_event_ibfk_1` FOREIGN KEY (`animal_id`) REFERENCES `core_animal` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2623841 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=COMPACT;

我想在我的代码中添加一个验证,以免除附加属性下的记录,我是Yii框架的新手,我在Yii文档中使用了验证解决方案,没有明确的前进方向

Yii2没有特定于JSON的验证,它可以将is作为字符串进行验证,也可以验证数组中的每个索引

但如果需要更精确的验证,则必须为此创建自己的验证规则。首先将您的自定义规则名称添加到
规则
方法:

公共功能规则()
{
返回[
// ...
['additional_attributes'、'validateAdditionalAttributes'],
// ...
];
}
创建自定义规则
方法

公共函数validateAdditionalAttributes($attribute、$params、$validator)
{
//JSON可能已经是一个字符串
if(是字符串($this->$属性)){
$decoded=\yii\helpers\Json::decode($this->$attribute);
}
//现在执行自定义验证
//如果出现错误,请将错误添加到字段中
//$this->addError($attribute,'mycustomerror');
//这将验证模型是否为false
}
阅读更多关于