If I understand you correctly, this can absolutely be done (see: Saving Related Model Data in the official documentation). Assuming Post hasMany Image
and Image belongsTo Post
, you'd set it up in the following way.
In your view, you'd create a Post creation form like so:
<?php
$form->create("Post", array('action'=>'add','type'=>'file'));
$form->input("Post.title");
$form->input("Post.body");
$form->input("Image.0.upload", array('type'=>'file', 'label'=>__('Attach an image:',true));
$form->input("Image.1.upload", array('type'=>'file', 'label'=>__('Attach an image:',true));
?>
This defines a quick and dirty form that renders Post.title and Post.body fields, and two file-attachment widgets for two new Images.
Then, in your posts_controller.php
:
class PostsController extends AppController
{
/* stuff before PostsController::add() */
function add()
{
if (!empty($this->data)) {
if ( $this->Post->saveAll( $this->data, array('validate'=>'first'))) {
$this->flash(__("Post added.",true), 5);
}
}
}
/* Stuff after PostsController::add() */
}
Assuming your Post and Image data validates, this will save a new Post, then save the two Images, simultaneously and automatically associating the new Image records with the new Post record.