error on multi select - cakephp

I am getting the following error when try to save.
SQL Query: INSERT INTO `table`.`users` (`username`, `password`, `group_id`, `applications`, `modified`, `created`) VALUES ('asdasd', '23ad37a839e26fc46a1f6640861a47305aea5d46', 3, Array, '2012-04-03 02:12:19', '2012-04-03 02:12:19')
here is the code for multiple select
echo $this->Form->input('User.applications',array('options'=>$options,'multiple'=>'multiple'));
and the add method is called, which is:
if ($this->request->is('post')) {
//debug($this->request);exit;
$this->User->create();
if ($this->User->saveAll($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
$groups = $this->User->Group->find('list');
$this->set(compact('groups'));
thanks!

solved it using this
public function add() {
if ($this->request->is('post')) {
if(count($this->request->data['User']['applications'])){
$this->request->data['User']['applications'] = implode(",", $this->request->data['User']['applications']);
}
....

Related

Cakephp multiple records using foreach

I am trying to insert multiple records using cakephp.My controller code are given below :
if (!empty($this->request->data)) {
foreach($this->request->data['Deal']['deal_date'] as $key => $data)
{
$this->Deal->create();
$this->Deal->id = $id;
$this->request->data['Deal']['deal_date'] = $data;
$this->request->data['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
if ($this->Deal->save($data)) {
$this->Session->setFlash(__('The deal has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
}
}
}
Its not inserting data into database properly.I used array in all the input fields.Any Idea?
Use saveAll or saveMany
if (!empty($this->request->data)) {
$records = array();
foreach($this->request->data['Deal']['deal_date'] as $key => $data) {
$record = $this->request->data;
$record['Deal']['id'] = $id; // are you sure you need this ?
$record['Deal']['deal_date'] = $data;
$record['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
$records[] = $record;
}
if ($this->Deal->saveMany($records)) {
$this->Session->setFlash(__('The deal has been saved'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
}
}
//try this
if (!empty($this->request->data)) {
$myData = array();
foreach($this->request->data['Deal']['deal_date'] as $key => $data)
{
$this->Deal->create();
$myData['Deal']['deal_date'] = $data;
$myData['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
if ($this->Deal->save($myData)) {
$this->Session->setFlash(__('The deal has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
}
unset($myData);
}
}
For inserting many rows use savemany or saveall as mention by cornelb,
but try to debug why your code is not working by printing sql log
if (!empty($this->request->data)) {
foreach($this->request->data['Deal']['deal_date'] as $key => $data)
{
$this->Deal->create();
$this->Deal->id = $id;
$this->request->data['Deal']['deal_date'] = $data;
$this->request->data['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
if ($this->Deal->save($data)) {
$log=$this->Deal->getDataSource()->getLog(false, false);
echo "<pre>";print_r($log);exit;
$this->Session->setFlash(__('The deal has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
}
}
}

Failure to Update record CakePHP

I am currently trying to update my user before sending an email which will have a confirm link with a resetkey for the user to reset the password.
The problem is that the user is not updating whilst the key is being generated.
the code can be seen hereunder
public function forgotpassword() {
if ($this->request->is('post')) {
$mail = $this->request->data['ContactDetail']['email'];
$data = $this->User->find('first', array('conditions' => array('ContactDetail.email' => $mail)));
if (!$data) {
$message = __('No Such E-mail address registerd with us ');
$this->Session->setFlash($message);
} else {
$data['User']['resetkey'] = Security::hash(mt_rand(),'md5',true);
debug($data);
if ($this->User->saveAssociated($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}
else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
$key = $data['User']['resetkey'];
$id = $data['User']['id'];
$mail = $data['ContactDetail']['email'];
$email = new CakeEmail('default');
$email->to($mail);
$email->from("xxxx#yahoo.com");
$email->emailFormat('html');
$email->subject('Password reset instructions from');
$email->viewVars(array('key' => $key, 'id' => $id, 'rand' => mt_rand()));
$email->template('reset');
if ($email->send('reset')) {
$message = __('Please check your email for reset instructions.');
$this->Session->setFlash($message);
} else {
$message = __('Something went wrong with activation mail. Please try later.');
$this->Session->setFlash($message);
}
}
$this->redirect('/');
}
}
I checked the execution log of MySQL but all I am seeing are selects no updates or even inserts.
This is probably something very dumb but I can't understand why it doesn't say anything.
Why do you use saveAssociated method for update single field?
Something like this would be easier:
$this->User->updateAll(
array( 'User.resetkey' => $hash ),
array( 'User.id' => $userId )
);
try using
$this->User->saveMany($data, array('deep' => true));
When saving. Also
if (empty($data)) {...
Might be a safer way to go
This will work for you-
$this->User->id = $data['User']['id'];
if ($this->User->save($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
Try to debug like:
if ($this->User->saveAssociated($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}
else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
$log=$this->User->getDataSource()->getLog(false, false);
echo "<pre>";print_r($log);exit;

Validation not working with saveMany

Validations are not working with saveMany for all values.its only working for first value. I am using translate behavior.
My code:
public function admin_add() {
if ($this->request->is('post')) {
$this->FaqCategory->create();
$this->request->data['FaqCategory']['name'] = Inflector::slug($this->request->data['FaqCategory']['name'], $replacement = '-');
if ($this->FaqCategory->saveMany($this->request->data)) {
$this->Session->setFlash('The faq category has been saved', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The faq category could not be saved. Please, try again.'));
}
}
$languages = $this->Language->getlangs();
$this->set('langs', $languages);
}
Try this: $this->FaqCategory->saveMany($this->request->data, array('validate' => true));

Edit function is INSERT instead of UPDATE CakePHP 2.1

I'm not exactly new to CakePHP though this is my first app in 2.x. I'm using the baked controller & view and am having a problem with the edit function.
For starters, here is what was baked:
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$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);
}
}
When this is left alone
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
I'm told that I have an invalid user, even when I know without a doubt that the user does exist.
If I change it to this:
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
The correct edit form comes up, and is populated with the correct data, but then upon save it tries to INSERT instead of Update.
Typically this is because CakePHP doesn't have an ID to work with but there is a hidden ID field in the form and I have even tried to force the issue by putting the following before the save.
$this->request->data['User']['id'] = $id;
Any ideas what's going on here?
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$user_data = $this->User->findById($id);
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'), 'flash');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'flash');
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
I got the same problem, code generated by bake, hidden id, etc..
Then I found that the field id on the database was not setted to autoincrement.
I just set to autoincrement and it works

how I want the user id become static

I'm kind new to the cakephp and I want to know how to static the id where the id is on drop down or list. I have do hidden but its not enter the database. This is my coding:
This is in the controller
function add() {
if (!empty($this->data)) {
$this->Post->create();
if ($this->Post->save($this->data)) {
$this->Session->setFlash(__('The post has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
$this->set('userid',$this->Auth->user('id'));
}
this coding is in the ctp
<?php
echo $this->Form->input('user_id');
?>
The best way is to not send the user id to the browser and back at all, since that opens the possibility of form tinkering and security breaches/invalid results. Just inject the user id into the data before saving:
function add() {
if (!empty($this->data)) {
$this->Post->create();
// setting user id
$this->data['Post']['user_id'] = $this->Auth->user('id');
if ($this->Post->save($this->data)) {
$this->Session->setFlash(__('The post has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}

Resources