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']));
Related
I am playing around with the CourseMembership HasMany through example in the CakePHP cookbook but I cant figure out how to add a new Course and multiple entries into CourseMembership (i.e. student_id and grade) all at the same time.
Course hasMany Coursemmembership
Student hasMany Coursemeembership
Coursemembership belongsTo Student, Course
//CoursemmembershipsController
public function add() {
if ($this->request->is('post')) {
$this->Coursemembership->create();
if ($this->Coursemembership->saveAll($this->request->data,array('deep' => true))) {
$this->Session->setFlash(__('The coursemembership has been saved.'));
//return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The coursemembership could not be saved. Please, try again.'));
}
debug($this->request->data);
}
$courses = $this->Coursemembership->Course->find('list');
$students = $this->Coursemembership->Student->find('list');
$this->set(compact('courses', 'students'));
}
__
//Coursemembership/view/add
$this->Form->create('Coursemembership');
echo $this->Form->input('Course.name');
echo $this->Form->input('0.Coursemembership.student_id');
echo $this->Form->input('0.Coursemembership.grade');
echo $this->Form->input('1.Coursemembership.student_id');
echo $this->Form->input('1.Coursemembership.grade');
?>
The data array successfully saves, inserts a new Course fine, but inserts only 1 Coursemembership entry with no student_id or grade.
Data array looks like:
array(
'Course' => array(
'name' => 'Math 101'
),
(int) 0 => array(
'Coursemembership' => array(
'student_id' => '1',
'grade' => '5'
)
),
(int) 1 => array(
'Coursemembership' => array(
'student_id' => '2',
'grade' => '2'
)
)
)
remove Coursemembership from Form input --- Use this
echo $this->Form->create('Coursemembership');
echo $this->Form->input('Course.name');
echo $this->Form->input('0.student_id');
echo $this->Form->input('0.grade');
echo $this->Form->input('1.student_id');
echo $this->Form->input('1.grade');
echo $this->Form->end('save');
Got it! I used the Course controller as recommended, with the following Course/add view and it now works. Thanks
echo $this->Form->input('Course.name');
echo $this->Form->input('Coursemembership.0.student_id');
echo $this->Form->input('Coursemembership.0.grade');
echo $this->Form->input('Coursemembership.1.student_id');
echo $this->Form->input('Coursemembership.1.grade');
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 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.
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.');
}
}