|
Db_File class
Represents a file or image attached to a model.
This class used for creating model relations which contain model attachments.
Please note that downloadable product files are presented with another class - Shop_ProductFile.
The example below demonstrates a typical relation which can be presented as a single file, single image, multiple file or multiple image
widget on the form, depending in the value passed to the field's Db_FormFieldDefinition::renderFilesAs() method:
public $has_many = array( 'images'=>array('class_name'=>'Db_File', 'foreign_key'=>'master_object_id', 'conditions'=>"master_object_class='Shop_Product' and field='images'", 'order'=>'sort_order, id', 'delete'=>true) ); After adding the relations a corresponding column should be created with Db_ActiveRecord::define_multi_relation_column() method: public function define_columns($context = null) { $this->define_multi_relation_column('images', 'images', 'Images', '@name')->invisible(); } public function define_form_fields($context = null) { $this->add_form_field('images')->renderAs(frm_file_attachments)->renderFilesAs('image_list') ->addDocumentLabel('Add image(s)')->tab('Images')->noAttachmentsLabel('There are no images uploaded') ->fileDownloadBaseUrl(url('ls_backend/files/get/')); } See AlsoPublic propertiesShow inherited properties.
Public methodsShow inherited methods.
Protected methodsShow inherited methods.
Events
Property details¶ description propertypublic string $description;
Specifies the file description.
¶ id propertypublic integer $id;
Specifies the file record identifier.
¶ is_public propertypublic boolean $is_public;
Determines whether the file on the server can be accessed from the Web.
Public files are stored to uploaded/public directory, which is open for the external access.
The form behavior considers image files (rendered as single image or image lists)
as public, and all other files as non public.
¶ name propertypublic string $name;
Specifies the file name.
¶ size propertypublic integer $size;
Specifies the file size, in bytes.
¶ title propertypublic string $title;
Specifies the file tittle.
Method details¶ copy() methodpublic Db_File copy()
Copies the file and returns the new file object.
The returned object is not saved to the database, so its properties can be
updated before it is saved.
¶ create() methodpublic static Db_File create(array $values=NULL)
Creates a new class instance.
¶ fromFile() methodpublic Db_File fromFile(string $file_path)
Creates a file object from a disk file.
Use this method for adding files from disk to a list of model's files.
$file = Db_File::create()->fromFile(PATH_APP.'/temp/picture.png'); $file->is_public = true; $file->master_object_class = 'Shop_Product'; $file->field = 'images'; $file->save(); $product->images->add($file); $product->save(); ¶ getPath() methodpublic string getPath()
Returns the file path relative to LemonStand root directory.
Use PATH_APP constant to obtain the absolute path:
$absolute_path = PATH_APP.$file->getPath(); ¶ getThumbnailPath() methodpublic string getThumbnailPath(mixed $width, mixed $height, boolean $as_jpeg=true, array $params=array( 'mode'=>'keep_ratio'))
Creates an image thumbnail.
This method is applicable only for image files.
The $width and $height parameters could be either integer numbers or the 'auto' word. If you specify an
integer number for the width or height, the thumbnail will have the exact width or height value, in pixels.
Use the 'auto' word to scale an image dimension proportionally. For example, to generate a thumbnail with
fixed width of 100 pixels and proportional height, use the following code:
$url = $file->getThumbnailPath(100, 'auto'); Behavior of this method can be altered by core:onProcessImage event handlers. ¶ is_image() methodpublic boolean is_image()
Returns TRUE if the file is an image.
The method detect images basing on the file extension. The following extensions are considered as images:
jpeg, jpg, gif, png.
¶ output() methodpublic void output(string $disposition='inline')
Outputs the file to the browser.
Depending on the file type and $disposition parameter value a browser either displays the file contents or offers to save it to the disk.
¶ thumb() methodpublic string thumb(mixed $width, mixed $height, boolean $as_jpeg=true, array $params=array( 'mode'=>'keep_ratio'))
This method is an alias for the getThumbnailPath() method.
Creates an image thumbnail.
|