The method of operation is one of a coherent data model class method,

One method is also consistent operation method data model classes, the set value for the current data object to be operated.

Write

Normally we are produced by way of assignment methods or create the data object, and then written to the database, for example:

  1. $Model = D('User');
  2. $Model->create();
  3. // 这里略过具体的自动生成和验证判断
  4. $Model->add();

Or direct assignment of data objects, such as:

  1. $Model = M('User');
  2. $Model->name = '流年';
  3. $Model->email = '[email protected]';
  4. $Model->add();

Then data is a direct method to generate a data object to be operated, for example:

  1. $Model = M('User');
  2. $data['name'] = '流年';
  3. $data['email'] = '[email protected]';
  4. $Model->data($data)->add();

Note: If the method we also create data objects using the create methods and data, then the last call is valid.

The method of supporting data arrays, and strings, objects as follows:

  1. $Model = M('User');
  2. $obj = new \stdClass;
  3. $obj->name = '流年';
  4. $obj->email = '[email protected]';
  5. $Model->data($obj)->add();

Character string is used as follows:

  1. $Model = M('User');
  2. $data = 'name=流年&[email protected]';
  3. $Model->data($data)->add();

You can also pass directly in the process add new data to the data objects, for example:

  1. $Model = M('User');
  2. $data['name'] = '流年';
  3. $data['email'] = '[email protected]';
  4. $Model->add($data);

However, this approach can only use the data parameter array.

Of course, data can also be used to update data method, for example:

  1. $Model = M('User');
  2. $data['id'] = 8;
  3. $data['name'] = '流年';
  4. $data['email'] = '[email protected]';
  5. $Model->data($data)->save();

Of course, we can directly use this:

  1. $Model = M('User');
  2. $data['id'] = 8;
  3. $data['name'] = '流年';
  4. $data['email'] = '[email protected]';
  5. $Model->save($data);

Similarly, when data parameters can only be passed in an array.

When you call the save method to update the data if there is a primary key value will automatically determine the presence of the current data object inside, the conditions, if any, will be automatically updated as. That is to say, above and below the equivalent usage:

  1. $Model = M('User');
  2. $data['name'] = '流年';
  3. $data['email'] = '[email protected]';
  4. $Model->data($data)->where('id=8')->save();

Read

In addition to external write operation, data reading method may also be used for the current data object, for example:

  1. $User = M('User');
  2. $map['name'] = '流年';
  3. $User->where($map)->find();
  4. // 读取当前数据对象
  5. $data = $User->data();
 

Guess you like

Origin www.cnblogs.com/furuihua/p/11792536.html