Reputation: 940
I am working on a project in CakePHP 1.3 which will store a collection of members - each with a forename and surname - and multiple results for each member which will store the date, a reference to the member and also the number of points.
The database structure is shown below.
CREATE TABLE `members` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`forename` varchar(255) NOT NULL,
`surname` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `results` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`member_id` int(10) unsigned NOT NULL,
`date` date NOT NULL,
`points` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `member_id` (`member_id`)
);
The application is working for entering each member's details one by one, which is fine - but I need to have an "Add Results" page which will list each of the members in the database and allow a numerical value to be entered for the number of points that member has been given. Each time results are added, they will all be for the same date, and each member will always get points, even if it is 0.
Can someone please give me a pointer on how to display a form that conforms to the above specification, and how to store multiple rows at the same time?
Thanks in advance.
Upvotes: 1
Views: 1406
Reputation: 4517
Since you are new to cake... here are a few tips. use pr($this->data)
in your controller to see exactly what data you are receiving when the form is submitted.
give your input fields naming conventions like this data[YourModel][field]
so for example you would change <input name="points[]" type="text" />
to <input name="data[Result][points]" type="text" />
then in the controller it will be accessible by
pr($this->data['Result']['points']);
SAVING TO MULTIPLE MODELS
you may find it easiest to give access to both models from the current controller so at the top of your controller add this
class ResultsController extends AppController {
var $uses = array('Member', 'Result');
now you can save whatever data you want to whatever model you want like so.
$dataForResults = array();
$dataForResults['Result']['points'] = $this->data['Result']['points'];
$this->Result->save($dataForResults);
this doesn't necessarily retrieve a record from the database, edit it and then put it back, this will create a new record.
to edit a record it will be more like,
$dataByMemberId = $this->Result->findByMemberId($data['Result']['member_id']);
$dataByMemberId['Result']['points'] = $this->data['Result']['points'];
$this->Result->save($dataByMemberId);
that will get the current record of that member in the Results table, edit the points
entry and then resubmit it back in it's location in the database.
A little winded but should get you started.
Upvotes: 2