1

I am trying to insert record using Cakephp.My model name is something like User.php. And My working controller name is SignupsController.I want to insert record using this two but I cant.I am give my some codes below :

View :

<?php echo $this->Form->create('Signups',array('action' => 'registration'));?>
                        <div class="row-fluid">
                            <div class="span5">
                                <label class="">*First Name</label>
                                <?php echo $this->Form->input('first_name', array('type' => 'text','label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
                            </div>
                            <div class="span5">
                                <label class="">*Last Name</label>
                                <?php echo $this->Form->input('last_name', array('type' => 'text', 'label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
                            </div>
                        </div>
<?php echo $this->Form->end(); ?>

My controller code is given below :

class SignupsController extends AppController {

var $name = 'Signups';
var $uses=array("User");



public function registration()
    {
        $this->layout="reserved";
        $this->Club->create();
        if (isset($_POST['registration'])) {
            echo "This is";

            echo "<pre>";print_r($this->request->data);echo"</pre>";
            $this->User->save($this->request->data);
            //$this->Session->setFlash(__('Promoter registration has been done successfully'));
            //$this->redirect('registration');
            //$this->redirect(array('action' => 'registration'));
        } 
    }

}

My model name is different which's name is User.php I want to insert the record using this above code.Any idea how to insert?

tereško
  • 58,060
  • 25
  • 98
  • 150
Vibration
  • 61
  • 1
  • 1
  • 8
  • 2
    if (isset($_POST['registration'])) { <-- why are you using super globals instead of the request object? And why do you need a Signups controller at all instead of doing this in the right scope of the Users controller? – floriank Mar 07 '14 at 12:03

2 Answers2

1

you can do this by loading the users model in current controller just write the following line

$this->loadModel('Name of the Model').

then

$this->nameofmodel->save()

As you are unable to understand see this

Controller::loadModel(string $modelClass, mixed $id)¶
The loadModel() function comes handy when you need to use a model which is not the     controller’s default model or its associated model:

$this->loadModel('Article');
$recentArticles = $this->Article->find(
'all',
array('limit' => 5, 'order' => 'Article.created DESC')
);

$this->loadModel('User', 2);
 $user = $this->User->read();

Above pasted code is taken from CookBook of Cakephp, if you still do not understand just read it it has complete detailed explanation you can also see this to understand

Community
  • 1
  • 1
1

you can use it with $uses variable in SignupController

class SingupController extends AppController
{
    public $uses = array('User');

    //rest of stuff
}

Or, if you want, you can load it on-demand inside a method:

$this->loadModel('User'); //now model is loaded inside controller and used via $this->User

EDIT: Your data array has to include the name of the model you're saving. So, replace:

$this->Form->create('Signups',array('action' => 'registration')

with:

$this->Form->create('User',array('url' => array('controller' => 'signups', 'action' => 'registration'));
Roberto Maldonado
  • 1,585
  • 14
  • 27
  • Please see my controller code,I have already used the code but its not inserting any record.I tried this but its not working – Vibration Mar 07 '14 at 12:06
  • This edit solve your problem . Notice : if you want use $uses variable you should add a default model name in this array like $uses = array('SignUp','User'); else your default Model methods doesn't work. – sdagli Mar 07 '14 at 13:31
  • I have changed form action,but its not inserting any records yet. – Vibration Mar 07 '14 at 13:41
  • Then please post your `$this->request->data` array. Probably the error is over there – Roberto Maldonado Mar 07 '14 at 13:45