2010-09-10 80 views
0

我知道它可能會使用戶在前端的管理generator.yml稍微奇怪,但每個頁面是一個列表或編輯頁面,它絕對是最簡單的事情。symfony sfDoctrineGuardPlugin generator.yml過濾user_id

在努力編寫日誌模塊之後,我安裝了sfDoctrineGuardPlugin並使用關係將它們鏈接到我的主用戶表。數據庫中的所有內容都依賴於這一張表,並且現在的活動網站上的數據已經建立了8個多月,我需要保留它。

我目前的問題是,一旦有人登錄,它存儲在警衛系統的ID,我需要所有的頁面過濾所有結果,包括列表和編輯。

是否有一個簡單的方法來做到這一點,或者我需要使用每個列表的table_method?

上述唯一的問題是,ID出現在地址中,我需要它來確保人們無法查看那些不應該的數據。

此外,這很可能會很快發生變化,此時表是MyISAM的更好的自動增量使用,可以讓用戶增加,因此每個人都從一開始。但是,它似乎不會執行外鍵,所以當我編寫代碼將其轉換爲INNOBDB時,地址中的ID變得更重要,因爲人們無法更改。

Owner: 
    columns: 
    id: { type: integer(4), primary: true, autoincrement: true } 
    slname: { type: string(64), notnull: true } 
    uuid: { type: string(36), notnull: true } 
    e_mail: { type: string(64) } 
    user: { type: string(16) } 
    pass: { type: string(40), notnull: true } 
    subscription: { type: integer(4), default: '0', notnull: true } 
    registered: { type: integer(1), default: '0', notnull: true } 
    relations: 
    Subscription: { local: subscription, foreign: id } 
    User: { class: sfGuardUser, foreign: id, local: id, type: one, onDelete: cascade, foreignType: one, foreignAlias: Owner } 

一旦插件正常工作,我打算從上面的表中刪除密碼,並使用默認的密碼,並強制大家做出新的通行證。

編輯:

感謝本我有模板工作我認爲。無論如何,它都會添加到數據庫中。

class Doctrine_Template_Owned extends Doctrine_Template 
{ 
    protected $_options = array(
     'name'   => 'owner_id', 
     'type'   => 'integer', 
     'length'  => 11, 
     'options'  => array(
      'default' => 0, 
      'notnull' => true 
     ) 
    ); 

    public function setTableDefinition() 
    { 
     $this->hasColumn($this->_options['name'], $this->_options['type'], $this->_options['length'], $this->_options['options']); 
     $this->addListener(new Doctrine_Template_Listener_Owned($this->_options)); 
    } 
} 

但是,聽者不會被調用。

class Doctrine_Template_Listener_Owned extends Doctrine_Record_Listener 
    { 
     protected $_options = array(); 

     public function __construct(array $options) 
     { 
      $this->_options = $options; 
     } 

     public function preDqlSelect(Doctrine_Event $event) 
     { 
      $params = $event->getParams(); 
      $field = $params['alias'] . '.' . $this->_options['name']; 
      $query = $event->getQuery(); 

      if ((! $query->isSubquery() || ($query->isSubquery() && $query->contains(' ' . $params['alias'] . ' '))) && ! $query->contains($field)) { 
       $query->addPendingJoinCondition(
        $params['alias'], $field . ' = ' . sfContext::getInstance()->getUser()->getGuardUser()->getId() 
        ); 
      } 
     } 
    } 

該監聽器的代碼是刪除了時間表和布爾檢查的軟刪除代碼。該行不會出現在開發欄中。

回答

0

如果您需要在此應用程序範圍內執行此操作,我將使用原則行爲,而不是在操作級別執行此操作。請看看lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/Doctrine/Template中的SoftDelete行爲如何工作(它的兩個文件,一個模板和一個監聽器),以及監聽器子文件夾) - 你應該能夠適應它,總是添加一個user_id = x子句,而不是它目前的功能,然後將它添加到相關類的schema.yml中。

然後,你需要獲得user_id的教義。您可以將一個名爲「configureDoctrine」的方法添加到config/ProjectConfiguration.class.php中,如果它存在於doctrine中,它將被調用。不知道在doctrine中傳遞給它的最好方式,但是您應該將當前用戶的用戶id傳遞給此方法中的doctrine - sfContext :: getInstance() - > getUser() - > getGuardUser() - > getId()爲拿到它,爲實現它。行爲應該從這裏拉取價值。如果沒有其他,監聽器或模板類的靜態屬性應該工作。

您可以通過從模板中調用sfContext :: getInstance直接選擇用戶id,但不建議,因爲它會在doctrine/symfony之間創建額外的鏈接,這是違背整個依賴注入/無全局狀態的特性symfony的,而且東西遲早會破壞的。

如果因爲任何原因無法正常工作,我會考慮重寫生成器模板中的相關文件(在lib/vendor/symfony/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/admin/)。您可以通過添加項目頂層數據文件夾下的任何文件來完成此操作。或者我認爲管理員生成的模塊也會拋出您可能會聽到的事件...

迴應關於它的評論無效,如果您還沒有使用任何其他依賴回調的行爲,請確保您的databases.yml具有use_dql_callbacks:true屬性:

all: 
    doctrine: 
    class: sfDoctrineDatabase 
    param: 
     dsn:  mysql:host=localhost;dbname=mydb 
     username: myuser 
     password: mypassword 
     attributes: 
     use_dql_callbacks: true 
+0

謝謝本。我已經添加了我寫給帖子的代碼。儘管模板接縫正在工作,並且正確地將該列添加到數據庫,但偵聽器不會。 – Kye 2010-09-12 15:06:26

+0

請參閱編輯reql_callbacks。 – benlumley 2010-09-13 07:28:32

+0

非常感謝Ben。沒有你的幫助,我對symfony一無所知。 – Kye 2010-09-13 08:16:08

0

只需更改actions.class.php文件中的函數即可。 例如,可以更改listSuccess函數以篩選結果,其中ID = USERID

+0

那還會考慮默認篩選器嗎? – Kye 2010-09-10 14:31:22

+0

它應該。讓我知道它是否不;) – jgallant 2010-09-10 15:31:45

+0

唯一的問題是,目前沒有listSuccess,因爲它使用yaml文件。我將如何寫一個獲取它即將執行的查詢以添加addWhere('user_id =?',$ userID); – Kye 2010-09-10 18:36:10

相關問題