public function attachments() { /** @var myModel $this */ return $this->make('attachments', function () { return Attachments::query()->where('attachable_id = :id:', ['id' => $this->id])->andWhere('attachable_type = :type:', ['type' => get_class($this)])->orderBy('created_at DESC')->execute(); }); }
public function createZipFile(array $file_Ids, $filename) { $files = Attachments::query()->inWhere('attachable_id', $file_Ids)->andWhere('attachable_type =:type:', ['type' => 'Files'])->leftJoin('Files', 'file.id = Attachments.attachable_id', 'file')->columns(['Attachments.*', 'file.*'])->execute(); $path = 'E:\\php\\standard\\public/'; $zip = new ZipArchive(); if ($zip->open($path . $filename, ZIPARCHIVE::CREATE) !== TRUE) { dd('无法生成ZIP文件,请检查是否具有写权限'); } foreach ($files as $row) { $zip->addFile($path . $row->attachments->url, $row->file->title . '/' . $row->attachments->name); $zip->addFromString($row->file->title . '/info.txt', $this->getFileInfo($row->file)); //@todo 将来用能够代表文档的数据形式来替代 } $zip->close(); return $filename; }