UploadIndexBehavior.php 789 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace common\modules\attachment\behaviors;
  3. use yii\db\ActiveRecord;
  4. use yii\base\Behavior;
  5. class UploadIndexBehavior extends Behavior
  6. {
  7. use UploadBehaviorTrait;
  8. public $file;
  9. public $attribute;
  10. /**
  11. *
  12. * @return array
  13. */
  14. public function events()
  15. {
  16. return [
  17. ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
  18. ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete'
  19. ];
  20. }
  21. public function afterInsert()
  22. {
  23. $file = $this->owner->{$this->file};
  24. if ($file["id"] == - 1) {
  25. $attachment = $this->attachFile($file);
  26. $this->saveIndex($attachment->primaryKey);
  27. } elseif ($file["id"] > 0) {
  28. $this->saveIndex($file["id"]);
  29. }
  30. }
  31. }