0

I have a table files and it belongs to one either post, article, notion or paragrah. Maybe in the future, there will be more.

What relationship do you suggest to use? Multiple HABTM tables, or multiple columns (post_id, aticle_id,...)?

Or maybe two columns parent_alias and parent_id? But how to easily work with this in CakePHP?

Thanks for any suggestions!

Ondrej Henek
  • 854
  • 9
  • 12

2 Answers2

1

this case can be solved using hasOne

just add one type column (varchar) and one foregin_key (integer) in your files table

and bind this model in your post, article ... models using hasOne relation

for post

$hasOne = array('File' => array('foreignKey'=>'foreign_key','conditions'=>array('type'=>'post')))

in article

$hasOne = array('File' => array('foreignKey'=>'foreign_key','conditions'=>array('type'=>'article')))

Just make sure you update 'type' column while saving association

Loken Makwana
  • 3,788
  • 2
  • 21
  • 14
  • Great, thanks! I was just wondering if something like you wrote is possible. I have to use $hasMany, but that doesn't really matter. :) – Ondrej Henek Aug 22 '12 at 20:27
0

We cannot choose for you. It depends of your data.

The question is: can a file belong to several posts?

If yes you'll need a HABTM.

If no BelongsTo will suffice.

L. Sanna
  • 6,482
  • 7
  • 33
  • 47