1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace common\modules\attachment\behaviors;
- use common\modules\attachment\models\Attachment;
- use common\modules\attachment\models\AttachmentIndex;
- use common\traits\EntityTrait;
- use yii\helpers\ArrayHelper;
- trait UploadBehaviorTrait
- {
- use EntityTrait;
-
- public function afterDelete()
- {
- $relatedIds = $this->getRelatedIds();
- AttachmentIndex::deleteAll([
- 'entity_id' => $this->getEntityId(),
- 'entity' => $this->getEntity(),
- ]);
- foreach ($relatedIds as $id) {
- $this->deleteFile($id);
- }
- }
- public function deleteNotExistFiles($relatedIds, $existIds)
- {
- foreach (array_diff($relatedIds, $existIds) as $id) {
-
- AttachmentIndex::deleteAll([
- 'attachment_id' => $id,
- 'entity_id' => $this->getEntityId(),
- 'attribute' => $this->attribute,
- 'entity' => $this->getEntity(),
- ]);
-
- $this->deleteFile($id);
- }
- }
-
- public function deleteFile($id)
- {
- $isUse = AttachmentIndex::find()->where([
- 'attachment_id' => $id,
- ])->count();
- if ($isUse > 0) {
- return false;
- }
- $file = Attachment::findOne([
- 'id' => $id,
- ]);
- if ($file !== null) {
-
- }
- }
- public function getRelatedIds()
- {
- $attachments = AttachmentIndex::find()->where([
- 'entity' => $this->getEntity(),
- 'entity_id' => $this->getEntityId(),
- 'attribute' => $this->attribute,
- ])->all();
- return ArrayHelper::getColumn($attachments, 'attachment_id');
- }
- public function saveIndex($id)
- {
- $relation = new AttachmentIndex();
- $relation->entity = $this->getEntity();
- $relation->entity_id = $this->getEntityId();
- $relation->attribute = $this->attribute;
- $relation->attachment_id = $id;
- $relation->save();
- }
- }
|