简体   繁体   English

CakePHP模型无法获取表关联

[英]CakePHP model fails to grab table association

I'm working with CakePHP to develop a web application that has a few tables and some relationships between them. 我正在与CakePHP合作开发一个Web应用程序,该应用程序具有一些表以及它们之间的某些关系。 In this instance I have a series of meetings which reference a calendar year, a department and the relevant school however the model fails to grab the associated table information when data is returned from the controller to the view. 在这种情况下,我有一系列参考日历年,部门和相关学校的会议,但是当数据从控制器返回到视图时,模型无法获取相关的表信息。

There is already existing $belongsTo relationships between staff and the department and school they belong to and I also managed to grab the associated meeting when the calendar array is returned on the calendar index but ideally I want to be able to list all the meetings with the names of calendars, departments and schools rather than the id field stored in the meetings table. 员工与他们所属的部门和学校之间已经存在$belongsTo关系,并且当日历索引返回日历数组时,我也设法抢占了相关的会议,但理想情况下,我希望能够列出所有与日历,部门和学校的名称,而不是会议表中存储的id字段。

Here are my tables: 这是我的桌子:

dbo.meetings
    id (int) *primary key*
    calendar_id (int)
    department_id (int)
    school_id (int)
    created (datetime2(7))
    modified (datetime2(7))

dbo.calendar
    id (int) *primary key*
    name (nvarchar(50))
    startdate (datetime2(7))
    enddate (datetime2(7))
    created (datetime2(7))
    modified (datetime2(7))

dbo.schools
    id (int) *primary key*
    name (nvarchar(255))
    (other fields)

dbo.departments
    id (int) *primary key*
    name (nvarchar(255))
    (other fields)

Here is the controller for Meetings: 这是会议的控制器:

<?php
App::uses('AppController','Controller');

class MeetingsController extends AppController {
    public $helpers = array('Form');

    public function beforeFilter() {
        parent::beforeFilter();
    }

    public function index() {
        $this->set('meetings', $this->Meeting->find('all'));
    }
}
?>

Here is the model for Meeting: 这是会议的模型:

<?php
App::uses('AppModel','Model');

class Meeting extends AppModel {
    public $primaryKey = 'id';
    public $recursive = -1;

    public $belongsTo = array(
        'Calendar' => array(
            'className' => 'Calendar',
            'foreignKey' => 'calendar_id'
        ),
        'School' => array(
            'className' => 'School',
            'foreignKey' => 'school_id'
        ),
        'Department' => array(
            'className' => 'Department',
            'foreignKey' => 'department_id'
        )
    );
}
?>

Currently the index.ctp file in /View/Meetings just contains <?php echo var_dump($meetings); ?> 当前, /View/Meetingsindex.ctp文件仅包含<?php echo var_dump($meetings); ?> <?php echo var_dump($meetings); ?> which is printing out the array until I can get the association working and then I will restyle it as required. <?php echo var_dump($meetings); ?>这将打印出数组,直到我可以使关联正常工作,然后根据需要重新设置其样式。

Here is what the array looks like atm: 这是atm数组的样子:

array(1) {
  [0]=>
  array(1) {
    ["Meeting"]=>
    array(6) {
      ["id"] => string(1) "1"
      ["calendar_id"] => string(1) "1"
      ["department_id"] => string(2) "33"
      ["school_id"] => string(1) "1"
      ["created"] => NULL
      ["modified"] => NULL
    }
  }
}

For some reason it just won't fetch the calendar, department and school details that it should and I want it to. 由于某种原因,它只是无法获取我应该获取的日历,部门和学校的详细信息。 Can anyone help? 有人可以帮忙吗?

Edit: Model for School 编辑:学校模型

<?php
App::uses('AppModel','Model');

class School extends AppModel {

    public $validate = array(
        'name' => array(
            'required' => array(
                'rule' => 'notBlank',
                'message' => 'A school name is required.'
            )
        )
    );
}
?>

Make sure that you are enabling the Containable behaviour for your model's using public $actsAs = ['Containable']; 确保使用public $actsAs = ['Containable'];启用模型的Containable行为 public $actsAs = ['Containable']; :- :-

App::uses('AppModel','Model');

class Meeting extends AppModel {
    public $recursive = -1;
    public $actsAs = array('Containable');

    public $belongsTo = array(
        'Calendar',
        'School',
        'Department'
    );
}

To apply Containable to all models set it in AppModel so that each model inherits the behaviour. 若要将Containable应用于所有模型,请在AppModel中对其进行设置,以使每个模型都继承行为。 Also, as long as you stick to CakePHP naming conventions you don't need to specify the className and foreignKey of your associations. 另外,只要您遵循CakePHP命名约定,就无需指定关联的classNameforeignKey

Now $this->Meeting->find('all') in your MeetingsController should return all the associated data along with the meeting. 现在, MeetingsController $this->Meeting->find('all')应该返回所有相关的数据以及会议。 If you only want it to return some of the associated data you can pass the contain option to the find . 如果只希望它返回一些关联的数据,则可以将contain选项传递给find For example:- 例如:-

$this->Meeting->find(
    'all',
    array(
        'contain' => array(
            'School',
            'Department'
        )
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM