Chris, forgive me if I'm not reading your original request properly,
because I seem to be missing something...
In CakePHP, the view.ctp is usually used to display the contents of an
EXISTING record, so having the option to "Create Resume" using this
view doesn't make sense to me. In fact, if you baked the
ResumeController, the view function would look something like this:
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid resume', true));
$this->redirect(array('action' => 'index'));
}
$this->set('resume', $this->Resume->read(null, $id));
}
In the default baked view function, the $id passed is the id of the
resume record, not the user_id in the resume table. So if the resume
doesn't exist, cake redirects to the index action and doesn't display
the resumes/view.ctp.
Assuming the following:
User hasOne Resume
Resume belongsTo User
the dynamic option to Create Resume vs. Edit/Delete Resume seems to be
more appropriate in the users/view.ctp. For example, I would probably
code it like this (note that this code is untested -- just going off
"memory"):
users_controller.php:
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
$this->User->contain('Resume'); // include hasOne Resume in
result
set
$this->set('user', $this->User->read(null, $id));
}
users/view.ctp:
<?php
if ($user['Resume']['id']) {
// Resume id exists, so user already has a resume on
file
echo $this->Html->link(__('Edit Resume'), true)),
'/resumes/edit' .
$user['Resume']['id'], array('class' => 'edit'));
echo $this->Html->link(__('Delete Resume'), true)),
'/resumes/
delete/' . $user['Resume']['id'], array('class' => 'delete'));
} else {
// Resume id does not exist, allow the user to create
one.
echo $this->Html->link(__('Create Resume'), true)),
'/resumes/add',
array('class' => 'album'));
}
?>
resumes_controller.php:
function add() {
if (!empty($this->data)) {
$this->Resume->create();
// Force the resume to be associated with the current
logged-in
user (via AuthComponent)
$this->data['Resume']['user_id'] =
$this->Auth->user('id');
if ($this->Resume->save($this->data)) {
$this->Session->setFlash(__('The resume has
been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The resume could
not be saved.
Please, try again.', true));
}
}
}
=====
Since you've defined the function "create" in your
resumes_controller.php, I am assuming you created a "view" function
that differs greatly from the default view function normally baked by
cake (the default function name when "baked" is "add"). I'll make the
big jump and assume that your view function is defined something like:
function view() {
$user_id = $this->Auth->user('id');
$this->set('resume', $this->Resume->find('first',
array('user_id' =>
$user_id)));
}
where you are acessing the resume record based on the user_id and not
the id of the resume record. If that is the case, you can basically
check if the id (of the resume record, not the user_id) is null. If
the id is null, it indicates that a resume record was not found, so
display the "create" link; otherwise display the "edit" and "delete"
links.
You might want to consider this alternative: in the controller,
instead of immediately saving the result set of the find() to the view
variable, save it to a local variable (i.e. $resume), check if the id
is defined; if not, display a session flash message indicating that
there is no resume on file, then redirect to the create action,
otherwise, save the local variable to the view variable and resume as
normal. That way, you don't have to deal with displaying the "name"
and "content" field headers with blank values and checking whether to
display the create vs. edit/delete links -- the user will be shown the
form to enter their resume info, avoiding the extra step of having to
click the "Create Resume" button; s/he can just as easily navigate
away from that page as s/he would from your original (blank) view.ctp.
On Jan 23, 2:59 pm, "[email protected]" <[email protected]> wrote:
> Hi guys,
>
> I wanna to make a statement in ../views/resume/view.ctp
>
> I have a table named: resume
>
> id int(11) UNSIGNED No auto_increment
> user_id int(11) Yes NULL
> name varchar(120) latin1_swedish_ci No
> content text latin1_swedish_ci No
>
> and in view... when resume is created, link to "Create Resume" will be
> replaced with "Edit Resume" and "Delete Resume"
>
> How do I do that...?
>
> What I have is manage to do is just a links:
>
> <span style="float: right; margin: 0 5px 0 0;">
> <?php echo $html->link(ucfirst(__('create Resume', true)), '/resumes/
> create', array('class' => 'album'), false, false, false) ?>
>
> <?php echo $html->link(__('edit Resume', true), '/resumes/
> edit/' . $resume['Resume']['id'], array('class' => 'edit'), false,
> false) ?>
>
> <?php echo $html->link(__('delete Resume', true), '/resumes/
> delete/' . $resume['Resume']['id'], array('class' => 'delete'), false,
> false) ?>
> </span>
>
> Mucho Thanks in advance ... !!!
> Chris
On Jan 23, 2:59 pm, "[email protected]" <[email protected]> wrote:
> Hi guys,
>
> I wanna to make a statement in ../views/resume/view.ctp
>
> I have a table named: resume
>
> id int(11) UNSIGNED No auto_increment
> user_id int(11) Yes NULL
> name varchar(120) latin1_swedish_ci No
> content text latin1_swedish_ci No
>
> and in view... when resume is created, link to "Create Resume" will be
> replaced with "Edit Resume" and "Delete Resume"
>
> How do I do that...?
>
> What I have is manage to do is just a links:
>
> <span style="float: right; margin: 0 5px 0 0;">
> <?php echo $html->link(ucfirst(__('create Resume', true)), '/resumes/
> create', array('class' => 'album'), false, false, false) ?>
>
> <?php echo $html->link(__('edit Resume', true), '/resumes/
> edit/' . $resume['Resume']['id'], array('class' => 'edit'), false,
> false) ?>
>
> <?php echo $html->link(__('delete Resume', true), '/resumes/
> delete/' . $resume['Resume']['id'], array('class' => 'delete'), false,
> false) ?>
> </span>
>
> Mucho Thanks in advance ... !!!
> Chris
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php