In PHP, arrays are often transmitted as parameters. One requirement is that the Web program obtains some parameters from the Request, such as usernumber, username, and password, assemble these into an array and use them as parameters, pass them to the model layer, and write data... in PHP, arrays are often transmitted as parameters. One requirement is that the Web program first obtains some parameters from the Request, such
usernumber,
username,
passwordEtc. Assemble these into an array and pass them as parameters
modelLayer and write data to the database. For example:
The following parameters are assembled in the controller:
$params = [ 'number' => 1001, 'username' => 'kirineko', 'password' => '123456', 'school' => 'cmu',];Model\User::insert($params);
WriteinsertMethod:
public static insert($params) { $ins = new self(); foreach($params as $key => $value) { $ins->{$key} = $value; } $ins->save();}
However, the current problem is that if the passed array index is not in the database when I pass the parameter, for example,schoolThis field does not exist in the Database. For example, you need to inputageIs not passed in the above data. All of the above situations will cause database write failures.
My solution is to filter the input data parameters in the insert method as follows:
public static insert($params) { $ins = new self(); $filters = ['number','username','password','age']; $real_params = []; foreach ($filters as $filter) { if(isset($params[$filter])) { $invite_params[$filter] = $params[$filter]; } } foreach($invite_params as $key => $value) { $ins->{$key} = $value; } $ins->save();}
By filtering, the valid input in the array is assigned to another array. However, this method is too troublesome. Is there a better way to implement this function?
Reply content:
In PHP, arrays are often transmitted as parameters. One requirement is that the Web program first obtains some parameters from the Request, suchusernumber,username,passwordEtc. Assemble these into an array and pass them as parametersmodelLayer and write data to the database. For example:
The following parameters are assembled in the controller:
$params = [ 'number' => 1001, 'username' => 'kirineko', 'password' => '123456', 'school' => 'cmu',];Model\User::insert($params);
WriteinsertMethod:
public static insert($params) { $ins = new self(); foreach($params as $key => $value) { $ins->{$key} = $value; } $ins->save();}
However, the current problem is that if the passed array index is not in the database when I pass the parameter, for example,schoolThis field does not exist in the Database. For example, you need to inputageIs not passed in the above data. All of the above situations will cause database write failures.
My solution is to filter the input data parameters in the insert method as follows:
public static insert($params) { $ins = new self(); $filters = ['number','username','password','age']; $real_params = []; foreach ($filters as $filter) { if(isset($params[$filter])) { $invite_params[$filter] = $params[$filter]; } } foreach($invite_params as $key => $value) { $ins->{$key} = $value; } $ins->save();}
By filtering, the valid input in the array is assigned to another array. However, this method is too troublesome. Is there a better way to implement this function?
UseORMCool!
ORM: no matter how many fields you pass in, I onlyInsert | updateYoursIntersection field.
Why do you need to transfer unnecessary schools ......
Write a private attribute, such as $ _ allow, and then a private attribute $ _ requiere. allow is the allowed field, and require is a required field, compare the fields to be operated with allow and use array_diff. If there are different fields, throw is an exception. It seems that everyone did this before. Let's look at tp first, or yii2, because of your problem, you don't need to consider using the framework. The framework has already been ready for you. You can use the method provided by the framework.
This is usually done in this way. If an object is a ing of a data table, it will read the structure of the data table during initialization. However, if the table structure is loaded every time, the performance will be affected, therefore, the table structure information is cached, that is, the data table structure is always loaded from the cache, and a special function is provided to clear the cache.
The table structure information can be:
1-data filtering by Field Names
2-simple validity check, such as non-empty check, numeric check, and length check
3-Auxiliary form generation
You can check the data table field list.
mysql> select COLUMN_NAME from information_schema.`COLUMNS` where TABLE_NAME='XXX';
This method is inefficient and insecure. We recommend that you use an ORM solution.
Write auxiliary functions to process PHP advanced filters