2013-05-07 28 views
5

我的schema.yml學說關係返回字符串

Organisation: 
    columns: 
    id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true } 
    name: { type: string(100), notnull: true, unique: true } 
    parent_organisation_id: { type: integer(4), notnull: false } 

    relations: 
    ParentOrganisation: { class: Organisation, local: parent_organisation_id, foreignAlias: ChildOrganisations } 

一些組織所存儲的整數值0,沒有這樣的organisation_id。出乎我的意料,當我運行這段代碼

class organisationActions extends autoOrganisationActions{ 

    public function executeEdit(sfWebRequest $request){ 

     $this->organisation = $this->getRoute()->getObject(); 
     $p = $this->organisation->getParentOrganisationId(); 
     var_dump($p); 

結果是字符串(1)「0」

爲什麼這不是返回一個整數,這樣我可以比較=== 0

+0

嗨@jdog,你使用的環境和堆棧? PHP版本,sf版本(1.4我想......),推動還是學說? MySQL或Postgres?所以我可以複製你的問題.... – Matteo 2015-03-16 09:30:30

+0

嗨@jdog,我的答案呢? – Matteo 2015-03-18 10:55:41

+0

嗨@jdog有什麼消息嗎? – Matteo 2015-03-20 08:59:59

回答

3

我做一些測試,我發現實體的每個值都通過神奇的調用返回,每個實體模型sfDoctrineRecord的父類都在_call方法中執行。所以call_user_func_array的返回類型對字符串或int等似乎沒有區別。我們在每個實體的每個字段上都有同樣的行爲,因此也實現了這個字段。

所以,解決方法,可以實現對檢查您的自定義的getter如果記錄爲空或爲第一(ID = 0),比較操作如下:

class Organisation extends BaseOrganisation 
{ 

     public function getParentIdAsIntegerOrNullOtherwise() 
     { 
      $an_id = $this->getParentOrganisationId(); 

      if (! is_null($an_id)) 
      { 
       return intval($an_id); 
      } 

      return NULL; 
     } 
    } 

在控制器:

$p = $this->organisation->getParentIdAsIntegerOrNullOtherwise(); 

    var_dump($p); 

將傾倒

NULL 

如果不與任何父節點

,並傾倒

int(0) 

如果此鏈接到與ID = 0

希望這有助於

讓我知道元素你怎麼看它

+0

嗨@matteo,對於延遲迴到這個抱歉抱歉。我看到我的錯誤是,結果實際上是null,因爲var_dump()而被強制轉換爲字符串?無論如何,你的回答幫助我解決了這個問題。 – jdog 2015-03-22 21:03:10