I have a model with associations. Root table is Master_student. Everything else has a foreign key to 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]
How can I use cake's find method such that I can list ALL addresses, phones, names that beling to a person by searching by a person's
1) Last Name 2)开发者_Go百科 Phone number
ie. I want all data associated with the model by cake doing a join on these tables.
The code in the MasterStudent Model is
$opts = array(
'conditions' => array(
'MasterStudentName.last_name LIKE ' => $searchvalue . '%'
)
);
$this->MasterStudent->recursive = 1;
$data = $this->MasterStudent->find('all', $opts);
The generated query with JohnP's approach is
SELECT [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] FROM [MASTER_STUDENTS] AS [MasterStudent] WHERE [MasterStudentName].[Last_name] = 'Smith'
A curious way to organize your data, but this should do it.
$opts = array(
'conditions' => array(
'MasterStudentName.last_name' => 'something', //assuming last name is stored in MasterStudentName
'MasterPhone.phone' => '23525222'
)
);
$this->MasterStudent->recursive = 1;
$data = $this->MasterStudent->find('all', $opts);
Recursive is set to 1 so it will pull all the associations.
精彩评论