Vibration
Vibration

Reputation: 61

How to use different model from the controller?

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?

Upvotes: 1

Views: 112

Answers (2)

Roberto Maldonado
Roberto Maldonado

Reputation: 1595

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'));

Upvotes: 1

I want to learn more
I want to learn more

Reputation: 11

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

Upvotes: 1

Related Questions