簡體   English   中英

Cakephp保存一個主鍵不是'id'的表

[英]Cakephp Save with a table where the primary key is not 'id'

我有一個現有的Web應用程序,我轉換為使用CakePHP。 問題是大多數表的主鍵都是這種格式“$ {table_name} _id”(story_id)而不是'id'的CakePHP方式

當我嘗試更新故事表中某行的某些字段時,Save()函數將返回false。 有沒有辦法從Save()函數獲取更詳細的錯誤報告。

當我設置Configure::write('debug', 2); core.php檢查SQL語句我沒有看到任何UPDATE命令,只看到SELECT語句。

我試圖編輯控制器添加以下行來手動設置控制器的id字段,但它沒有幫助。

$this->Story->id = $this->data['Story']['story_id'] ; 

我的想法已經不多了。 有什么建議?

我已經包含了我在下面使用的源代碼

故事控制器:

 function admin_edit($id = null) 
 {
  if (!$id && empty($this->data)) {
   $this->Session->setFlash(__('Invalid '. Configure::read('Site.media') , true));
   $this->redirect(array('action'=>'index'));
  }

  $this->layout = 'admin';  
  if (!empty($this->data)) {
   if ($this->Story->save($this->data)) {
    $this->Session->setFlash(__('The '. Configure::read('Site.media') .' has been saved', true));
   } else {
    $this->Session->setFlash(__('The '. Configure::read('Site.media') .' could not be saved. Please, try again.', true));
   }
  }

  $this->data = $this->Story->read(null, $id );
 }

故事模型:

class Story extends AppModel {
    var $name = 'Story';
    var $primaryKey = 'story_id';

    var $validate   = array(
       'author_id'  => array('numeric'),       
       'title'   => array('notempty'),
       'story'   => array('notempty'),
       'genra'   => array('notempty'),
       'form'    => array('notempty'),  
       'wordcount'  => array('Please enter a number between 1 and 1000' => array( 
              'rule'   => array('range', 1, 1001),
              'message'  => 'Please enter a number between 1 and 1000' ),
              'Required'  => array( 'rule' => 'numeric', 'required' => true )
            )
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
     'Author' => array(
      'className' => 'Author',
      'foreignKey' => 'author_id'
     )
    );

    var $hasMany = array(
     'UserNote' => array(
      'className' => 'UserNote',
      'foreignKey' => 'story_id',
      'dependent' => false,
      'conditions' => 'UserNote.notes != ""'
     )
    );
}

故事視圖:

    echo $form->create('Story', array('action' => 'edit' ) );
    echo $form->input('story_id',array('type'=>'hidden') );
    echo $form->input('title');
    echo $form->input('story');
    echo $form->input('bio' );
    echo $form->end('Update story details');?>

故事表

CREATE TABLE IF NOT EXISTS `stories` (
  `story_id` int(11) NOT NULL AUTO_INCREMENT,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `closed` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `author_id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `story` text NOT NULL,
  `genra` varchar(255) NOT NULL,
  `form` varchar(128) DEFAULT NULL,
  `wordcount` varchar(255) NOT NULL,
  `terms` varchar(255) NOT NULL DEFAULT '0',
  `status` varchar(255) NOT NULL DEFAULT 'slush',
  `published` date NOT NULL,
  `payment` varchar(255) NOT NULL DEFAULT 'none',
  `paypal_address` varchar(255) NOT NULL,
  `resubmission` tinyint(1) NOT NULL DEFAULT '0',
  `bio` text NOT NULL,
  `password` varchar(255) NOT NULL DEFAULT 'yyggrrdd',
  `comments` text NOT NULL,
  PRIMARY KEY (`story_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10905 ;

您應該手動覆蓋模型中的主鍵字段(這是執行此操作的正確位置 - 主鍵字段的名稱是模型的屬性,而不是應該在控制器中“捏造”的東西。)

class Example extends AppModel {    var $primaryKey = 'example_id'; // example_id is the field name in the database}

以上代碼來自http://book.cakephp.org/view/437/primaryKey

雖然關閉驗證的建議會起作用,但這不是正確的方法。

最后,如果您在控制器中設置模型變量,則使用$ this-> Model-> set('attributeName',value)而不是$ this-> Model-> attributeName

看起來故事控制器正在驗證數據,數據無效。 將以下行添加到控制器將停止數據驗證。

$this->Story->validate = array(); // Stop valadation on the story.

我在這個頁面上找到了這個解決方案15必不可少的CakePHP技巧

暫無
暫無

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

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