Back to Start Page
With the attachOne function files like images, movies or documents can be attached to a record of a database table. Media attachments are using built-in relations of WinterCMS. So it is very easy to implement them.
Todo Cheat Sheet
Prerequisites are
Required skills
The base model requires to know the type of desired relation as well as the name of the relation. This name can be seen like a field name of a database-table.
The base model is located in the plugin directory, subdirectory models using the singular of the related db-table name. In our example in the namspace pds the plugin library with a book model this is:
/pds/library/models/book.php
Here, the attachOne relation has to be inserted:
These are the lines of code to add for the library example to add images of book covers with a new related field called cover.
Notice, that the chosen name for this new field must not exist already in the parent table where the related image will be attached to!
// add a relation for a SINGLE image, field name is 'cover' // this table must not have a field named 'cover' ! public $attachOne = [ 'cover' => ['System\Models\File', 'public' => false] ];
Next step is to complete the form with an additional field - the File upload widget to upload and display media and/or files:
Finally, in the backend form view of the plugin, a file can be uploaded by clicking on the file upload symbol - the area below the 'Cover Image' title in the example:
Now the form is ready to attach images and/or files to each record.
Back to Start Page