簡體   English   中英

CakePHP 1.3存在($ id)函數示例/如何

[英]CakePHP 1.3 exists($id) function examples/how to

Cakephp 1.3是否存在一個現存($ id)函數,以查看表中是否存在記錄? 我有一個從另一台服務器同步的數據庫。 我的數據庫有2個表,一個是同步表,另一個是我用來擴展已同步表的表。 現在,我的應用程序列出了主(已同步)表中的所有項目,當它們單擊該項目時,它們會進入視圖以添加與第二個表不同的信息。 它將ID傳遞到第二個表。

我想做的是先檢查第二張表,看是否存在具有相應外鍵的記錄,如果存在,然后移到第二張表中該記錄的編輯屏幕,如果不存在,我想確保該記錄存在於第一個表中,如果存在,則將記錄添加到第二個表中,並以給定的$ id作為外鍵。

現在考慮一下,它將正確的ID傳遞給第二個表,我只是不希望用戶能夠鍵入數字並假定它存在於主表中並向第二個表中添加一條記錄, t實際上存在。 如果沒有檢查功能,可以使用關聯檢查嗎? 喜歡:

if (!$this->Table2->Table1->id) {
    //if id does not exist in parent table don't create the record in the second table and print an error
} else {
    //id does exist in parent table either add a new record with the foreign key being the id passed from parent or redirect to edit screen for that record in second table
}

使用Model :: exists()函數:

$model->id = 5;
if($model->exists()){
    // Record with ID 5 found
}else{
    // Record not found
}

您還可以嘗試同時加載和執行這兩項操作(檢查並加載是否存在)

$model->id = 5;
if($model->read()){
    // Record exists and you have in $model->data the data
}else{
    // Record not found
}

如果我理解得很好(我的英語不太好),那么您可以這樣做:

if(!$this->Table2->Table1->id || !$this->Table2->Table1->exists()){
    // Table1->id is empty or record not found
}else{
    // Table1->id has id and record exists
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM