On Tue, Mar 8, 2011 at 3:27 PM, grady <[email protected]> wrote:
> I posted a question similar to the following on the unofficial CakePHP
> forums over a week ago and haven't received any sort of response, and
> then I noticed the google group so here is my second attempt...
>
> I'm working on a site mainly to get my feet wet with CakePHP and I
> realized that I am unsure of how to go about laying out my database
> tables to play nice with CakePHP.
>
> Basically my site centers around artifacts, which have names,
> descriptions, and one or more assets (images, sounds, videos, etc.)
> related to them.  In my database I have an artifact table and an asset
> table.  I have set up the relationship so that the artifact
> hasAndBelongsToMany assets (and vic versa).  (So I also have an
> artifacts_assets table to accommodate this).  So far so good.

Anothre approach would be to have models (and tables) for Image,
Video, etc. Then give your Artifact model a $haveMany assoc. to these,
and each of the $belongsTo Artifact. The results of a search on
Artifact will come back with empty arrays for any of the assoc. where
this particular Artifact has no examples, eg. an Artifact with no
Videos will have an empty Video array.

Or, you could create an Asset class that is extendable (search for
ExtendableBehavior). Your assets table would then have columns for
model and foreign_key, which would contain, eg. 'Image' and an
Image.id. I use this approach often.

> In addition to an artifact having a couple assets, I would like to be
> able to designate an image file (which is, in my mind, another asset)
> as the preview image for each artifact.  I have set it up so that in
> addition to the HABTM relationship between artifacts and assets, there
> is another relationship -- artifact belongsTo asset which is set up on
> a "preview_id" foreign key in the artifact table.

What I often do when working with images is to have Image, Preview,
and Thumbnail tables. The latter 2 having image_id columns. In this
case, Preview is simply a different size (and perhaps cropped
differently) and each Image has one. It would be used where a
thumbnail would be too small but the Image too large. Say, if rather
than display a series of thumbs to click on to get the larger version
(Image), I wanted to show a series of pics somewhat larger than a
thumb.

Or, closer to your situation, I wanted to have a "preview" image for a
Gallery. A Gallery view would have many Thumbnails to click on to see
the Image, but listing many Galleries would show theGallery.title as
well as a single Preview. In this case, Gallery would have a column,
preview_id.

Of course, if your Artifact "preview" image is the same size as your
Thumbnail, you could skip the Preview model and have
Artifact.thumbnail_id.

> This seems right, but I believe CakePHP is choking on the multiple
> relationships.  For instance, I set up a form using the Form helper
> and setting the value of $this->Form->input("preview_id") doesn't grab
> any of the assets for selection.  This leads me to believe that I am
> going about this the wrong way, and would be grateful to anyone who
> could point me in the right direction.

FormHelper does nothing to fetch the data from the DB. At the point
where your view is being interpreted, your controller/model code
should have already got everything the action requires. FormHelper
does know what to do given certain input names as long as the correct
information is inside $this->data. It's possible that either your
association is incorrect or your find for whatever reason is not
including the other model.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to