Reputation: 166
I've been trying to figure this problem out all morning. Tried some things from other questions, but it either didn't really apply to my situation or didn't work. I have two tables:
users = (id,name,username,password,roles,last_edit,language)
french_translations = (id, french_clinical_recommendations, french_tradenames, include_drug, french_category, drug_id, user_id)
User hasMany french_translations and french_translations belongsTo User.
When a user adds or edits a french_translation I want it to save the user id in the french_translations table, the field user_id, for that record, and then put the generic name for a drug in the last_edit field in the users table. Right now it creates a new record in each table. In the users table, everything is blank except for the id and last_edit field(Which puts the correct drug name in the field). And the french_translations table has a record with blanks, with the user_id being the same as the blank one created in the users table.
Controller:
function add($id = null) {
$userid = $session->read('Auth.User.id');
$drug = $this->FrenchTranslation->Drug->read(
array(
'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations',
'Drug.category','Drug.lrc'
),
$id
);
$this->set('user',$userid);
$this->set('drug',$drug);
if (!empty($this->data)) {
$french_translation['FrenchTranslation']['id'] = $this->Session->read('id');
$this->FrenchTranslation->create();
if ($this->FrenchTranslation->save($this->data)) {
$this->Session->setFlash(__('The french translation has been saved', true));
$this->redirect(array('controller'=>'drugs','action' => 'index'));
} else {
$this->Session->setFlash(__('The french translation could not be saved. Please, try again.', true));
}
}
$drugs = $this->FrenchTranslation->Drug->find('list');
$this->set(compact('drugs'));
}
function edit($id = null) {
//$this->FrenchTranslation->id = $id;
$userid = $this->Auth->user('id');
$username = $this->Auth->user('name');
//$this->FrenchTranslation->user_id = $id;
$drug = $this->FrenchTranslation->Drug->read(
array(
'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations',
'Drug.category','Drug.lrc'
),
$id
);
$this->set('drug',$drug);
$this->set('user',$userid);
$this->set('username',$username);
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid french translation', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->FrenchTranslation->saveAll($this->data)) {
$this->Session->setFlash(__('The french translation has been saved', true));
$this->redirect(array('controller'=>'drugs','action' => 'index'));
} else {
$this->Session->setFlash(__('The french translation could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->FrenchTranslation->read(null, $id);
}
$drugs = $this->FrenchTranslation->Drug->find('list');
$this->set(compact('drugs'));
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for french translation', true));
$this->redirect(array('action'=>'index'));
}
if ($this->FrenchTranslation->delete($id)) {
$this->Session->setFlash(__('French translation deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('French translation was not deleted', true));
$this->redirect(array('action' => 'index'));
}
Edit View:
<?php echo $this->Form->input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']['generic'])); ?>
<?php echo $this->Form->input('user_id', array('type'=>'hidden','value'=>$user)); ?>
Upvotes: 2
Views: 1651
Reputation: 2819
This is a bit of a late answer, but you should never use the view to set secure variables like user_id
. Instead, you can set these variables in your controller:
$this->request->data['User']['last_edit'] = $this->request->data['Drug']['generic'];
Upvotes: 1
Reputation: 166
I figured it out, however, I feel like there's a better and more secure, way of doing it. I am using the security component, and a person has to login to see any of the site, so it's probably safe enough.
I simply added a user.id input for the user id, at that point it could find the appropriate user to store the last_edit into. In the controller I have a variable called $userid which gets the current user id using $userid = $this->Auth->user('id');
and is passed to the view using $ths->set('user',$userid);
I used that same user id value in the input for the drugs user_id field.
View:
<?php echo $this->Form->input('User.id',array('type'=>'hidden','value'=>$user));//The user ID last edit will be stored in ?>
<?php echo $this->Form->input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']['generic']));//The drug edited last by the specified user ?>
Upvotes: 0