April 6, 2010

How to Select Field Names in Table in CakePHP

In php you can use mysql_field_name to get the field name of the specified field in a result. Someone asked me the following question.

If Employee table has three rows id,name and address. How do we fetch those names (id,name,address) using sql query in cakephp?

First of all, you can’t actually use mysql_field_name() in cakephp. But there are workarounds. Say you want to find the field names of the the table ‘Post’. Here is what you can do.

$array = $this->Post->find('all');
 
$array = Set::extract('/0/Post', $array);
pr($array);
 
$field_names = array_keys($array[0]['Post']);
pr($field_names);

The result of the print statements will display the following

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] => 2
                    [title] => A title once again
                    [body] => And the post body follows.
                    [created] => 2010-02-08 07:48:03
                    [modified] => 
                )
 
        )
 
)
 
Array
(
    [0] => id
    [1] => title
    [2] => body
    [3] => created
    [4] => modified
)

I hope this helps. Feel free to comment on this page. I’ll get notified immediately.

March 24, 2010

How to Save Multiple Models in CakePHP

It’s inevitable that you will have a form where you will need to save multiple models at once. Here is a small hack on how you might do that.

if (!empty($this->data)) {
    $this->ModelName->create();
    $allSaved = true;
 
    foreach($this->data as $model => $data) {
        switch ($model) {
            case 'ModelName1' :
                /*manipulate ModelName1 data here*/
                if (!$this->ModelName1->saveAll($this->data['ModelName1']))
                    $allSaved = false;
                break;
            case 'ModelName2' :
                /*manipulate ModelName2 data here*/
                if (!$this->ModelName2->saveAll($this->data['ModelName2']))
                    $allSaved = false;
                break;
        }
    }
 
    if ($allSaved) {
        /*Print Success Message*/
    }
}

Write Custom SQL in CakePHP

There may be a time when you want to simply just write your own custom sql in cakephp. Now, cakephp doesn’t want you to do this. The find() and save() calls should be able to do everything you need to do. But, I’ve found it useful to write custom sql queries for testing purposes. Here is how you do it.

$query = $this->ModelName->query("SELECT * FROM TableName");