Solved

CakePHP 2.0  Saving form data help

Posted on 2012-04-01
1
1,080 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now