UploadBehavior.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace common\modules\attachment\behaviors;
  3. use common\behaviors\BaseAttachAttribute;
  4. use yii\db\ActiveRecord;
  5. use common\modules\attachment\models\Attachment;
  6. use common\modules\attachment\models\AttachmentIndex;
  7. use yii\helpers\ArrayHelper;
  8. class UploadBehavior extends BaseAttachAttribute
  9. {
  10. use UploadBehaviorTrait;
  11. public $multiple = false;
  12. /**
  13. * @return array
  14. */
  15. public function events()
  16. {
  17. return ArrayHelper::merge(parent::events(), [
  18. ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
  19. ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
  20. ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
  21. ]);
  22. }
  23. // 根据特性返回这个值
  24. protected function getValue()
  25. {
  26. $value = [];
  27. if ($this->multiple == false) {
  28. $value = null;
  29. }
  30. if (!$this->owner->isNewRecord) {
  31. $value = $this->getAttachments();
  32. if ($this->multiple == false) {
  33. $value = array_shift($value);
  34. }
  35. }
  36. return $value;
  37. }
  38. protected function setValue($value)
  39. {
  40. if (!empty($value)) {
  41. $value = array_filter($value, function ($val) {
  42. if (empty($val)) {
  43. return false;
  44. }
  45. return true;
  46. });
  47. }
  48. $this->value = $value;
  49. }
  50. public function getAttachments()
  51. {
  52. $attachmentIndex = AttachmentIndex::find()->where([
  53. 'entity_id' => $this->getEntityId(),
  54. 'entity' => $this->getEntity(),
  55. 'attribute' => $this->attribute
  56. ])->all();
  57. $attachmentIds = ArrayHelper::getColumn($attachmentIndex, 'attachment_id');
  58. return Attachment::findAll(['id' => $attachmentIds]);
  59. }
  60. public function afterInsert()
  61. {
  62. if ($this->value == null) {
  63. return;
  64. }
  65. $files = $this->value;
  66. if ($this->multiple == false) {
  67. $files = [$files];
  68. }
  69. $indexIds = []; // 只建立关系
  70. foreach ($files as $file) {
  71. if ($file['id'] > 0) {
  72. $indexIds[] = $file['id'];
  73. }
  74. }
  75. $this->batchSaveIndex($indexIds);
  76. }
  77. /**
  78. * 和数据库中的ID进行比较
  79. *
  80. * 两组不同的则是删除的.
  81. *
  82. * 删除附件的方法
  83. * 1.删除该item和附件的关系.
  84. * 2.检查该附件是否有其他引用,如果没有引用则删除附件表中的文件.再删除文件
  85. */
  86. public function afterUpdate()
  87. {
  88. if ($this->value != null) {
  89. $files = $this->value;
  90. if ($this->multiple == false) {
  91. $files = [$files];
  92. }
  93. } else {
  94. $files = [];
  95. }
  96. $relatedIds = $this->getRelatedIds();
  97. $indexIds = []; // 只建立关系
  98. $existIds = []; // 存在Oldid中的id,不存在的代表已经被删除的
  99. foreach ($files as $file) {
  100. if ($file['id'] > 0 && !in_array($file['id'], $relatedIds)) {
  101. $indexIds[] = $file['id'];
  102. } else {
  103. $existIds[] = $file['id'];
  104. }
  105. }
  106. $this->batchSaveIndex($indexIds);
  107. $this->deleteNotExistFiles($relatedIds, $existIds);
  108. }
  109. public function batchSaveIndex( $indexIds)
  110. {
  111. // $indexIds 关系可以去重
  112. foreach (array_unique($indexIds) as $id) {
  113. $this->saveIndex($id);
  114. }
  115. }
  116. }