I have a Course model and a User model joined by Subscription. I want to create a postLink form helper on the Courses index page that would automatically subscribe the user to that, but I don't know how to pass the parameters correctly. In essence, I want the user to click Subscribe, and have a subscriptions/add form automatically submitted so that the join table has another record.
My specific question is: How do I use a model in another model's view?
Here's the Courses index.ctp array from print_r:
Array
(
[0] => Array
(
[Course] => Array
(
[id] => 1
[name] => Flying
[created] => 2014-01-27 19:05:43
[modified] => 2014-01-27 19:05:43
)
[Subscription] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 2
[course_id] => 1
)
)
)
)
Here's my bad postLink in the Course index.ctp view:
<?php
echo $this->Form->postLink(__('Subscribe'),
array( 'controller' => 'Subscriptions',
'action' => 'add' ),
null,
__('Are you sure you want to subscribe to # %s?',
$course['Course']['name']));
?>
Assuming they're associated, you can do like you would in the Controller - just don't include the model you're actually in, since $this
is already that model:
$this->AssociatedModel->save($data);