2011-03-17 82 views
0

我有一個關聯模型。根表是Master_student。其他一切有一個外鍵由一個人的cakephp - 加入表格

1)最後搜索到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] 

如何使用蛋糕的find方法,這樣我可以列出所有地址,電話,姓名是亂棍一個人名稱 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_ 8],[MasterStudent]。[ssn] AS [MasterStudent _2],[MasterStudent]。[ssn] AS [MasterStudent_ 8] ] AS [MasterStudent _19],[MasterStudent]。[ssn] AS [MasterStudent_ 26],[MasterStudent] [SSN] AS [MasterStudent _36] FROM [MASTER_STUDENTS] AS [MasterStudent] WHERE [MasterStudentName] [LAST_NAME] = '史密斯'

+0

你真的還在使用PHP 4嗎? – 2011-03-18 18:10:19

+0

@Justin,這只是Cake的代碼生成。它確實保持兼容 – JohnP 2011-03-18 18:14:58

+0

我指的是在PHP類聲明中使用'var'。 – 2011-04-28 06:34:46

回答

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,因此它將拉動所有關聯。

+0

此查詢不會從主學生姓名錶中提取任何內容。它確實選擇了Master Student的所有列,並且因爲它試圖匹配主學生名錶中存在的MasterStudentName.last_name而失敗......幫助! – caker 2011-03-18 15:22:49

+0

我可以幫助你,除非你把你的數據庫結構。只需在你的問題中以簡單的格式提出表格即可。 _不只是轉儲你的sql_ – JohnP 2011-03-18 15:31:09

+0

感謝JohnP,我編輯了我原來的問題來表示一個簡化的表結構 – caker 2011-03-18 18:08:06