2013-05-21 85 views
0

我正在爲使用Yii php的項目創建一個審計跟蹤模塊,而且我是一個新手..我們都知道審計跟蹤涉及創建歷史記錄,事務日誌......我爲審計線索 創建了一個模型並生成了一個控制器以及一個單獨的模型。 (當然使用GII)Yii創建審計跟蹤;控制器到另一個控制器

我的問題在於如何真正創建一個條目,或跟蹤(日誌,歷史,我不知道術語對不起= 3)

我有我的AuditTrailController與動作:

class AuditTrailController extends Controller{ 

    public function actionCreate() 
    { 
    //do create the trail 
    } 

} 

我的計劃是從每一個其他控制器所有在整個項目中的模塊,如發言權調用actionCreate():

class StudyController extends Controller { 

     public function addRecord(){ 
     //add some record 


     //---> i want to call the acionCreate() from the AuditTrailController from here 
     //to create an entry telling that the some user "user" at time "time" 
     //performed an "add" operation.. 
     } 

     public function updateRecord(){ 
     //update some record 


     //---> i want to call the acionCreate() from the AuditTrailController from here 
     //create an audit entry telling the user "updated" 
     } 

} 

-The的Yii:應用程序()沒有按」 Ť做的伎倆..(或IVE使用它的錯誤)..

我已經搜索了很多答案,但他們說這是「邪惡的」從一個調用另一個控制器..我看到像使用組件..其他說它應該在模型中完成......但我不知道..我是新的OOP和Yii所以我在這裏奮鬥..幫助我出來謝謝..但如果上面是可能的,那麼樣品代碼將非常有用,謝謝!

回答

1

可以調用另一個控制器動作以這種方式

//這裏是你的動作控制器

class AuditTrailController extends Controller 
{ 

    public function actionCreate() 
    { 
    //do create the trail 
    } 

} 

//此處控制器,你想用你的行動

class StudyController extends Controller { 

     public function addRecord(){ 
       $controller = new AuditTrailController($this->id); 
       $controller->actionCreate(); 
     } 
} 

並且不要忘記添加到配置此行來導入

'application.controllers.*', 
+0

錯誤顯示沒有這樣的文件或目錄; 我忘了告訴你...這兩個控制器是在單獨的模塊.. 說:模塊1包含audittrailcontroller和模塊2包含studycontroller ..感謝 所以我有點猜測studycontroller類找不到其他類,以便「新審計TrailController()」不起作用 – muffin

+0

我基本上創建一個名爲「審計」的模塊,以跟蹤整個系統中特定用戶交易的行爲,所以我真的期待所有模塊該項目與審計模塊一起調用至少它的actionCreate()來生成日誌 – muffin

+0

在您的模塊目錄文件yournameModule.php,函數Init()中,添加到$ this-> setImport,您可以從另一個模塊控制器的位置,如'audit(您的模塊的名稱).controllers。*', –