2011-05-01 79 views
1

創建視圖寄存器和數據發送到控制器如何將數據表單控制器發送到其他模型?

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

    } 

我想數據形式memberscontroller發送給我的資料型號 我嘗試使用

$use = array('Myprofile'); 

我得到

Undefined property: MembersController::$Member 

當我使用

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

我得到

​​

我不知道正確的方式或不 也有另一種方式來解決我的問題 感謝您的任何意見

回答

0

如果您使用$uses數組,請確保您具有該數組中包含的當前模型。否則,如果您定義了特定的$uses陣列,則當前模型不會被默認包含。

如果MyProfile有關Member您可以通過

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

訪問它,你也可以使用App:import

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

你有兩個模型添加到您的$uses array:

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

้ำhe嘿我跟着你們的建議,但我得到了未定義的屬性:MembersController :: $ Myprofile – krissanawat 2011-05-02 08:02:45

1

$ uses數組只允許您訪問您在其中指定的模型。

如果您已註釋掉$ uses,您仍然可以訪問$ this-> Member模型,因爲您位於Members控制器中。

一旦您向$ uses數組添加另一個模型,您必須記得也包含您的初始模型。

我還發現,在某些情況下,這是非常有用的,以確保當你做這樣的事情,你應該指定默認的模型FIRST

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

否則,你可能會得到意外的結果來自$ this-> paginate()

+0

้哎 我跟着建議你們 但我得到了 未定義的屬性:MembersController :: $我的資料 – krissanawat 2011-05-02 07:59:17

+0

那麼問題很可能在你我的資料模型的代碼,無論是在它的類名或在它的文件名。 – stevecomrie 2011-05-03 16:45:49

0

嘗試在您的操作中使用$this->loadModel('Myprofile');

你確定你的型號是'Myprofile'嗎?

你甚至可以調試它,看它是否返回true或false。這將幫助您查找模型是否已正確安裝。

0

您可以調用在正常方式相關模型中的任何公共方法。對於如

在Profile.php

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

從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); 
    */ 
} 

作爲一個側面說明,如果你遇到了這種情況作出的URL更爲明智,請看路由器( http://book.cakephp.org/2.0/en/core-utility-libraries/router.html)

相關問題