> Hi there,
>
> I'm new to Cake and now I seem to have a little understanding problem.
> The situation is this:
>
> I have a UsersController and a User Model. Then there is an
> editProfile() method within my UsersController. Of course I also
> created a view (users/edit_profile.ctp). My problem is that I want to
> have an editable automagic form on that page that is linked to the
> user_profiles table.
>
> What I did is, I added "var $hasOne = array('UserProfile');" to my
> User Model. And I created a form like this:
>
> echo $form->create('UserProfile');
> echo $form->input('id');
> echo $form->input('name');
> echo $form->end();
>
> Then I created a user_profile.php model file and added "var $belongsTo
> = 'User';" to it. The user_profiles table has a "user_id" foreign key.
>
> When I send the form, cake throws an error as it cannot find the
> "UserProfileController". Normally this would be ok but for I want the
> UsersController to do all the work it's not. What can I do? I mean, I
> could create a user_profiles_controller.php to send the form and
> afterwards do a redirect to the form but that wouldn't be very
> fashionable.
Assuming that the data you want to modify are in the users table, and
that you want to submit the form with AJAX, you can handle it all with
a single action in UsersController.
AppController:
public $components = array('Auth', 'Session', 'RequestHandler', ...);
function beforeFilter()
{
parent::beforeFilter();
// ...
if ($this->RequestHandler->isAjax())
{
Configure::write('debug', 0);
$this->layout = 'ajax';
}
}
UsersController:
function beforeFilter()
{
parent::beforeFilter();
// ...
if ($this->RequestHandler->isAjax())
{
$this->viewPath = 'elements/users';
}
}
public function edit()
{
$id = $this->Auth->user('id');
if (!$id)
{
$this->flash('invalid request');
}
if (!empty($this->data))
{
if ($this->User->save($this->data))
{
if ($this->RequestHandler->isAjax())
{
$this->render('profile');
}
}
else
{
if ($this->RequestHandler->isAjax())
{
$this->render('form');
}
}
}
else
{
$this->data = $this->User->read(null, $id);
}
// set whatever else you need
}
views/elements/users/form.ctp:
<?php
if (!empty($form->validationErrors))
{
?>
<p class="Error">There was an error in the submission. Please review
the fields below.</p>
<?php
}
?>
<p class="Required">* Required Field</p>
<?php
switch ($this->params['action'])
{
case 'admin_add':
echo $form->create('User', array('action' => 'add', 'admin' => 1));
break;
case 'add':
echo $form->create('User', array('action' => 'add'));
break;
case 'admin_edit':
echo $form->create('User', array('action' => 'edit', 'admin' => 1));
echo $form->hidden('User.id');
break;
case 'edit':
echo $form->create('User', array('action' => 'edit'));
echo $form->hidden('User.id');
break;
}
In the case blocks above, you could include your JS link(s) for the
AJAX form stuff. You can also switch on $this->params['action'] for
the submit buttons, test it for whether to include certain fields,
etc. The add, edit, admin_add, admin_edit views all just include the
form element.
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment