krissanawat
krissanawat

Reputation: 626

How To send data form Controller to other Model?

I create view for register and send data to controller

//app::import('Model','Myprofile');
Class MembersController extends AppController {
    var $name = 'Members';
    var $helpers = array('Form', 'Session');
    var $uses = array('Myprofile'); //

    function register() {
        //$myprofile = new Myprofile();
        if (!empty($this - > data)) {
            $this - > Member - > create();
            if ($this - > Member - > save(($this - > data['Member']['username']), ($this - > data['Member']['password'])) && $this - > Myprofile - > save(($this - > data['Myprofile']['name']), ($this - > data['MyProfile']['address']), ($this - > data['Myprofile']['phonenumber']))) {
                $this - > redirect('index');
            }
        } else {

            $this - > Session - > setFlash('failed');
        }

    }

I want to send data form memberscontroller to Myprofile Model I try to use

$use = array('Myprofile');

i got

Undefined property: MembersController::$Member

when I use

//app::import('Model','Myprofile');
//$myprofile = new Myprofile()

i got

Undefined property: MembersController::$Myprofile

I do not know the right way or not There is also another way to fix my problem Thank for any advice

Upvotes: 1

Views: 1903

Answers (5)

Kishor Kundan
Kishor Kundan

Reputation: 3165

You can invoke any public methods of related models in normal way. for eg

In Profile.php

function someMethod( $param = null ) {
    // some definition
}

From MembersController.php

function register ( ) {
    $this->Member->Profile->someMethod( $my_data_to_pass );    // if related
    /* if not related 
    $this->loadModel('Profile');
    $this->Profile->someMethod($my_data_to_pass);
    */
}

As a side note, if you ran into this situation for making urls more sensible, Please look into Router (http://book.cakephp.org/2.0/en/core-utility-libraries/router.html)

Upvotes: 0

JohnP
JohnP

Reputation: 50019

If you use the $uses array, make sure that you have the current model included in that array as well. Otherwise the current model does not get included by default if you define a specific $uses array.

If MyProfile is related to Member you can access it via

$this->Member->MyProfile; //depends on associations

You can also use App:import

App:import('Model', array('Myprofile')); //loads the class
$myProfile = new MyProfile();
// OR
MyProfile::staticMethod();

Upvotes: 0

talelcool
talelcool

Reputation: 423

try to use $this->loadModel('Myprofile'); in your action.

and are you sure your model name is 'Myprofile' ?

you can even debug it to see if it return true or false. this will help you find if the model has been correctly instancieted.

Upvotes: 0

stevecomrie
stevecomrie

Reputation: 2483

The $uses array will only give you access to the models you specify in it.

If you'd commented out $uses, you would still have access to $this->Member model by default since you are in the Members controller.

Once you add another model to the $uses array, you must remember to include your initial model as well.

I've also found that in some cases, it's very useful to make sure that when you're doing something like that, you should specify your default model FIRST

var $uses = array( 'Member', 'Myprofile' );

Otherwise you might get unexpected results from actions like $this->paginate()

Upvotes: 1

dhofstet
dhofstet

Reputation: 9964

You have to add both models to your $uses array:

var $uses = array('Myprofile', 'Member');

Upvotes: 0

Related Questions