I am building a form with Cakephp 2.x and I want to submit some of the data that is received when submitting it to table A and the other data to table B. Does anyone know how to achieve this in cakephp?
I believe you need to look at Cakephp's model documentation. Looking at it now it seems that saveMany could help you with that. http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array
Yes. You should do what you want follow below way:
1. Create method receiveData() in FooController.
2. Create view file receive_data.ctp in folder foo.
$this->Foo->create();
$this->Foo->input('....', array(....));
// other input.
$this->Foo->end();
3. In FooController\receiveData(), When submit, you have data array $myDataArray like this: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html#adding-posts
(Notice: It is Foo controller).
4. Before you save data from submitted data, you maybe need manipulation with submitted data array.
5.
You must call Model Bar inside FooController by:
$uses = array('Bar');
You will save to other table by:
$this->Bar->save($myDataArray);
(Notice: It is Bar Model).
Do Table A and Table B represent two different Models?
if yes saveAssociated might help you.
http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto
If you want to save the same kind of model n times then saveMany is what you want to do.
Related
I have three tables/models:
User(id)
Map(id, user_id, attribute_id)
Attribute (id, name)
Map belongsTo the others, the others hasMany Map.
I'd like the user (via user controller and user view) to create many links to associations at once. How can I do this, assuming that the user forms (add/edit) have 10 attribute fields, all linking to the same table?
I'd need to save up to 10 records in Map in one go. To start with, I'm unsure what the field should be - create('Attribute.name')? Also, cake outputs the same input name to each input as they point to the same field - what's the best way to fix this?
I have already read the relevant documentation, but didn't get much from it.
Thanks!
You can use saveAssociated() method. Prepare an input array according to the instructions given in the link and directly use $this->User->saveAssociated($your_array, array('deep' => true));
I've got a Cake application with a reports query interface, where the admin user can filter the data by various inputs in a form and the results are then displayed on the screen. I am looking for the simplest way to add a button which allows the user to download the results of this same query as CSV.
I'm sure I can create one for myself if I have to, but is there already a way to regenerate any given form based on $this->data? That way, I can just add .csv to the form action and use RequestHandler to choose the right output format.
[here take a look at the following...
instead of finding the data from Db you can simply pass $this->data to it.
take a look at follo
Exporting data to CSV the CakePHP way
I guess you have to replicate the function on your controller, one for generating the results on screen and another same function intended for csv, but on the function for csv it must have parameters which are similar to the values of $this->data. Use javascript to redirect on the function for csv.
I have scenario where I have 3 different models data being saved from one form, by means of the saveAll pass through method in CakePHP 2.x
The models on the form are:
Project
ProjectImage
ProjectAttachment
1 and 2 are saving perfectly, but I am having problems with the ProjectAttachment fields. If you look at the following image, the fields in question are at the bottom right, highlighted by a red frame: http://img.skitch.com/20120417-bnarwihc9mqm1b49cjy2bp13cf.jpg
Here is the situation:
I have named the fields as follows: *ProjectAttachment.0.project_id*, ProjectAttachment.0.name .... *ProjectAttachment.6.project_id* etc where the numbers are relative to the already present amount of attachments the user has added, as there is no limit. So if the user has ALREADY added 6 documents, the field would be called ProjectAttachment.7.id and so on. This is because of the naming convention when using the saveAll method.
I have made use of two hidden fields, one to store the user ID, the other to store the project ID.
The problem is that if the user does not fill in the Document Title and select a file to upload, the form fails! If I remove all validation rules, the form no longer fails BUT a blank ProjectAttachment record is inserted.
I suspect the problem may also be that the project_id and user_id fields (that are hidden) already have values in them?
I gave it some thought, and came up with a simple concept: In my controller, before the saveAll call, I would check to see if a blank Document Title field was submitted, and if so, I would completely eliminate the relevant array entry from the $this->request->data['ProjectAttachment'] array, but this did not seem to work.
To clarify, I am not looking for validation rules. If the user decides they only want to upload images and not touch the ProjectAttachment form, them the saveAll operation must not fail, it must simple not attempt to save the blank fields in the form, if this is at all possible.
Any suggestions?
Kind regards,
Simon
Well it seems that the solution was far simpler than I had initially thought! I was partially on the right track, and I had to unset the variable ProjectArray key in the $this->request->data array if I had encountered any blank fields.
I did this as follows:
if(!empty($this->request->data['ProjectAttachment'])){
foreach($this->request->data['ProjectAttachment'] as $key => $value){
if(is_array($value) && array_key_exists('upload_file', $value)){
if($value['upload_file']['name'] == ''){ // Blank document file
unset($this->request->data['ProjectAttachment']);
}
}
}
}
I hope someone finds this somehow useful in some form or another.
Kind regards,
Simon
I would like to set $this->data in a view rather than the controller. Will this work with the form helper to automagically input values?
many thanks!
Further explanation if you really want to know...
You may wonder why I wouldn't just put the values right into the value field but it makes sense in this situation to put it into $this->data; I have a ton of fields of various types and I do not want to have to add if isset() to every value field because form fields are generated based on a stored value and may or may not have already been filled in. I cannot set this->data in the controller because of the data being in JSON. Plus the data has to go through several layers before getting to where it is at this point.
unresolved but I got around it by entering everything manually. It would have been nice to add it automagically.
I have a cakePHP action that must return a CSV file in different formats according a parameter.
Which is the best way to write code for this?
-Should i return all data and creating different view based on that parameter? don't think so.
-Should i pass the parameter to model and returning different fields from the model? i don't like the idea to insert a switch() in the model.
Other ideas? thanks !
in one of the cases i had to different outputs from data in my database(similar to yours but not csv) . crated a __Process() and passed type as a parameter.based on type it formatted the output and returned it back now my view just looped through the data and displayed it. I dont know if this answers your question
But from what u say different view is pretty okay.
This article in the CakePHP bakery describes how to return CSV files: http://bakery.cakephp.org/articles/jeroendenhaan/2010/04/23/exporting-data-to-csv-the-cakephp-way for CakePHP1.2. For version 2 there's a plugin: https://github.com/josegonzalez/cakephp-csvview