我试图在MongoDB中存储用户对详细健身报告调查问卷的回复。提交表单后,我将解析表单数据并创建一个具有预定义结构的数组/对象。
我正在考虑创建一个具有整个对象层次结构的类,其值设置为空字符串''。我想要存储的响应如下所示:
{
"_basic": {
"_response_type": "text",
"_response": {
"name": "eNeMetchH",
"gender": "male",
"age": 20,
"contact": "9876543210",
"email": "email@email.com",
"dob": "1997-03-20",
"package": {
"id": "1001",
"name": "Alpha Monthly",
"start_date": "2017-01-01",
"end_date": "2017-01-31"
}
}
},
"_parq": {
"footnotes": "If yes to one or more questions, please talk to your doctor before you start becoming more physically active or before you have a fitness appraisal. Tell your doctor about the PAR-Q and which questions you have answered yes.",
"response_type": "radio",
"response": [
{
"question": "Has your doctor ever said you have a heart condition and you should only do physical activity that is described by doctor?",
"response": "NO"
},
{
"question": "Do you feel pain in your chest when you do physical activity?",
"response": "NO"
},
{
"question": "In the past month, have you had chest pain when you were not doing physical activity?",
"response": "NO"
},
{
"question": "Do you lose your balance because of dizziness or do you ever lose consciousness?",
"response": "NO"
},
{
"question": "Do you have a bone or joint problem that could not be made worse by a change in your physical activity?",
"response": "NO"
},
{
"question": "Is your doctor currently prescribing drugs for your BP & Heart conditions?",
"response": "NO"
},
{
"question": "Do you know any other reason why you should not do physical activity?",
"response": "NO"
}
]
},
"_conditioning_level": "_intermediate_1_3",
"_occupation": "_professional",
"_primary_goal": "_weight_gain",
"_secondary_goal": "_muscular_strength",
"_workout_intensity": "_mild",
"_availability": {
"_days_per_week": "6",
"_min_per_day": "90"
},
"_sleep": "_insomnia",
"_physical_activity": "_sedentary",
"_stress_level": "_mild",
"_medical_history": [
"_knee_pain",
"_hypertension",
"_high_cholesterol"
],
"_alcohol": "_alcoholic",
"_smoking": "YES",
"_tobacco": "NO",
"_shoe_analysis": {
"_toe_box": "_normal",
"_torsion": "_normal",
"_heel_support": "_abnormal",
"_arch": "_normal"
},
"_postural_analysis": {
"_head_neck": "Blah blah",
"_shoulder": "Blah blah",
"_thoracic_spine": "Blah blah",
"_lumbar_spine": "Blah blah",
"_knee": "Blah blah",
"_feet": "Blah blah"
},
"_anthropometry": {
"_bp": "160/90 mm/Hg",
"_resting_heart_rate": "90 bpm",
"_weight": "82",
"_height": "183",
"_bmi": "20"
},
"_functional_movement": {
"_ssn": "Blah blah",
"_height": "183",
"_weight": "82",
"_primary_score": "Dono",
"_primary_position": "Dono",
"_hand_leg_dominance": "Dono",
"_previous_test_score": "Dono",
"tests": {
"_deep_squat": [
{
"_raw_score": "30",
"_final_score": "40"
}
],
"_hurdle_step": {
"L": {
"_raw_score": "30",
"_final_score": "40"
},
"R": {
"_raw_score": "30",
"_final_score": "40"
}
},
"_inline_lunge": {
"L": {
"_raw_score": "30",
"_final_score": "40"
},
"R": {
"_raw_score": "30",
"_final_score": "40"
}
},
"_shoulder_mobility": {
"L": {
"_raw_score": "30",
"_final_score": "40"
},
"R": {
"_raw_score": "30",
"_final_score": "40"
}
},
"_impingement_clearing_test": {
"L": {
"_raw_score": "30",
"_final_score": "40"
},
"R": {
"_raw_score": "30",
"_final_score": "40"
}
},
"_active_straight_leg_raise": {
"L": {
"_raw_score": "30",
"_final_score": "40"
},
"R": {
"_raw_score": "30",
"_final_score": "40"
}
},
"_trunk_push_up": [
{
"_raw_score": "30",
"_final_score": "40"
}
],
"_press_up_test": [
{
"_raw_score": "30",
"_final_score": "40"
}
],
"_rotary_stability": {
"L": {
"_raw_score": "30",
"_final_score": "40"
},
"R": {
"_raw_score": "30",
"_final_score": "40"
}
},
"_posterior_rocking_test": [
{
"_raw_score": "30",
"_final_score": "40"
}
],
"_total": [
{
"_raw_score": "30",
"_final_score": "40"
}
]
}
},
"_fitness_test": {
"_aerobic_endurance": {
"_distance_covered": "2 km",
"_pre_heart_rate": "72",
"_post_heart_rate": "95",
"_vo2_max": "90",
"_mets": "30"
},
"_muscular_strength": {
"_upper_body": "6",
"_lower_body": "7"
},
"_muscular_endurance": {
"_ab_crunches": "6",
"_free_squats": "7"
},
"_flexibility": {
"_sit_reach": "5",
"_cobra_stretch": "6"
},
"_proprioception": {
"_lt_side": "10",
"_rt_side": "10"
},
"_core_strength": {
"_iron_man": "55"
}
},
"_assessment_summary": {
"_age": "20",
"_sex": "_male",
"_conditioning_level": "_intermediate_1_3",
"_goal": "_weight_gain",
"_occupation": "_professional",
"_stress_level": "_mild",
"_health_problem": "None",
"_test_result": "Need fat loss and Increment in lung capacity",
"_preferences": "None whatsoever"
},
"_remarks": "Blah blah blah"
}
我需要创建一个上述结构完整且空值的模板。不应再允许添加密钥或删除密钥。
最佳方法是什么?
答案 0 :(得分:1)
如果将变量保持为受保护状态,则只能通过成员函数对其进行修改,并且无法直接访问。 __set函数将阻止添加新变量。
创建一个类
class Foo{
// if you keep the variable as protected, then it can be modified only
// by member functions so noone will be able to remove the variable.
protected $v1;
// this set function will prevent addition of new variable.
public function __set($name, $value) {
throw new Exception("Cannot add new property \$$name to instance of " . __CLASS__);
}
}
占领等属性的示例,您必须将其定义为
protected $occupation = "professional";