Skip to content

Commit b8bcee3

Browse files
committed
Documentation for polymorphic usage of the Upload plugin
1 parent 2c3be06 commit b8bcee3

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

README.textile

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,105 @@ class User extends AppModel {
150150

151151
h3. PDF Support
152152

153-
It is now possible to generate a thumbnail for the first page of a PDF file. (Only works with 'imagick' thumbnailMethod.)
153+
It is now possible to generate a thumbnail for the first page of a PDF file. (Only works with 'imagick' @thumbnailMethod@.)
154154

155155
Please read about the Behavior options for more details as to how to configure this plugin.
156156

157+
h3. Using a Polymorphic Attachment Model for File Storage
158+
159+
In some cases you will want to store multiple file uploads for a multiple models, but will not want to use multiple tables for some reason. For example, we might have a @Post@ model that can have many images for a gallery, and a @Message@ model that has many videos. In this case, we would use an @Attachment@ model:
160+
161+
@Post hasMany Attachment@
162+
163+
We could use the following database schema for the @Attachment@ model:
164+
165+
<pre><code>CREATE table attachments (
166+
`id` int(10) unsigned NOT NULL auto_increment,
167+
`model` varchar(20) NOT NULL,
168+
`foreign_key` int(11) NOT NULL,
169+
`name` varchar(32) NOT NULL,
170+
`attachment` varchar(255) NOT NULL,
171+
`dir` varchar(255) DEFAULT NULL,
172+
`type` varchar(255) DEFAULT NULL,
173+
`size` int(11) DEFAULT 0,
174+
`active` tinyint(1) DEFAULT 1,
175+
PRIMARY KEY (`id`)
176+
);
177+
</code></pre>
178+
179+
Our attachment records would thus be able to have a name and be activated/de-activated on the fly. The schema is simply an example, and such functionality would need to be implemented within your application.
180+
181+
Once the @attachments@ table has been created, we would create the following model:
182+
183+
<pre><code><?php
184+
class Attachment extends AppModel {
185+
var $name = 'Attachment';
186+
var $actsAs = array(
187+
'Upload.Upload' => array(
188+
'attachment' => array(
189+
'thumbnailSizes' => array(
190+
'xvga' => '1024x768',
191+
'vga' => '640x480',
192+
'thumb' => '80x80',
193+
),
194+
),
195+
),
196+
);
197+
198+
var $belongsTo = array(
199+
'Post' => array(
200+
'className' => 'Post',
201+
'foreignKey' => 'foreign_key',
202+
),
203+
'Message' => array(
204+
'className' => 'Post',
205+
'foreignKey' => 'foreign_key',
206+
),
207+
);
208+
}
209+
?>
210+
</code></pre>
211+
212+
We would also need to present a valid counter-relationship in the @Post@ model:
213+
214+
<pre><code><?php
215+
class Post extends AppModel {
216+
var $name = 'Post';
217+
var $hasMany = array(
218+
'Image' => array(
219+
'className' => 'Attachment',
220+
'foreignKey' => 'foreign_key',
221+
'conditions' => array(
222+
'Attachment.model' => 'Post',
223+
),
224+
),
225+
);
226+
}
227+
?>
228+
</code></pre>
229+
230+
The key thing to note here is the @Post@ model has some conditions on the relationship to the @Attachment@ model, where the @Attachment.model@ has to be @Post@. Remember to set the @model@ field to @Post@, or whatever model it is you'd like to attach it to, otherwise you may get incorrect relationship results when performing find queries.
231+
232+
We would also need a similar relationship in our @Message@ model:
233+
234+
<pre><code><?php
235+
class Message extends AppModel {
236+
var $name = 'Message';
237+
var $hasMany = array(
238+
'Video' => array(
239+
'className' => 'Attachment',
240+
'foreignKey' => 'foreign_key',
241+
'conditions' => array(
242+
'Attachment.model' => 'Message',
243+
),
244+
),
245+
);
246+
}
247+
?>
248+
</code></pre>
249+
250+
Please note that this is not the only way to represent file uploads, but it is documented here for reference.
251+
157252
h2. Behavior options:
158253

159254
* @pathMethod@: The method to use for file paths. This is appended to the @path@ option below

0 commit comments

Comments
 (0)