public function getFile() { return $this->hasMany(File::className(), array('attribute_id' => 'ticket_id'))->where('attribute=:table_name', array('table_name' => Ticket::tableName())); }
/** * @return \yii\db\ActiveQuery */ public function getFiles() { return $this->hasMany(File::className(), ['uploaded_by' => 'id']); }
public function upload($instance_file, $object = 'person', $object_id, $file, $object_file, $type = 'photo', $resize = false, $current_file = '', $thumb = false) { if ($object_file == null) { $object_file = new ObjectFile(); } if ($file == null) { $file = new File(); } $ext = end(explode(".", $instance_file->name)); $filenames = uniqid() . '.' . $ext; $file->file_name = $filenames; $path = ''; if (isset(Yii::$app->params['uploadPath'])) { $path = Yii::$app->params['uploadPath'] . '/' . $object . '/' . $object_id . '/'; } else { $path = Yii::getAlias('@file') . '/' . $object . '/' . $object_id . '/'; } @mkdir($path, 0755, true); @chmod($path, 0755); if (isset($current_file)) { @unlink($path . $current_file); @unlink($path . 'thumb_' . $current_file); } if (isset($filenames)) { $instance_file->saveAs($path . $filenames); if ($resize) { \hscstudio\heart\helpers\Heart::imageResize($path . $filenames, $path . 'thumb_' . $filenames, 148, 198, 0); } if (!isset($file->name)) { $file->name = $filenames; } if (!isset($file->status)) { $file->status = 1; } $file->save(); $object_file->object = $object; $object_file->object_id = $object_id; $object_file->type = $type; $object_file->file_id = $file->id; $object_file->save(); return true; } return false; }