2012-08-17 48 views
0

我有以下的JSON:的CakePHP和中可容納的行爲

{ 
    "Companies":[ 
     { 
     "CompanyPersonTask":{ 
      "company_id":"502d1844-3638-44dc-824c-14f02e0cc009", 
      "person_id":"2", 
      "task_id":"1" 
     }, 
     "Company":{ 
      "id":"502d1844-3638-44dc-824c-14f02e0cc009", 
      "name":"mostro", 
      "Office":[ 
       { 
        "id":"502d1844-b90c-44f5-84c5-14f02e0cc009", 
        "company_id":"502d1844-3638-44dc-824c-14f02e0cc009", 
        "region":"Emilia-Romagna", 
        "city":"Rozzemilia", 
        "address":"-", 
        "phone":"-", 
        "legal_office":true 
       } 
      ], 
      "CompanyPersonTask":[ 
       { 
        "company_id":"502d1844-3638-44dc-824c-14f02e0cc009", 
        "person_id":"2", 
        "task_id":"1" 
       } 
      ] 
     }, 
     "Person":{ 
      "id":"2", 
      "first_name":"Carlo", 
      "last_name":"Giusti", 
      "home_address":"Russi", 
      "job_address":null, 
      "phone":null, 
      "fax":null, 
      "mail":null, 
      "full_name":"Giusti Carlo", 
      "OfficePersonTask":[ 

      ], 
      "CompanyPersonTask":[ 
       { 
        "company_id":"502d1844-3638-44dc-824c-14f02e0cc009", 
        "person_id":"2", 
        "task_id":"1" 
       } 
      ] 
     }, 
     "Task":{ 
      "id":"1", 
      "short_name":"Proprietario", 
      "full_name":null, 
      "info":null, 
      "OfficePersonTask":[ 

      ], 
      "CompanyPersonTask":[ 
       { 
        "company_id":"502d1844-3638-44dc-824c-14f02e0cc009", 
        "person_id":"2", 
        "task_id":"1" 
       } 
      ] 
     } 
     } 
    ] 
} 

被此方法創建,CompanyPersonTask模型內部:

public function getCompaniesByRegion($region){ 
    $this->recursive = 2; 
    return $this->find('all'); 
} 

,但我希望我的JSON來使用此格式:

{ 
    "Companies":[ 
     { 
     "Company":{ 
      "id":"502d1844-3638-44dc-824c-14f02e0cc009", 
      "name":"mostro", 
      "Office":[ 
       { 
        "id":"502d1844-b90c-44f5-84c5-14f02e0cc009", 
        "company_id":"502d1844-3638-44dc-824c-14f02e0cc009", 
        "region":"Emilia-Romagna", 
        "city":"Rozzemilia", 
        "address":"-", 
        "phone":"-", 
        "legal_office":true 
       } 
      ] 
     }, 
     "Person":{ 
      "id":"2", 
      "first_name":"Carlo", 
      "last_name":"Giusti", 
      "home_address":"Russi", 
      "job_address":null, 
      "phone":null, 
      "fax":null, 
      "mail":null, 
      "full_name":"Giusti Carlo" 
     } 
     } 
    ] 
} 

如何修改我的方法?

回答

3

使用可包含的行爲來過濾結果數據。 欲瞭解更多解釋,請參閱this鏈接。

0

您可以在cake php中檢查unbindModel以移除關係。這將阻止不需要的表中的數據。 Check this.

<?php 
$this->Model->unbindModel(
    array('associationType' => array('associatedModelClassName')) 
); 
+0

我可以解除綁定的唯一模型是CompanyPersonTask,即定義該方法的模型... – 2012-08-17 16:45:17