这里是我的数据库模式:如何使用CakePHP模型关联查找所有对象?
create table Personas
(
id int primary key AUTO_INCREMENT,
Nombre varchar(255),
Apellidos varchar(255),
FechaDeNacimiento date,
Sexo Bool,
CarnetDeIdentidad varchar(255)
);
create table Tutors
(
id int primary key AUTO_INCREMENT,
persona_id int,
FOREIGN KEY (persona_id) REFERENCES Personas(id)
);
create table Alumnos
(
id int primary key AUTO_INCREMENT,
persona_id int,
FOREIGN KEY (persona_id) REFERENCES Personas(id)
);
create table CoordinadorDeProgramas
(
id int primary key AUTO_INCREMENT,
persona_id int,
FOREIGN KEY (persona_id) REFERENCES Personas(id)
);
这里是我的模型声明:
<?php
class Alumno extends AppModel {
public $belongsTo = 'Persona';
}
<?php
class Coordinadordeprograma extends AppModel {
public $belongsTo = 'Persona';
}
<?php
class Tutor extends AppModel {
public $belongsTo = 'Persona';
}
<?php
class Persona extends AppModel {
public $hasOne = array('Alumno', 'Tutor', 'Coordinadordeprograma');
}
在我的控制器我只是想,如果他们有Alumnos外键关系(获取所有的Persona记录例)。
这里是我的代码,我希望它说明了什么我想要做的事:
public function filter($type = null) {
if ($type == "alumno") { // www.app.com/personas/filter/alumno
$this->set('personas', $this->Alumno->Persona->find('all'));
}
}
但是这是回访每一个假面记录,而不是只具有在Alumno表中的记录的人。
你如何建议我解决这个问题?我认为通过使用$this->Alumno->Persona
我只能到达Alumno表中的女神人物。
谢谢!