Solved

CakePHP 2.0  Saving form data help

Posted on 2012-04-01
1
1,090 Views
Last Modified: 2012-06-21
Hi,  Just in the beginning stages of learning CakePHP, and would like help with saving data from a form to the database.  Basically i have about twenty forms which all are very similar, so solving this would also help me with the other forms.

What code do i put in my Models Views and Controllers, what files to create.

I have a Name texfield and Notes textarea  and a save button.

Entering a Name, and some Notes and clicking save, i want to save to a database table called Risks.

The Risks table is
-----------------------
id - INT
name - VARCHAR (255)
notes - VARCHAR (255)
created_at - DATETIME
updated_at - DATETIME

The form i have has this markup :
<form class="new-risk">
  <fieldset>
    
    <div class="control-group">
      <label class="control-label" for="input01">Name</label>
      <div class="controls">
        <input type="text" class="input-xlarge" id="input01">
      </div><!-- End Controls -->
    </div><!-- End Control Group -->
  	
  	<div class="control-group">
      <label class="control-label" for="textarea">Notes</label>
        <div class="controls">
          <textarea class="input-xlarge" id="textarea" rows="3"></textarea>
       	</div><!-- End Controls -->
   	</div><!-- End control-group -->
  
  	<div class="form-actions">
    	<button type="submit" class="btn btn-primary">Save</button>
    </div><!-- End form-actions -->
  
  
  </fieldset>
</form>

Open in new window


Solving this, would then help me with all my other forms.
0
Comment
Question by:jonathan1978
1 Comment
 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 37795856
Hi,

assuming you have looked at this page: http://book.cakephp.org/2.0/en/models/saving-your-data.html

and this for creating the form:

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

so in your case your form needs to return an array like:

Array
(
    [ModelName] => Array  (your model name here)
    (
        [id]=>'a id'
        [name] => 'value'
        [notes] => 'value'
    )
)

Open in new window


cake will deal with the updated and created fields for you, in 2.x you dont need a model unless you have any special requirements.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question