BaseAttachAttribute.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace common\behaviors;
  3. use yii\base\Behavior;
  4. use yii\db\ActiveRecord;
  5. class BaseAttachAttribute extends Behavior
  6. {
  7. /**
  8. *
  9. * @var \yii\db\ActiveRecord
  10. */
  11. public $owner;
  12. /**
  13. * @var string
  14. */
  15. public $attribute;
  16. /**
  17. *
  18. * @var mixed
  19. */
  20. protected $value;
  21. public function events()
  22. {
  23. return [
  24. ActiveRecord::EVENT_AFTER_FIND => 'afterFind'
  25. ];
  26. }
  27. public function afterFind()
  28. {
  29. $this->value = $this->getValue();
  30. }
  31. public function canGetProperty($name, $checkVars = true)
  32. {
  33. return parent::canGetProperty($name, $checkVars) || $this->attribute == $name;
  34. }
  35. public function canSetProperty($name, $checkVars = true)
  36. {
  37. return parent::canSetProperty($name, $checkVars) || $this->attribute == $name;
  38. }
  39. // 根据特性返回这个值
  40. public function __get($name)
  41. {
  42. if ($name == $this->attribute) {
  43. return $this->value;
  44. }
  45. return parent::__get($name);
  46. }
  47. public function __set($name, $value)
  48. {
  49. if ($name == $this->attribute) {
  50. $this->setValue($value);
  51. return;
  52. }
  53. parent::__set($name, $value);
  54. }
  55. protected function getValue()
  56. {
  57. }
  58. protected function setValue($value)
  59. {
  60. }
  61. }