How to solve save data problem into table with cakephp?

Answer: Here i have given example to save data into table
Step 1:
Create table
CREATE TABLE IF NOT EXISTS `titles` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT ‘0’,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)

Step 2:
Controller file controllers/titles_controller.php

class TitlesController extends AppController
{
	var $helpers = array ('Html','Form');
	var $name = 'Titles';
	function index()
	{
		$this-> Title ->recursive = 0;
		$this->set('title', $this->paginate());
	}
	function add()
	{
		if (!empty($this->data))
		{
			if ($this->Title->save($this->data))
			{
				$this->Session->setFlash('Your title has been saved.');
			}
		}
	}
}

Step 3:
Model file title.php
NOTE: You have to check model file when your data is not saved if there is any field required true in the validation component and that field not exist into your form then data is not save into your table problem occurs

class Title extends AppModel {
	var $name = 'Title';
  var $validate = array('title'=>array('rule'=>'notEmpty','message'=>'Please Enter Title','allowEmpty'=>false,'required'=>false));
}

step 4:
View file save into app/views/titles/ folder add.ctp file

 echo $this->Form->create('Title'); 

<fieldset>
<legend>

  __('Add Title');  

</legend>

 echo $this->Session->flash();  
		echo $this->Form->input('title');
	

</fieldset>

 echo $this->Form->end(__('Submit', true)); 

Now successfully your data saved

Speak Your Mind

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: