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));
Related
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.'));
}
}
I have to 2 models that are related ItQuery and ItQueryComment. When a user adds a ItQuery other users should be able to comment on it. What i am trying to achieve is when other users add comments on a query they should be redirect to the view of the query not the index page for the it_query_comments
here is my code for my comments add view
<?php echo $this->Form->create('ItQueryComment'); ?>
<fieldset>
<legend><?php echo __('Add It Query Comment'); ?></legend>
<?php
echo $this->Form->input('it_query_id');
echo $this->Form->input('comment');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
and here is my add function in the controller
public function add() {
if ($this->request->is('post')) {
$this->ItQueryComment->create();
if ($this->ItQueryComment->save($this->request->data)) {
$this->Session->setFlash(__('The it query comment has been saved'));
$this->redirect(array('controller' => 'it_queries','action' => 'view', $itQuery['ItQuery']['id']));
} else {
$this->Session->setFlash(__('The it query comment could not be saved. Please, try again.'));
}
}
$itQueries = $this->ItQueryComment->ItQuery->find('list');
$this->set(compact('itQueries'));
}
If anyone could show me how to do this, that would be awesome. Thanks in advance
try the following
$this->redirect(array(
'controller' => 'it_queries',
'action' => 'view',
$this->request->data['ItQuery']['id'])
);
I've been trying to figure this problem out all morning. Tried some things from other questions, but it either didn't really apply to my situation or didn't work. I have two tables:
users = (id,name,username,password,roles,last_edit,language)
french_translations = (id, french_clinical_recommendations, french_tradenames, include_drug, french_category, drug_id, user_id)
User hasMany french_translations and french_translations belongsTo User.
When a user adds or edits a french_translation I want it to save the user id in the french_translations table, the field user_id, for that record, and then put the generic name for a drug in the last_edit field in the users table. Right now it creates a new record in each table. In the users table, everything is blank except for the id and last_edit field(Which puts the correct drug name in the field). And the french_translations table has a record with blanks, with the user_id being the same as the blank one created in the users table.
Controller:
function add($id = null) {
$userid = $session->read('Auth.User.id');
$drug = $this->FrenchTranslation->Drug->read(
array(
'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations',
'Drug.category','Drug.lrc'
),
$id
);
$this->set('user',$userid);
$this->set('drug',$drug);
if (!empty($this->data)) {
$french_translation['FrenchTranslation']['id'] = $this->Session->read('id');
$this->FrenchTranslation->create();
if ($this->FrenchTranslation->save($this->data)) {
$this->Session->setFlash(__('The french translation has been saved', true));
$this->redirect(array('controller'=>'drugs','action' => 'index'));
} else {
$this->Session->setFlash(__('The french translation could not be saved. Please, try again.', true));
}
}
$drugs = $this->FrenchTranslation->Drug->find('list');
$this->set(compact('drugs'));
}
function edit($id = null) {
//$this->FrenchTranslation->id = $id;
$userid = $this->Auth->user('id');
$username = $this->Auth->user('name');
//$this->FrenchTranslation->user_id = $id;
$drug = $this->FrenchTranslation->Drug->read(
array(
'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations',
'Drug.category','Drug.lrc'
),
$id
);
$this->set('drug',$drug);
$this->set('user',$userid);
$this->set('username',$username);
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid french translation', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->FrenchTranslation->saveAll($this->data)) {
$this->Session->setFlash(__('The french translation has been saved', true));
$this->redirect(array('controller'=>'drugs','action' => 'index'));
} else {
$this->Session->setFlash(__('The french translation could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->FrenchTranslation->read(null, $id);
}
$drugs = $this->FrenchTranslation->Drug->find('list');
$this->set(compact('drugs'));
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for french translation', true));
$this->redirect(array('action'=>'index'));
}
if ($this->FrenchTranslation->delete($id)) {
$this->Session->setFlash(__('French translation deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('French translation was not deleted', true));
$this->redirect(array('action' => 'index'));
}
Edit View:
<?php echo $this->Form->input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']['generic'])); ?>
<?php echo $this->Form->input('user_id', array('type'=>'hidden','value'=>$user)); ?>
This is a bit of a late answer, but you should never use the view to set secure variables like user_id. Instead, you can set these variables in your controller:
$this->request->data['User']['last_edit'] = $this->request->data['Drug']['generic'];
I figured it out, however, I feel like there's a better and more secure, way of doing it. I am using the security component, and a person has to login to see any of the site, so it's probably safe enough.
I simply added a user.id input for the user id, at that point it could find the appropriate user to store the last_edit into. In the controller I have a variable called $userid which gets the current user id using $userid = $this->Auth->user('id'); and is passed to the view using $ths->set('user',$userid); I used that same user id value in the input for the drugs user_id field.
View:
<?php echo $this->Form->input('User.id',array('type'=>'hidden','value'=>$user));//The user ID last edit will be stored in ?>
<?php echo $this->Form->input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']['generic']));//The drug edited last by the specified user ?>
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 can I add an item and insert it into 2 tables?
So I have a 'Type' table and 'SpecificType' table.
Type has fields 'id' and some other common fields.
SpecificType has fields 'id', 'type_id' and some other uncommon fields.
When I go to /specific_types/add and I submit, ideally I want to first add that to 'Type' then add it to 'SpecificType'.
This is what I have now.
In SpecificType Model
var $belongsTo = array(
'Type' => array(
'className' => 'Type',
'foreignKey' => 'type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
In SpecificType Controller
var $uses = ('Type', 'SpecificType');
function add() {
if (!empty($this->data)) {
$this->Type->create();
if ($this->Type->save($this->data)) {
$this->SpecificType->create();
if ($this->SpecificType->save($this->data)) {
$this->Session->setFlash(__('The SpecificType has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The SpecificType could not be saved. Please, try again.', true));
}
} else {
$this->Session->setFlash(__('The Type could not be saved. Please, try again.', true));
}
}
}
In SpecificType add.ctp
echo $form->input('Type.data1');
echo $form->input('title');
So right now, it saves Type.data1 but title isn't getting saved.
What am I missing?
Thanks,
Tee
Additional Info:
The 2nd model isn't saving only when I turn on MeioUpload.
Make sure your view is setup to create a form for SpecificType:
<?php echo $this->Form->create('SpecificType', array('action' => 'add')); ?>
<?php echo $this->Form->input('data1'); ?>
<?php echo $this->Form->input('title'); ?>
<?php echo $this->Form->end(); ?>
This will put all of your form data into: $this->data['SpecificType']
Right before your code:
$this->Type->create();
You need to do this:
$this->data['Type'] = $this->date['SpecificType'];
Then process the save. As long as the view is setup correctly for the SpecificType controller, all of the data from the form will be stored in $this->data['SpecificType']. If you pr($this->data) and there is data you need to save outside of $this->data['SpecificType'], review and fix the view.
Side note: Your design sounds awfully sketchy. You should never need to save the data in two locations. I would recommend revisiting the design of your app. There is something fundamentally wrong with it if you need to save the same data into two tables.
Just copy the data over to a SpecificType index in the data array and use saveAll(). The models are related so Cake will automatically link them via the type_id. We're working off the Type alias, so make sure you also have var $hasMany = array('SpecificType'); in your Type model.
function add() {
if (!empty($this->data)) {
$this->Type->create();
$this->data['SpecificType'] = $this->data;
if ($this->Type->saveAll($this->data)) {
$this->Session->setFlash(__('The SpecificType has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The SpecificType could not be saved. Please, try again.', true));
}
} else {
$this->Session->setFlash(__('The Type could not be saved. Please, try again.', true));
}
}
The code seems to be fine, use saveAll instead of save