У меня есть модель с ассоциациями.Корневым столом является Master_student.У всего остального есть внешний ключ для master_student_ssn
<?php
class MasterStudent extends AppModel {
var $name = 'MasterStudent';
var $primaryKey = 'ssn';
var $displayField = 'SSN';
var $useTable = 'MASTER_STUDENTS';
var $order = array("ssn" => "asc");
var $hasMany = array(
'MasterEmail' => array(
'className' => 'MasterEmail',
'foreignKey' => 'master_student_ssn',
'conditions' => '',
'order' => 'source',
'limit' => '',
'dependent' => true
),
'MasterAddress' => array(
'className' => 'MasterAddress',
'foreignKey' => 'master_student_ssn',
'conditions' => '',
'order' => 'source',
'limit' => '',
'dependent' => true
),
'MasterPhone' => array(
'className' => 'MasterPhone',
'foreignKey' => 'master_student_ssn',
'conditions' => '',
'order' => 'source',
'limit' => '',
'dependent' => true
),
'MasterStudentName' => array(
'className' => 'MasterStudentName',
'foreignKey' => 'master_student_ssn',
'conditions' => '',
'order' => 'Effective_Date desc,source',
'limit' => '',
'dependent' => true
)
);
}
?>
[MASTER_STUDENTS](
[SSN] [int] NOT NULL,
[date_student_added] [datetime] NULL,
CONSTRAINT [PK_MASTER_STUDENT] PRIMARY KEY CLUSTERED
(
[SSN] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
[MASTER_ADDRESSES](
[id] [int] IDENTITY(1,1) NOT NULL,
[master_student_ssn] [int] NOT NULL,
[address1] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[city] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[state] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[zip] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_master_addresses] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
[MASTER_STUDENT_NAMES](
[ID] [int] IDENTITY(1,1) NOT NULL,
[master_student_ssn] [int] NOT NULL,
[First_Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Middle_Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Last_Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_MASTER_STUDENTS_NAMES] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Как я могу использовать метод поиска торта, чтобы я мог перечислить ВСЕ адреса, телефоны, имена, которые ему звонили, путем поиска по
* 1005 человека* 1) Фамилия 2) Номер телефона
т.е.Я хочу, чтобы все данные, связанные с моделью при помощи тортов, объединяли эти таблицы.
Код в модели MasterStudent:
$opts = array(
'conditions' => array(
'MasterStudentName.last_name LIKE ' => $searchvalue . '%'
)
);
$this->MasterStudent->recursive = 1;
$data = $this->MasterStudent->find('all', $opts);
Сгенерированный запрос с использованием подхода JohnP
ВЫБРАТЬ [MasterStudent]. [SSN] AS [MasterStudent__0], CONVERT (VARCHAR (20), [MasterStudent]. [Date_student_added], 20) AS [MasterStudent_ 1], [MasterStudent]. [Ssn] AS [MasterStudent _2], [MasterStudent]. [Ssn] AS [MasterStudent_ 8], [MasterStudent]. [Ssn] AS [MasterStudent _19], [MasterStudent]. [Ssn] AS [MasterStudent_ 26], [MasterStudent]. [Ssn] AS [MasterStudent _36] ОТ [MASTER_STUDENTS] AS [MasterStudent] ГДЕ [MasterStudentName]. [Last_name] = 'Smith'