Activerecord 如何将数据数组保存到数据库

Activerecord 如何将数据数组保存到数据库,activerecord,yii2,Activerecord,Yii2,如何使用yii2活动记录将下面的数据数组保存到数据库 Array ( [0] => Array ( [0] => Array ( [UID] => RPS2019U002 [Name ] => Nishanth [Father] => mallesh [Mother] => Lakshmi [Occupation] => Govt Employee [Mobile] => 9856985689 [Email] => mallesh@gmail

如何使用yii2活动记录将下面的数据数组保存到数据库

Array ( [0] => Array ( [0] => Array ( [UID] => RPS2019U002 [Name ] => Nishanth [Father] => mallesh [Mother] => Lakshmi [Occupation] => Govt Employee [Mobile] => 9856985689 [Email] => mallesh@gmail.com [Class] => UKG [dob] => 5/12/2016 [Route] => 1 [Addres] => Mysore [Remarks] => No ) [1] => Array ( [UID] => RPS2019U003 [Name ] => Nishanth [Father] => mallesh [Mother] => Lakshmi [Occupation] => Govt Employee [Mobile] => 9856985690 [Email] => mallesh@gmail.com [Class] => UKG [dob] => 5/13/2016 [Route] => 2 [Addres] => Mysore [Remarks] => No ) [2] => Array ( [UID] => RPS2019U004 [Name ] => Nishanth [Father] => mallesh [Mother] => Lakshmi [Occupation] => Govt Employee [Mobile] => 9856985691 [Email] => mallesh@gmail.com [Class] => UKG [dob] => 5/14/2016 [Route] => 3 [Addres] => Mysore [Remarks] => No ) ) [1] => Array ( ) [2] => Array ( ) )

最好的选择是使用
json\u encode/json\u decode

您还可以使用
序列化/取消序列化

JSON更便于携带。它可以在任何编程语言中使用。(更广泛的格式支持)
您可以使用getter和setter创建两个方法:(选择上述方法之一)
请参见此
对于序列化:

serialize ([Array]); 
unserialize([Serialized value]);

模型规则 如果要在验证规则中检查数组:

['yourField',function ($attribute, $params) {
    if(!is_array($this->valueYourField)){
         $this->addError('yourField','yourField not array!');
     }
}]
例如,如果数组应仅为整数。使用
allowArray

['yourField', 'in', 'range' => [1, 2, 3], 'allowArray' => true, 'message' => '{attribute} is invalid.']
您还可以检查控制器中数组的内容,使用上述方法后,在模型文件中使用
Safe
。(规则)

注意:不要使用
内爆()。引起问题

还要检查这个或

 ['yourField', 'safe'],