cakephp insert array after explode function - cakephp-2.0

Hello I have done something similar in PHP core, but trying to do the same in cakephp, is proving difficult for me. I want to take the tags input, and explode the POST data and insert the the new array into its own table using the posts id for each tag separated by a ",". However when I insert the exploded array is empty when the post is created.
add.ctp
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->input('tags', array('label' =>'Separate tags by commas'));
echo $this->Form->end('Save Post');
PostsController
public function add() {
if ($this->request->is('post')) {
$tags = $this->set($this->request->data['Post']['tags']);
$exploded_tags = explode(",",$tags);
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}

$this->set() is for sending variables to the view in CakePHP. You don't need it to retrieve data.
public function add() {
if ($this->request->is('post')) {
$tags = $this->request->data['Post']['tags'];
$exploded_tags = explode(",",$tags);
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}

Related

Cakephp 3.3 - Not able to save images using plugin josegonzalez

I am not able to save the photo name and photo dir in the users table based on logged in used id.I am trying to upload the user photo for the existing user based on his used id.The photo fields are not getting updated for the existing user. I am trying to upload the photo using this plugin josegonzalez. Please help me.
<?php echo $this->Form->create($user, ['type' => 'file']); ?>
<?php echo $this->Form->input('photo',['type' => 'file', 'class' => 'form-control']); ?>
<?php echo $this->Form->input('photo_dir', ['type' => 'hidden']); ?>
<?php echo $this->Form->button(__('Submit'), ['type'=>'submit','class' => 'btn btn-success']); ?>
<?php echo $this->Form->end(); ?>
UsersTable
$this->addBehavior('Josegonzalez/Upload.Upload', [
'photo' => [
'fields' => [
// if these fields or their defaults exist
// the values will be set.
'dir' => 'photo_dir', // defaults to `dir`
],
],
]);
UsersController/add
public function add($id=null)
{
if ($this->request->is('post')) {
if (!$id) {
$id = $this->Auth->user('id');
}
$user = $this->Users->get($id);
$fileName = $this->request->data['photo']['name'];
$user->photo = $fileName;
//$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('Your photo has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to add your photo.'));
}
$this->set('user', $user);
}
Have you tried using 'enctype' in your form tag?
<form enctype="multipart/form-data">
It is explained here.
You can use Proffer plugin. It is easy to use and can be used to generate thumbnails too. I'm using it since 3.1 and working fine. It's even working on 3.3
https://github.com/davidyell/CakePHP3-Proffer
This is how it worked. I did not use any plugin. I uploaded single image at a time.
UsersController/add function.
public function add()
{
//check for logged in user authentication
$id = $this->Auth->user('id');
$user = '';
//check if the request is post
if ($this->request->is('post')) {
$user = $this->Users->get($id);
//check if upload file is not empty
if(!empty($this->request->data['photo']['name'])){
$fileName = $this->request->data['photo']['name'];
$extention = pathinfo($fileName,PATHINFO_EXTENSION);
$arr_ext = array('jpg', 'jpeg', 'gif','png');
//check for uploded file extension
if(in_array($extention, $arr_ext)){
$newfileName=$id.'.'.$extention;
$destDir = WWW_ROOT .'img'. DS .'users'. DS . $newfileName;
//move uploded file to destination
if(move_uploaded_file($this->request->data['photo']['tmp_name'],$destDir)){
$user->photo = $newfileName;
//save the uploded image in user table
if ($this->Users->save($user)) {
$this->Flash->success(__('Your profile photo has been uploaded successfully.'));
return $this->redirect([
'controller' => 'Users',
'action' => 'view', $id
]);
} else{
$this->Flash->error(__('Unable to upload image, please try again.'));
}
}else{
$this->Flash->error(__('Unable to upload image, please try again.'));
}
}else{
$this->Flash->error(__('Please choose a image to upload.'));
}
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
$this->set('id', $id);
}

edit post is not working in cakephp

I am learning Cakephp framwork. I am having problem while I am trying to update database record.
This is Controller code for edit post....
$this->loadModel('Post');
if($this->request->is('put')):
$this->Post->id = $this->params['id'];
if($this->Post->save($this->request->data)):
$this->Session->setFlash(__('Page has been edited'));
$this->redirect('/User/index');
endif;
else:
$this->set('postinfo', $this->Post->findById($this->params['id']));
endif;
}
This is view/edit.ctp file
echo $this->Form->update('Post', array(
'method' => 'put'
));
echo $this->Form->input('title',array('type' => 'text','value'=>$postinfo['Post']['title']));
echo $this->Form->input('body', array('type' => 'textarea','value' => $postinfo['Post']['body']));
echo $this->Form->submit('Submit', array('class'=>'btn btn-primary'));
echo $this->Form->end();
But this code does not update record in database...I tried everything from book.cakephp.org tutorial and other tutorials related to cakephp..
I hope I'll get some help from you guys :)
If this is PostController, than you don't need to call the $this->loadModel('Post'); function.
In view you need a hidden field with id of post.
Controller code for edit post
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->Post->id = $id;
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash('Unable to update your post.');
}
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
view/edit.ctp file
<h1>Edit Post</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Post');
?>

CakePHP: pass product_id value to checkout add page

I'm pretty new to cakePHP and I've been stuck on this problem for a few days now. My Products index view displays a list of products we have in inventory and includes a "Checkout" action with every product that points to the Checkout/add view. The problem is the product_id from the product that needs to be checked out does not get passed to the add checkout page and I can't figure out how to make this happen. If anyone has any suggestions I could really use some help.
Here is my CheckoutController add action:
public function add() {
if ($this->request->is('post')) {
$this->Checkout->create();
if ($this->Checkout->save($this->request->data)) {
$this->Session->setFlash(__('The checkout has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The checkout could not be saved. Please, try again.'));
}
}
$products = $this->Checkout->Product->find('list');
$users = $this->Checkout->User->find('list');
$this->set(compact('products', 'users'));
}
Checkout Add View
<?php echo $this->Form->create('Checkout');?>
<fieldset>
<legend><?php echo __('Add Checkout'); ?></legend>
<?php
echo $this->Form->input('product_id');
echo $this->Form->input('start_time');
echo $this->Form->input('end_time');
echo $this->Form->input('user_id');
echo $this->Form->input('description');
?>
</fieldset>
Link from the Products index page
<?php echo $this->Html->link(__('Checkout'), array('controller' => 'Checkouts','action' => 'add', $product['Product']['id'])); ?>
Cake will pass the product_id as the first argument of your action; Default Cake 'Routes' will match this url;
/mycontroller/myaction/param1/param2/param3
To this action:
MycontrollerController::myaction(param1, param2, param3)
You can pass this value to the form by adding an argument to the add() action and adding it to the 'request' if the form is not posted. Like this;
public function add($productId = null) {
if ($this->request->is('post')) {
$this->Checkout->create();
if ($this->Checkout->save($this->request->data)) {
$this->Session->setFlash(__('The checkout has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The checkout could not be saved. Please, try again.'));
}
} else {
$this->request->data['Checkout']['product_id'] = $productId;
}
$products = $this->Checkout->Product->find('list');
$users = $this->Checkout->User->find('list');
$this->set(compact('products', 'users'));
}
This will automatically propagate the 'value' of the product_id drop down
from your links on the products index page it seems that
public function add()
should read
public function add($product_id)
furthermore you should set the $product_id into the view, and also
fill it into the inputbox echo $this->Form->input('product_id', array('value'=>$product_id));

edit multiple models cake php

trying to edit multiple models
The Controller
function edit($id = null) {
if (!empty($this->data)) {
$this->Qnote->save($this->data);
if ($this->Qnote->save($this->data)) {
$this->data['Step']['qnote_id'] = $this->Qnote->id;
$this->Step->save($this->data);
$this->Session->setFlash(__('The qnote has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The qnote could not be saved. Please, try again.', true));
}
}
The Form
<?php echo $this->Form->create();?>
<fieldset>
<legend><?php __('Edit Qnote'); ?></legend>
<?php
echo $this->Form->hidden('Qnote.id');
echo $this->Form->input('Qnote.subject');
echo $this->Form->input('Qnote.body');
echo $this->Form->hidden('Step.0.id');
echo $this->Form->Hidden('Step.qnote_id');
echo $this->Form->Hidden('Step.user_id');
echo $this->Form->input('Step.0.body');
?>
<?php echo $this->Form->end(__('Submit', true));?>
I am trying to edit and update information in associated models , Qnotes and Step
The information show up in the form. however when i submit the form. the
the Qnote information is saving with out any problem . however the step information is not updating
The models are associated. with Steps belong to Qnote, QNote has Many Steps
Your form include '0' for all the Step inputs.
echo $this->Form->hidden('Qnote.id');
echo $this->Form->input('Qnote.subject');
echo $this->Form->input('Qnote.body');
echo $this->Form->hidden('Step.0.id');
echo $this->Form->Hidden('Step.0.qnote_id');
echo $this->Form->Hidden('Step.0.user_id');
echo $this->Form->input('Step.0.body');
And in your controller action, you need to call saveAll() instead.
if ($this->Qnote->saveAll($this->data)) {
...
Try this to load different model. :)
var $uses = array('Qnote', 'Step', 'modelName');
If you want to save data in mutiple model you have to call the model in controller. Using
$this->loadModel('Step');
then do the save part like below. You have called save function for an object twice.
function edit($id = null) {
if (!empty($this->data)) {
$save = $this->Qnote->save($this->data);
if ($save) {
$this->data['Step']['qnote_id'] = $this->Qnote->id;
$this->Step->save($this->data);
$this->Session->setFlash(__('The qnote has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The qnote could not be saved. Please, try again.', true));
}
}
If the models are associated, you can save the whole thing at once (in all the models concerned) just by using the saveAll() function.

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