title: SplBean meta:

  • name: description content: EasySwoole SplBean
  • name: keywords content: swoole|swoole extension|swoole framework|easyswoole,SplBean

Use

Used to define the table structure and filter out invalid field data.

SplBean related methods

Method list

Method Name Parameters Description Notes
__construct array $data = null,$autoCreateProperty = false Constructor, initialize bean data
allProperty Filter and convert to array data
toArray array $columns = null,$filter = null Transfer character
toArrayWithMapping array $columns = null,$filter = null Get filtered array data with field aliases
arrayToBean array $data,$autoCreateProperty = false Set class properties
addProperty $name,$value = null Set class member variables
getProperty $name Get class member variable values
jsonSerialize Get class member variable collection
initialize Initialization operation
setKeyMapping Set the keyMapping relationship, which is the field alias
setClassMapping Set the classMapping relationship, which is the associated class.
restore array $data = [], $autoCreateProperty = false Reinitialize bean data
classMap Binding association class

how to use

  1. /**
  2. *
  3. * User: LuffyQAQ
  4. * Date: 2019/10/16 16:45
  5. * Email: <1769360227@qq.com>
  6. */
  7. include "./vendor/autoload.php";
  8. use EasySwoole\Spl\SplBean;
  9. class TestBean extends SplBean
  10. {
  11. public $a = 2;
  12. protected $b;
  13. private $c;
  14. protected $d_d;
  15. protected function setKeyMapping(): array
  16. {
  17. return [
  18. 'd-d' => "d_d"
  19. ];
  20. }
  21. }
  22. $bean = new TestBean([
  23. 'a'=>'a',
  24. 'b'=>'b',
  25. 'c'=>'c',
  26. 'd_d'=>'d_d'
  27. ]);
  28. var_dump($bean->allProperty());
  29. $data = $bean->toArray(null, function ($a) {
  30. if (in_array($a, ['d_d'])) {
  31. return $a;
  32. }
  33. });
  34. $bean = new TestBean([
  35. 'a'=>1,
  36. 'b'=>2,
  37. 'c'=>3,
  38. 'd_d'=>4
  39. ]);
  40. $data = $bean->toArrayWithMapping(['a', 'b', 'd-d'], function ($val) {
  41. return $val;
  42. });
  43. var_dump($data);
  44. var_dump($bean->toArrayWithMapping(['a','d-d']));
  45. $bean = new TestBean();
  46. $bean->addProperty('a', 'es');
  47. $bean->addProperty('b', 'es');
  48. $bean->addProperty('d_d', 'es');
  49. var_dump($bean->toArray());
  50. var_dump($bean->getProperty('a'));
  51. var_dump( $bean->jsonSerialize());
  52. var_dump($bean->restore()->toArray());