Before I would just ask user to confirm if he/she really wants to delete any instance like this:
$this->Html->link($this->Html->image('delete.png', array(
'alt'=>'delete', 'title'=>__('Delete'))),
array(
'controller' => 'users', 'action' => 'delete', $user['User']['id']),
array(
'escape'=>false, 'confirm'=>'Are you sure, you want to delete this user?'));
Right now user hasMany events, and I would like to check if he/she really does. That's no problem, so in the controller I would just try to get the first event his that user's id. But then, if there are any events, I would like to notify the users, that the deletion is not possible because there are related events.
I can go around and come up with some custom javascript solution, but there has to be a cake way to do it, it is just I cant find any.
Any suggestions?
Here is the controller action as of right now:
public function delete($id = null,$user_id = null) {
if (!$id) {
$this->Session->setFlash(__('Incorrect user id'));
$this->redirect(array('action'=>'index'));
}
if ($this->User->delete($id)) {
$this->Session->setFlash(__('User has been deleted'), 'positive_notification');
$this->redirect(array('controller'=>'full_calendar', 'action'=>'view_for', $user_id ));
}
$this->Session->setFlash(__('The user could not be deleted. Please try again.'), 'negative_notification');
$this->redirect(array('action' => 'index'));
}
Given that you have properly set up the model relationships like this:
//User.php
public $hasMany = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'user_id'
),
//Event.php
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
);
public function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Incorrect user id'));
$this->redirect(array('action'=>'index'));
}
//check if User has Events
$user=$this->User->findById($id); //you can debug first the $user so you can know the values inside the array. Given that it has many Events, events associated to the User is in the $user
if(count($user["Event"])==0){ //if user has no events
if ($this->User->delete($id)) { //delete user
$this->Session->setFlash(__('User has been deleted'), 'positive_notification');
$this->redirect(array('controller'=>'full_calendar', 'action'=>'view_for', $user_id ));
}
else{
$this->Session->setFlash(__('The user could not be deleted. Please try again.'), 'negative_notification');
$this->redirect(array('action' => 'index'));
}
}
else{
$this->Session->setFlash(__('The user could not be deleted. Some events are associated to this User.'), 'negative_notification');
$this->redirect(array('action' => 'index'));
}
}
Related
I m having trouble in updating the student profile form,after i gave the user_id to the student profile.Every time when i try to edit the form, on saving it provide new id to the existing user. I don't know where I'm getting wrong. Plz help.Thanks in Advance.
Here is my add and edit function in Student Profiles controller:
public function add() {
if ($this->request->is('post')) {
$this->StudentProfile->create();
$data = $this->request->data;
$data['StudentProfile']['user_id'] = $this->Auth->user('id');
// code implemented below
//$this->loadModel('User');
if ($this->StudentProfile->save($data)) {
//Update user here if Profile saved successfully
//$this->StudentProfile->id = $this->Auth->user('id');
$this->Session->setFlash(__('Your account profile has been created'));
$this->redirect(array('controller' => 'studentprofiles', 'action' => 'index'));
}else{
$this->Session->setFlash(__('Your Profile was saved, but an error has occurred while updating the Users table'));
//Email your self the user ID here or something ??
}
}
$h=array(
'fields' => array('Height.height'),
'recrusive' => 0
);
$this->loadModel('Height');
$height = $this->Height->find('list',$h);
$this->set(compact('height'));
}
public function edit($id = null) {
if (!$this->StudentProfile->exists($id)) {
throw new NotFoundException(__('Invalid student profile'));
}
if ($this->request->is(array('post', 'put'))) {
$this->request->data['StudentProfile']['user_id'] = $this->Auth- >user('id');
// code implemented below
// $this->loadModel('User');
if ($this->StudentProfile->save($this->request->data)) {
//$this->StudentProfile->id = $this->Auth->user('id');
$this->Session->setFlash(__('The student profile has been saved.'), 'default', array('class' => 'alert alert-success'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The student profile could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
}
} else {
$options = array('conditions' => array('StudentProfile.' . $this- >StudentProfile->primaryKey => $id));
$this->request->data = $this->StudentProfile->find('first', $options);
}
$h=array(
'fields' => array('Height.height'),
'recrusive' => 0
);
$this->loadModel('Height');
$height = $this->Height->find('list',$h);
$this->set(compact('height'));
}
Can't believe I'm replying to my own question . Well In case anyone have the same above problem,the solution is just add the following code after this-
$this->request->data['StudentProfile']['user_id'] = $this->Auth->user('id');
// add this code to avoiding giving new id to existing user on edit:
$this->request->data['StudentProfile']['id'] = $id;
A better way of saving edited information is to set the ID on the model before the save like so
$this->StudentProfile->id = $this->Auth->user('id');
If you initiate a save after setting the above then the row will be edited and not add a new row.
I am trying to get my edit to work I need the contact detail data to load when the user data loads. I have set the data in a similar manner to how I am retrieving the list of roles. I also don't know how to retrieve according to the model currently I was hard-coding it to retrieve 28. Would greatly appreciate any help provided.
public function edit($id = null) {
//Populate roles dropdownlist
$data = $this->User->Role->find('list', array('fields' => array('id', 'name')));
$this->set('roles', $data);
$data2 = $this->User->ContactDetail->find('first', array(
'conditions' => array('ContactDetail.id' =>'28')));
$this->set('contactdetails', $data2);
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
my view is set up in the following manner
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Edit User'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('role_id');
echo $this->Form->input('ContactDetail.name');
echo $this->Form->input('ContactDetail.surname');
echo $this->Form->input('ContactDetail.address1');
echo $this->Form->input('ContactDetail.address2');
echo $this->Form->input('ContactDetail.country');
echo $this->Form->input('ContactDetail.email');
echo $this->Form->input('ContactDetail.fax');
?>
<label>Are you interested in buying property in Malta?</label>
<?php
$interest_buy = array('0'=>'no','1' => 'yes');
echo $this->Form->input('ContactDetail.interest_buy_property',array('type'=>'radio','options'=>$interest_buy,'value'=>'0','legend'=>FALSE));
?>
<label>Are you interested in renting property in Malta?</label>
<?php
$interest_rent = array('0'=>'no','1' => 'yes');
echo $this->Form->input('ContactDetail.interest_rent_property',array('type'=>'radio','options'=>$interest_rent,'value'=>'0','legend'=>FALSE));
echo $this->Form->input('ContactDetail.mobile');
echo $this->Form->input('ContactDetail.phone');
echo $this->Form->input('ContactDetail.postcode');
echo $this->Form->input('ContactDetail.town');
echo $this->Form->input('ContactDetail.newsletter',array('type'=>'checkbox','label'=>'Would you like to register for the newsletter?' ,'checked'=>'1','legend'=>FALSE,));
?>
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
User Model
public $primaryKey = 'id';
public $displayField = 'username';
public function bindNode($user) {
return array('model' => 'Role', 'foreign_key' => $user['User']['role_id']);
}
public function beforeSave($options = array()) {
$this->data['User']['password'] = AuthComponent::password(
$this->data['User']['password']
);
return true;
}
public $belongsTo = array(
'Role' => array('className' => 'Role'));
public $hasOne = array(
'ContactDetail' => array(
'foreignKey' => 'id'));
public $actsAs = array('Acl' => array('type' => 'requester', 'enabled' => false));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['role_id'])) {
$roleId = $this->data['User']['role_id'];
} else {
$roleId = $this->field('role_id');
}
if (!$roleId) {
return null;
} else {
return array('Role' => array('id' => $roleId));
}
}
}
ContactDetail Model
public $primaryKey = 'id';
public $displayField = 'name';
If I get you right, you have 1 contact details row for each user. In this case, you need to define in your User model:
public $hasOne = array(
'ContactDetail' => array(
'foreignKey' => 'id',
),
);
Like this, your ids will be synced in both tables. If you don't want to make the id a foreign key, you don't have to, it's just a suggestion.
Next, when you retrieve your data in the controller, you can do:
$this->User->Behaviors->load('Containable');
$user = $this->User->find('first', array(
'conditions' => array('User.id' => $id),
'contain' => array('ContactDetail'),
));
Now I don't know if there is an automated way to do this, but I sort my data manually to fill in the inputs. I am guessing you will get a structure like array('User' => array(), 'ContactDetail' => array()).
$user['User']['ContactDetail'] = $user['ContactDetail'];
unset($user['ContactDetail']);
$this->request->data = $user;
Then in your view just set the fields as the input array:
$this->Form->create('User');
$this->Form->input('User.some_user_field');
$this->Form->input('User.ContactDetail.some_contact_detail_field');
This should fill in your fields. When you go save your data, if your array is structured like this, you can use saveAssociated():
$this->User->saveAssociated($this->request->data, array('deep' => true));
EDIT
In case your relation is defined as User hasMany ContactDetail, then you need to structure your data like this:
$this->request->data = array(
'User' => array(
// user data
'ContactDetail' => array(
[0] => array(
//contact data
),
),
),
);
And in your view:
$this->Form->input('User.ContactData.0.field')
This is for 1 row only, If you need more rows on the child table with 1 input, do your logic accordingly.
Once your User model hasOne ContactDetail, you don't need retrieve the information twice, since you've done in $this->request->data line, all association model will retrieve too.
So, your Controller looks like this:
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
// Roles
$roles = $this->User->Role->find('list', array('fields' => array('id', 'name')));
$this->set(compact('roles');
if ($this->request->is(array('post', 'put'))) {
if ($this->User->saveAll($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
}
}
And your View, for ContactDetail's fields looks like this:
echo $this->Form->input('ContactDetail.name');
And so for all the fields related to the model ContactDetail. You can find more details here: Saving Your Data.
User model:
public $hasOne = array(
'ContactDetail' => array(
'className' => 'ContactDetail',
'foreignKey' => 'user_id',
'dependent' => true
)
);
The best solution I have found is by setting
public $belongsTo = array(
'Role' => array('className' => 'Role')
, 'ContactDetail' => array('className' => 'ContactDetail'));
This way the contact detail data loads. Although the data once saved does not update contactdetails.
For me to save I used
$this->User->saveAll($this->request->data)
this worked fully
I have the next tables in the database:
Lift
Vehicle
Manufacturer
Lift belongsTo Vehicle
Vehicle belongsTo Manufacturer
Manufacturer hasMany Vehicles
LiftsController.php add method:
public function add() {
if ($this->request->is('post')) {
$this->Lift->create();
$this->request->data['Lift']['user_id'] = $this->Auth->user('id');
if ($this->Lift->save($this->request->data)) {
$this->Session->setFlash(__('The lift has been saved'));
$this->redirect(array('action' => 'my_lifts'));
} else {
$this->Session->setFlash(__('The lift could not be saved. Please, try again.'));
}
}
$vehicles = $this->Lift->Vehicle->find('all', array('fields' => array('id', 'model', 'manufacturer_id')));
$towns = $this->Lift->TownFrom->find('list', array('order' => 'county ASC, name ASC', 'fields' => array('id', 'name', 'county')));
$this->set(compact('users', 'vehicles', 'towns'));
}
LiftsController.php add view:
echo $this->Form->input('vehicle_id');
A printscreen:
So what I need is to display instead of Manufacturer ID's the manufacturer name.
In every model the $displayField is set to the correct value
Try changing:
echo $this->Form->input('vehicle_id');
To
echo $this->Form->input('Vehicle');
Change
$vehicles = $this->Lift->Vehicle->find('all', array('fields' => array('id', 'model', 'manufacturer_id')));
To
$vehicles = $this->Lift->Vehicle->find('list', array('conditions' => array('Vehicle.user_id' => $this->Auth->user('id')), 'recursive' => 0, 'fields' => array('Vehicle.id', 'Vehicle.model', 'Manufacturer.title')));
I created a site for fun to learn CakePHP but for some reason I can't get a multiple select dropdown box to show my selected items. In this example 1 video game can have multiple difficulties (easy, normal, hard). On my Game edit page I have a multi select box to pick the difficulties. It shows all three difficulties and I can select them and it saves correctly. However when I return to the edit page the items I previously saved do not show highlighted as selected. I have verified that the records are saving correctly in the database.
Tables:
games
difficulties
difficulties_games
Models:
class Game extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
'Difficulty' =>
array(
'className' => 'Difficulty',
'joinTable' => 'difficulties_games',
'foreignKey' => 'game_id',
'associationForeignKey' => 'difficulty_id',
'unique' => 'true'
)
);
}
class Difficulty extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
'Game' =>
array(
'className' => 'Game',
'joinTable' => 'difficulties_games',
'foreignKey' => 'difficulty_id',
'associationForeignKey' => 'game_id',
'unique' => 'true'
)
);
}
Controller:
$game = $this->Game->findById($id);
$this->set('difficulties', $this->Game->Difficulty->find('list'));
View (edit.ctp):
echo $this->Form->input('Difficulty');
This has to be something simple I am missing but I've read through the book on HABTM and searched here and couldn't find much on multi-select boxes.
UPDATE:
Here is the entire edit function in the controller:
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$game = $this->Game->findById($id);
if (!$game) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->Game->id = $id;
if ($this->Game->saveAll($this->request->data)) {
$this->Session->setFlash('Your game has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash($this->Game->invalidFields());
}
}
if (!$this->request->data) {
$this->request->data = $game;
}
$this->set('systems', $this->Game->System->find('list'));
$this->set('genres', $this->Game->Genre->find('list'));
$this->set('difficulties', $this->Game->Difficulty->find('list'));
}
Also here is some more on the View:
echo $this->Form->create('Game');
echo $this->Form->input('name');
echo $this->Form->input('system_id');
echo $this->Form->input('genre_id');
echo $this->Form->input('Difficulty');
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Game');
What CakePHP version are you using? Releases 2.2.6 and 2.3.0 have a bug related to showing existing habtm selected. So updated to 2.2.7 if using 2.2.6 or if using 2.3.0 use the master branch from github until the next bugfix release is done.
For everyone who sets public $recursive = -1; in his AppModel due to performance reasons, or changes the recursion in his controller:
The automagic won't work without recursive 1! Make sure you change it in the options when retreiving your data for the edit.
$options = array(
'recursive' => 1,
'conditions' => array('YourModel.' . $this->YourModel->primaryKey => $id)
);
$this->request->data = $this->YourModel->find('first', $options);
I'm using cakephp 2.
Is it possible to use the form helper postLink function to update a record? Basically I want a "Approve" button that changes the "Approved" field of a record to 1.
Everything I can find only relates to performing a delete function? The docs don't go into much details either.
Any help would be great, thanks in advance
My code:
<?php echo $this->Form->postLink(__('Approve'), array(
'controller' => 'expenseclaims',
'action' => 'edit', $expenseClaim['ExpenseClaim']['id'],
'approved' => '1',
'approved_by' => $adminUser,
), array(
'class' => 'btn btn-danger'
), __('Are you sure you want to Approve # %s?',$expenseClaim['ExpenseClaim']['id']
)); ?>
New code: view post link:
<?php echo $this->Form->postLink(__('Approve'), array('action' => 'approve', $expenseClaim['ExpenseClaim']['id'], 'admin' => true), array('class' => 'btn btn-danger'), __('Are you sure you want to Approve # %s?',$expenseClaim['ExpenseClaim']['id']));
?>
Controller code:
public function admin_approve($id = null) {
debug($this->request);
$this->ExpenseClaim->id = $id;
if (!$this->request->is('post') && !$this->request->is('put')) {
throw new MethodNotAllowedException();
}
if (!$this->ExpenseClaim->exists()) {
throw new NotFoundException(__('Invalid expense claim'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->request->data['ExpenseClaim']['approved'] = '1';
$this->request->data['ExpenseClaim']['approved_by'] = $this->Auth->user('id');
if ($this->ExpenseClaim->save($this->request->data)) {
$this->Session->setFlash('The expense claim has been Approved', 'flash_success');
$this->redirect(array('action' => 'index', 'admin' => true));
} else {
$this->Session->setFlash('The expense claim could not be approved. Please, try again.', 'flash_failure');
}
}
}
yes, of course that is possible.
just post to something like
/expenseclaims/approve/id
to trigger the approve action for example:
public function approve($id = null) {
if (!$this->request->is('post') && !$this->request->is('put')) {
throw new MethodNotAllowedException();
}
//validate/save
}
you can also make it more generic, of course