我正在学习CakePHP,但在处理关联的方式上遇到了一些问题(可能只是我的误解)。我有一个RESULTS表,它有一个由表WORKSHEETS和STUDENTS的两个外键组成的复合键。下面是RESULTS、Worksheets和Students之间的关系。
结果N - 1学生
结果N - 1工作表
Data Schema here
我的问题是,RESULTS表的搭建ADD页没有显示STUDENTS和WORKSHEETS的<select>
字段。
首先,这是results/add.php页面的HTML,由Form-〉控件生成。STUDENT和WORKSHEET的两个输入不是Select,而是隐藏的输入。
<input type="hidden" name="student_id" options="(object)Cake\Datasource\ResultSetDecorator" id="student-id"/>
<input type="hidden" name="worksheet_id" options="(object)Cake\Datasource\ResultSetDecorator" id="worksheet-id"/>
<div class="input text">
<label for="result">Result</label>
<input type="text" name="result" id="result" maxlength="6"/></div>
<button type="submit">Submit</button>
下面是为ResultsTable.php搭建的initialize()代码:
$this->setTable('results');
$this->setDisplayField(['student_id', 'worksheet_id']);
$this->setPrimaryKey(['student_id', 'worksheet_id']);
$this->addBehavior('Timestamp');
$this->belongsTo('Students', [
'foreignKey' => 'student_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Worksheets', [
'foreignKey' => 'worksheet_id',
'joinType' => 'INNER',
]);
下面是add.php中的表单控件生成(前两行在搭建过程中甚至没有添加)。
echo $this->Form->control('student_id', ['options' => $students]);
echo $this->Form->control('worksheet_id', ['options' => $worksheets]);
echo $this->Form->control('result');
下面是控制器中add函数的最后一部分:
$students = $this->Results->Students->find('list', ['limit' => 200])->all();
$worksheets = $this->Results->Worksheets->find('list', ['limit' => 200])->all();
$this->set(compact('result', 'students', 'worksheets'));
初始化()
$this->setTable('students');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Ranks', [
'foreignKey' => 'rank_id',
]);
$this->belongsTo('Grades', [
'foreignKey' => 'grade_id',
'joinType' => 'INNER',
]);
$this->hasMany('Results', [
'foreignKey' => 'student_id',
]);
初始化()
$this->setTable('worksheets');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Subranks', [
'foreignKey' => 'subrank_id',
'joinType' => 'INNER',
]);
$this->hasMany('Results', [
'foreignKey' => 'worksheet_id',
]);
}
关于为什么会发生这种情况的一些提示?
如果您需要任何其他信息或代码,请告诉我。
PS:具有belongsTo关系的所有其他表都已正确搭建,并具有正确的<select>
字段。
调试($workesks)输出:
ROOT\templates\Results\add.php (line 8)
object(Cake\Datasource\ResultSetDecorator) id:0 {
'count' => (int) 12
}
调试($students)输出:
ROOT\templates\Results\add.php (line 10)
object(Cake\Datasource\ResultSetDecorator) id:0 {
'count' => (int) 2
}
从控制器的add方法中删除-〉all()后,得到一个查询对象,如下所示:
ROOT\templates\Results\add.php (line 10)
object(Cake\ORM\Query) id:0 {
'(help)' => 'This is a Query object, to get the results execute or iterate it.'
'sql' => 'SELECT Students.id AS Students__id, Students.name AS Students__name FROM students Students LIMIT 200'
'params' => [
]
'defaultTypes' => [
'Students__id' => 'integer',
'Students.id' => 'integer',
'id' => 'integer',
'Students__student_number' => 'string',
'Students.student_number' => 'string',
'student_number' => 'string',
'Students__name' => 'string',
'Students.name' => 'string',
'name' => 'string',
'Students__rank_id' => 'integer',
'Students.rank_id' => 'integer',
'rank_id' => 'integer',
'Students__worksheets_count' => 'integer',
'Students.worksheets_count' => 'integer',
'worksheets_count' => 'integer',
'Students__perfects_count' => 'integer',
'Students.perfects_count' => 'integer',
'perfects_count' => 'integer',
'Students__accuracy_rate' => 'integer',
'Students.accuracy_rate' => 'integer',
'accuracy_rate' => 'integer',
'Students__created' => 'date',
'Students.created' => 'date',
'created' => 'date',
'Students__modified' => 'date',
'Students.modified' => 'date',
'modified' => 'date',
'Students__email' => 'string',
'Students.email' => 'string',
'email' => 'string',
'Students__password' => 'string',
'Students.password' => 'string',
'password' => 'string',
'Students__grade_id' => 'integer',
'Students.grade_id' => 'integer',
'grade_id' => 'integer',
]
'decorators' => (int) 0
'executed' => false
'hydrate' => true
'buffered' => true
'formatters' => (int) 1
'mapReducers' => (int) 0
'contain' => [
]
'matching' => [
]
'extraOptions' => [
]
'repository' => object(App\Model\Table\StudentsTable) id:1 {
'registryAlias' => 'Students'
'table' => 'students'
'alias' => 'Students'
'entityClass' => 'App\Model\Entity\Student'
'associations' => [
(int) 0 => 'Ranks',
(int) 1 => 'Grades',
(int) 2 => 'Results',
]
'behaviors' => [
(int) 0 => 'Timestamp',
]
'defaultConnection' => 'default'
'connectionName' => 'default'
}
}
ROOT\templates\Results\add.php (line 8)
object(Cake\ORM\Query) id:0 {
'(help)' => 'This is a Query object, to get the results execute or iterate it.'
'sql' => 'SELECT Worksheets.id AS Worksheets__id FROM worksheets Worksheets LIMIT 200'
'params' => [
]
'defaultTypes' => [
'Worksheets__id' => 'integer',
'Worksheets.id' => 'integer',
'id' => 'integer',
'Worksheets__subrank_id' => 'integer',
'Worksheets.subrank_id' => 'integer',
'subrank_id' => 'integer',
'Worksheets__link' => 'string',
'Worksheets.link' => 'string',
'link' => 'string',
'Worksheets__created' => 'date',
'Worksheets.created' => 'date',
'created' => 'date',
'Worksheets__modified' => 'date',
'Worksheets.modified' => 'date',
'modified' => 'date',
]
'decorators' => (int) 0
'executed' => false
'hydrate' => true
'buffered' => true
'formatters' => (int) 1
'mapReducers' => (int) 0
'contain' => [
]
'matching' => [
]
'extraOptions' => [
]
'repository' => object(App\Model\Table\WorksheetsTable) id:1 {
'registryAlias' => 'Worksheets'
'table' => 'worksheets'
'alias' => 'Worksheets'
'entityClass' => 'App\Model\Entity\Worksheet'
'associations' => [
(int) 0 => 'Subranks',
(int) 1 => 'Results',
]
'behaviors' => [
(int) 0 => 'Timestamp',
]
'defaultConnection' => 'default'
'connectionName' => 'default'
}
}
1条答案
按热度按时间bd1hkmkf1#
尝试强制类型选择到表单控件