HasAndBelongsToMany返回不必要的数据

时间:2015-04-13 13:28:50

标签: cakephp

当我使用hasAndBelongsToMany获取记录时,它会向我发送以下输出:

[Location] => Array
    (
        [id] => 1
        [uuid] => 5a8e4d7f-e1d0-11e4-9567-c03fd5623744
        [name] => chandigarh
        [address_line1] => dummy
        [address_line2] => dummy
        [latitude] => 30.737780
        [longitude] => 76.784439
        [timezone] => UTC
        [is_deleted] => 0
        [created] => 2015-04-13 16:59:31
        [modified] => 2015-04-13
        [entry_ts] => 2015-04-13 16:59:31
    )

[Department] => Array
    (
        [0] => Array
            (
                [dept_name] => Heater
                [LocationDepartment] => Array
                    (
                        [id] => 1
                        [location_id] => 1
                        [department_id] => 1
                        [is_deleted] => 0
                        [created] => 2015-04-13 17:00:50
                        [modified] => 2015-04-13 17:00:50
                        [entry_ts] => 2015-04-13 17:00:50
                    )

            )
    )

但我不需要LocationDepartment数组。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

如果您使用快速查找,则可以在执行此操作之前执行此操作

$this->Location->recursive = 1;
$this->Location->findById($id);

如果您使用完整查找,则可以将递归作为传递参数的一部分添加

$this->Location->find('first', array('conditions' => array('id' => $id), 'recursive' => 1));

发布示例代码将有助于解决这个简单的问题


您还可以添加“可容纳的'模型上的行为,可以让您准确指定要检索的模型/数据。

class Location extends AppModel {

  var $actsAs = array('Containable');

  function GetRecord($id) {
    return $this->find('first', array(
      'conditions' => array(
        'id' => $id,
      ),
      'contain' => array('Department'),
    );
  } 
}