我有一个有关联的模型。根表是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]
我如何使用cake的find方法,以便我可以列出所有地址,电话,通过某人搜索而向某人发送的名字
1)姓氏 2)电话号码
即。我希望所有与模型相关的数据都通过cake在这些表上进行连接。
MasterStudent模型中的代码是
$opts = array(
'conditions' => array(
'MasterStudentName.last_name LIKE ' => $searchvalue . '%'
)
);
$this->MasterStudent->recursive = 1;
$data = $this->MasterStudent->find('all', $opts);
使用JohnP方法生成的查询是
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'
答案 0 :(得分:0)
组织数据的一种奇怪的方法,但是应该这样做。
$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);
递归设置为1,因此它将拉出所有关联。