2012-11-24 33 views
0

我已經在cakephp appcontrollet.php文件中創建了一個函數。這retrive我細節上從ID 的功能如下cakephp2.0 Appcontroller函數調用

function getDetail($id = null) // to check that user is valid or not 
    { 
    $data = $this->ModelName->find('first',array('conditions'=>array('ModelName.is_active'=>'Y','ModelName.is_deleted'=>'N','ModelName.primaryKey'=>$id))); 
    if($data !=array()) 
    { 
     // return true; 
     return $data; 
    } else { 
     $data = array(); 
     return $data; 
     //return false; 
    } 
    } 

提到,記錄現在,我想從任何controlller調用這個函數來獲取記錄的細節像我都會調用這個函數弗朗Userscontroller,adminscontroller ,市場控制器等,它會返回我的相關數據

我的問題是,如何appcontroller知道請求來自哪個控制器和哪個模型用戶?

誰能幫我提前

回答

1

將這個功能AppModel解決這個

感謝,並從相關模型中的每個控制器調用它。然後,函數看起來像:

class AppModel extends Model 
{ 
    public function getDetail($id = null) { 
    $data = $this->find('all', array(
     'conditions' => array($this->name.'.id' => $id) 
    )); 

// ... 

根據您的呼叫從模型中它會自動使用正確的模式。這樣的呼叫可以是這樣的:

class UsersController extends AppController 
{ 
    public $uses = array('User', 'Market'); 

    public function index() 
    { 
    // Fetch data from markets database 
    $this->Market->getDetail($id); 
    // Fetch data from users database 
    $this->User->getDetail($id); 

// ... 

在這種情況下,模型市場將被用來從數據庫中獲取的細節。

+0

是的,它的工作,但我怎麼能添加條件像'User.user_id'=> $ id或'Customer.customer_id'=> 4 .. –

+0

在getDetails()你可以使用'$ this-> name'來獲得當前模型的名稱。因此,條件是'$ this-> name。'user_id'=> $ id'。 – mixable

+0

是的,這是真的假設已從UsersControlle調用此功能,然後「$ this->名稱。」。user_id'=> $ id「這工作正常,但當這種funnction從另一個主控像CustomersController或CompaniesController調用.. –