hi all the data from my dropdown box isn't saving correctly to the database, its saving to the foreign key template_id as null
here is the function
function add(){
$this->Session->setFlash("Please create your required fields.");
$templates = $this->Template->find('all', array('fields' => array('Template.id' )));
$this->set('templates', $templates);
if($this->request->is('post'))
{
$this->Field->create();
if ($this->Field->save($this->request->data))
{
if($this->request->data['submit'] == "type_1")
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'fields','action' => 'add'));
}
if($this->request->data['submit'] == "type_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
}
else
{
$this->Session->setFlash('The field could not be saved. Please, try again.');
}
}
}
here is the view
<?php
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->input('name', array('label'=>'Name: '));
echo $this->Form->input('description', array('label'=>'Description: '));
echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'options' => $templates));
echo $this->Form->button('Continue adding fields', array('name' => 'submit', 'value' => 'type_1'));
echo $this->Form->button('Finish adding fields', array('name' => 'submit', 'value' => 'type_2'));
echo $this->Form->end();
?>
sorry guys it was a long day, it is saving the correcting information.
Related
in this editdrprofile.ctp file not retrieves gender field value and when am click save Drprofile link in editprofile page no action donne page refreshing no image uploaded nothing changed
app/Controller/DashboardsController.php
public function index() {
$this-> loadModel('Drprofile');
$this->set('variable', $this->Drprofile->find('all', array('conditions' => array('Drprofile.user_id' => $this->Auth->user('id')))));
}
public function editdrprofile($id = null) {
$this-> loadModel('Drprofile');
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Drprofile->findByuser_id($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('Drprofile', 'put'))) {
$this->Drprofile->user_id = $id;
// $this->set('posts', $this->carrier->find('all'));
if($this->request->is('post')){
Configure::read();
// pr($this->data);
$this->Carrier->create();
$filename = null;
if (
!empty($this->request->data['Drprofile']['image']['tmp_name'])
&& is_uploaded_file($this->request->data['Drprofile']['image']['tmp_name'])
) {
// Strip path information
$filename = basename($this->request->data['Drprofile']['image']['name']);
move_uploaded_file(
$this->data['Drprofile']['image']['tmp_name'],
WWW_ROOT . DS . 'documents' . DS . $filename
);
//$this->data['Carrier']['Resume'] = $filename;
}
//pr($filename);
// Set the file-name only to save in the database
$this->request->data['Drprofile']['image'] = $filename;
pr($this->data);
if ($this->Drprofile->save($this->request->data)) {
// ...
/*if ($this->Carrier->save($this->request->data)) {
if ($this->Carrier->save($this->data)) {
*/
$this->Session->setFlash(__('Your Details has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Unable to add your Details'));
}
}
/*pr_('$this->Drprofile->user_id = $id');
if ($this->Drprofile->save($this->request->data)) {
//$this->Drprofile->save($this->request->data);
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));*/
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
in model
app/model/Drprofile.php
<?php class Drprofile extends AppModel {
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
?>
in view/dashboards/index.ctp
<?php
foreach ($variable as $post1):
?>
<table>
<tr><h3>Doctor Profile</h3></tr>
<tr> <td>TTTTTTTTTTTTTTTTTTTTTTTTTTTT</td> <td><table>
<tr><td>Name</td><td><?php echo $post1['User']['fullname'];?></td></tr>
<tr><td>Email</td><td><?php echo $post1['User']['email'];?></td></tr>
<tr><td>Mobile</td><td><?php echo $post1['User']['contactnumber'];?></td></tr>
<tr><td>Gender</td><td><?php echo $post1['User']['gender'];?></td></tr>
<tr><td>D.O.b</td><td><?php echo $post1['Drprofile']['dob'];?></td></tr>
<tr><td>Experience</td><td><?php echo $post1['Drprofile']['exp'];?></td></tr>
</table></td></tr>
</table>
<?php
echo $this->Html->link(
'Edit Profile', array('action' => 'editdrprofile', $post1['Drprofile']['user_id'])
);
?>
<?php
endforeach; ?>
app/view/editdrprofile.ctp
<h1>Edit Post</h1>
<?php
echo $this->Form->create('Drprofile');
?>
<table>
<tr><h3>Edit profile</h3></tr>
<tr><td>Name</td><td><?php echo $this->Form->text('User.fullname'); ?></td></tr>
<tr><td>Email</td><td><?php echo $this->Form->text('User.email'); ?></td></tr>
<tr><td>Mobile</td><td><?php echo $this->Form->text('User.contactnumber'); ?></td></tr>
<tr><td>Gender</td><td><?php
$options=array('M'=>'Male','F'=>'Female');
$attributes=array('legend'=>false);
echo $this->Form->radio('User.gender',$options,$attributes);
?></td></td></tr>
<tr><td>D.O.b</td><td><?php echo $this->Form->text('dob'); ?></td></tr>
<tr><td>Experience</td><td><?php echo $this->Form->select('exp', array('options' => array('1 year','2 years ','3 years','4 years','5-10 years'))); ?></td></tr>
<tr><td><?php echo $this->Form->input('drprofile.Resume', array('between'=>'<br />','type'=>'file'));?></td></tr>
<tr><td><?php echo $this->Form->end('Save Drprofile');?></td></tr>
<?php /*?><?php echo $this->Form->input('id', array('type' => 'hidden'));?><?php */?>
</table>
First thing you are doing this in Dashboards controller, and you are creating data for Drprofile. If you in the end you want to do it in DashboardsController then you should change your from to this:
echo $this->Form->create('Drprofile', array(
'url' => array('controller' => 'dashboards', 'action' => 'editdrprofile')
));
This way you are telling form what action to use. But I would suggest you move that to DprofilesController and edit that data there.
One more thing, you closed your form there and you place $this->Form->input for id after it, change that.
I'm using the cakephp uploader plugin by Miles Johnson. I like the plugin and I've got it working to be able to upload photos for a photo model.
The problem I'm running into is that when I try to edit an existing record, instead of replacing the photo associated with the record I'm editing a new record is created. The new record has all the same information with the exception of the new photo that's been uploaded.
Here's how the photo model looks:
'Uploader.Attachment' => array(
'imgPath' => array(
// 'name' => '', // Name of the function to use to format filenames
'baseDir' => '', // See UploaderComponent::$baseDir
'uploadDir' => 'files/uploads/', // See UploaderComponent::$uploadDir
'dbColumn' => 'imgPath', // The database column name to save the path to
'maxNameLength' => 60, // Max file name length
'overwrite' => false, // Overwrite file with same name if it exists
'stopSave' => true, // Stop the model save() if upload fails
'allowEmpty' => true, // Allow an empty file upload to continue
'transforms' => array( // What transformations to do on images: scale, resize, etc
array(
'method' => 'resize',
'width' => 50,
'height' => 50,
'append' => '_thumb',
'dbColumn' => 'imgPathThumb',
)
)
)
)
This is the photo admin edit view form:
<?php echo $this->Html->script('ckeditor/ckeditor');?>
<?php echo $this->Form->create('Photo', array('type' => 'file'));?>
<fieldset>
<legend><?php echo __('Admin Edit Photo'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('caption', array('class'=>'ckeditor'));
echo $this->Form->input('imgPath', array('type' => 'file'));
echo $this->Form->input('alt_tag');
echo $this->Form->input('type', array( 'label' => 'Choose a type of photo', 'options' => array(
'gallery'=>'gallery',
'news'=>'news',
'post'=>'post',
)));
echo $this->Form->input('active', array( 'label' => 'Make active', 'options' => array('yes'=>'yes','no'=>'no' )));
echo $this->Form->input('gallery_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
This is the controller admin edit code:
public function admin_edit($id = null) {
$this->layout = 'admin';
if (!$this->Photo->exists($id)) {
throw new NotFoundException(__('Invalid photo'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Photo->save($this->request->data)) {
$this->Session->setFlash(__('The photo has been saved.'), 'success');
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The photo could not be saved. Please, try again.'), 'error');
}
} else {
$options = array('conditions' => array('Photo.' . $this->Photo->primaryKey => $id));
$this->request->data = $this->Photo->find('first', $options);
}
$galleries = $this->Photo->Gallery->find('list');
$this->set(compact('galleries'));
}
Have I overlooked something obvious?
Cheers, Paul
When You editing record, You must set ID of this record.
Add to form $this->Form->input('id') or set ID in controller before save() action
Hi all I'm trying to create a form where a user can create a new user which will have the same account_id as the person creating the user.
currently my page is displaying the correct account_id in the form but when we submit the form to the database it saves as the wrong thing.
here is my function
function add_Employee(){
$accounts=$this->User->find('list', array(
'fields'=>array('account_id'),
'conditions' => array(
'User.id' => $this->Auth->user('id'))));
if($this->request->is('post')){
$this->User->create();
if ($this->User->save($this->request->data))
{
$this->Session->setFlash('The user has been saved');
$this->redirect( array('controller'=>'Users','action' => 'index_admin'));
}
else { $this->Session->setFlash('The user could not be saved. Please, try again.'); }
}
$this->set('accounts',$accounts);
}
and the view
<h2>Add Employee</h2>
<p>Add a new Employee here, please enter their details below.</p>
<?php
echo $this->Form->create('User', array('action'=>'add_Employee'));
echo $this->Form->input('username',array('label'=>'Username: '));
echo $this->Form->input('password',array('label'=>'Password: '), array('type'=>'password'));
echo $this->Form->input('password_confirmation',array('label'=>'Confirm: '), array('type'=>'password'));
echo $this->Form->input('email',array('label'=>'Email: '));
echo $this->Form->input('title',array('label'=>'Title: '));
echo $this->Form->input('firstname',array('label'=>'First Name: '));
echo $this->Form->input('surname',array('label'=>'Surname: '));
echo $this->Form->input('active', array('default'=>true, 'type'=>'hidden'));
echo $this->Form->input('account_id', array('value'=>$accounts['User']['account_id']));
echo $this->Form->input('access_level', array(
'label' => 'Access Level:',
'options' => array('1'=>'Employee','2'=>'Adminstrator')));
echo $this->Form->end('Submit');
?>
you have to put account id as value in form :
In controller:
$accounts=$this->User->find('first', array(
'conditions' => array(
'id' => $this->Auth->user('id'))));
$this->set('account',$accounts);
In view :
echo $this->Form->input('account_id', array('label'=>'Account','value'=>$account['User']['account_id']));
my function puts the correct value into the form for my view but doesn't save the correct value in the database. The database is saving the accounts_users.id NOT the accounts_users.account_id to my the code looks like it should be entering the persons accounts_users.account_id. I see the correct information in the dropdown box in the form
add function
function add(){
$accounts=$this->User->AccountsUser->find('list', array(
'fields'=>array('id','account_id'),'conditions' => array(
'user_id' => $this->Auth->user('id'))));
if($this->request->is('post')){
$this->Template->create();
if ($this->Template->save($this->request->data)) {
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
} else {
$this->Session->setFlash('The template could not be saved. Please, try again.');
}
}
here is the add view
<?php
echo $this->Form->create('Template', array('action'=>'add'));
echo $this->Form->input('name',array('label'=>'Template Name: '));
echo $this->Form->input('account_id',array('label'=>'Business: ', 'type' => 'select', 'options' => $accounts));
echo $this->Form->input('description',array('label'=>'Short Description Of Template: '));
echo $this->Form->end('Click Here To Submit Template');
?>
Try the following code:
function add(){
$accounts=$this->User->AccountsUser->find('list', array(
'fields'=>array('account_id','account_id'),'conditions' => array(
'user_id' => $this->Auth->user('id'))));
if($this->request->is('post')){
$this->Template->create();
if ($this->Template->save($this->request->data)) {
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
} else {
$this->Session->setFlash('The template could not be saved. Please, try again.');
}
}
hi all i have page that has two buttons, one should allow the person go to the add page and continue adding to the database otherwise if they click the other button it goes to the index page.
currently they both just add the entered information into the database and refresh the page, so when a person clicks the type_2 button they aren't being taken to the index page.
here is the if statement in controller
if ($this->Field->save($this->request->data))
{
if($this->params['form']['type_1'] == 'type_1')
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
}
else if($this->params['form']['type_2'] == 'type_2')
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Templates','action' => 'index'));
}
}
here is the view
<?php
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->input('name', array('label'=>'Name: '));
echo $this->Form->input('description', array('label'=>'Description: '));
echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text'));//this would be the conventional fk fieldname
echo $this->Form->button('Continue adding fields', array('name' => 'type', 'value' => 'type_1'));
echo $this->Form->button('Finish adding fields', array('name' => 'type', 'value' => 'type_2'));
echo $this->Form->end();
?>
Your if conditions are wrong, you're checking for indices ['form']['type_1'] and ['form']['type_2'], this should be ['form']['type'] in both occasions, and then you check for their value, so it becomes:
if ($this->Field->save($this->request->data))
{
if($this->params['form']['type'] == 'type_1')
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
}
else if($this->params['form']['type'] == 'type_2')
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Templates','action' => 'index'));
}
}
had to use request instead of params
if ($this->Field->save($this->request->data))
{
if($this->request->data['submit'] == "type_1")
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'fields','action' => 'add'));
}
if($this->request->data['submit'] == "type_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
}