Question

My models are essentially

  • owner hasOne album
  • asset hasMany album
  • album belonsTo owner
  • album belongsTo asset

A record in 'album' associates an asset to an owner, a 'link', containing extra information such as position, just as the cakephp example does here: hasMany through (The Join Model). Two problems - the docs demo saving via the hasMany/relationship controller, not either end, and in my case, the first model has one record, and the latter has many (in a single save).

When isolated, I can upload multiple assets in one form. The controller then calls saveManyIndividually(), a method inside the asset model, which uploads each asset with a corresponding DB record with a loop.

I'd like to do this when creating a new owner, so the owner form allows multiple assets to be uploaded (with corresponding records saved at once). Then an album (link) record needs to be created per asset record, with the foreign keys being the one, single owner_id through all, and a different asset_id in each.

I can't see a sure way of saveAssociated in the Owner controller doing this. Maybe I could use the asset->saveManyIndividually method in the beforeSave?

I can further clarify is this is unclear.

Was it helpful?

Solution

I think the best way to do this is to separate the operations in the controller:

  1. Create the owner
  2. Upload the assets and save records for them
  3. save associations between the Owner ID and the asset record IDs

I haven't had time to do this yet, but if there's a better way to do so I'll edit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top