Saving Form Data Twice in CakePHP

CakePHP makes it increbibly easy to insert data into MySQL tables with the use of their inbuilt form helper and and save function however one question I have seen alot is how to save form data more than once equating to multiple row inserts? The answer is actually quite simple.

When you use the CakePHP save method, CakePHP holds on to the last primary ID it created, and so if you try using the save method again, in the same session.. nothing happens, as the row already exists as far as Cake is concerned. You need to make Cake forget about the last primary ID it's holding.


$this->Whatever->create();
$this->Whatever->save($this->data);

$this->Whatever->create();
$this->Whatever->save($this->data);

The code above runs the create() method on the Whatever model reseting any primary ID Cake was holding for it. Then it runs the save method saving the form data to the Whatever model. It then runs the create method again, resetting the primary ID and then saves the form data again, giving you two rows in your table with the same data all bar the primary key.

Of course you would more than likely put your save in a loop of some kind if you were planning on multiple saves.


$inserts_required = 1;
while ($inserts_required <= 10) {

$this->Whatever->create();
$this->Whatever->save($this->data));
echo $inserts_required++;
}

The above will insert 10 rows with the same data, bar the primary ID.


Links on Images in the CakePHP Framework

Introduction: I have seen a fair number of posts on the cakePHP google group and also on a few blogs regarding how to make links in views that are images using the cakePHP helpers. There are ways to fudge this and hard code paths to images and links but when you start swapping between /pages and /controllers this all gets very messy and is not using what cakePHP provides so I thought I would write a quick tutorial on this. Actually it's more like a few notes with an example - this is a real easy task, and quite obvious once you consider what we are doing.

Assumptions: I'm assuming your using cakePHP 1.2 here if your not, go get it :) If you can't upgrade then just read this example code below then check the 1.1 manual for 'image' and 'link' and you should be able to work it.

Step 1: Displaying an Image on a View or Layout

O.K as you may or may not know, to display and image on a layout or view in correct the correct cake manor just ...

echo $html->image('home.gif');

Obviously above, change 'home.gif' to your image.

Step 2: Making a HREF Link on a View or Layout

Another one you may already know...

echo $html->link("Home", array('action' => 'features'), array('escape' => false));

And again, change 'Home' to your link text and 'features' to your action in your controller. The above example will link the text 'Home' to the 'features' action of the currently active controller. If you have this on a view in the /pages cake directory it will link to /pages/features. This is no good if you wan't to link to another controller, however this is easy too ...

echo $html->link("Products", array('controller'=>'products', 'action' => 'features'));

Above we are also specifying the controller name. The above example will link the text 'Products' to the controller 'products' and the action 'features'. ie /products/features.

Step 3: Coming image and link for a linked image

O.K the bit of code you came to this page for ...

echo $html->link($html->image("products.gif"), array('controller'=>'products', 'action' => 'features'), array('escape' => false));

As you can see, from a PHP point of view it is as simple as including a function within another function. So here, the image 'products.gif' links to the 'products' controller and the 'features' action of that controller. You may also noticed we have added escape = false. If you do not specify this as fasle, or you specify it as true then the raw html for the image will be out printed to the view as the link instead of the actual image.

That's everything you need for now to get you linking images within CakePHP views. For more information regarding CakePHP and more tutorials please visit CakePHP.co.uk