簡體   English   中英

Cakephp 3種形式,插入到FOREIGN KEY中

[英]Cakephp 3 forms, insert into FOREIGN KEY

這是我的鱈魚:

<?= $this->Form->create('Posts', array('url' => array('controller' => 'MyController', 'action' => 'index')));?>
                        <?= $this->Form->input('title', array('type'=>'title','class'=>'form-control mr-sm-2'));?>
                        <?= $this->Form->textarea('text', array('type'=>'text','rows' => '3','class'=>'form-control'));?>
                        <?= $this->Form->submit('Posten', array('class'=>'btn btn-outline-success my-2 my-sm-0','style'=>'float:right'));?>
                    <?= $this->Form->end();?>

這是我的控制器:

 $post = $this->Posts->newEntity();
    if ($this->request->is('post')) {
        $post = $this->Posts->patchEntity($post, $this->request->getData());
        debug($post);
        if ($this->Posts->save($post)) {

        $this->Flash->success(__('You have posted something.'));
        }


        return $this->redirect(['action' => 'index']);
    }else {
        $this->Flash->error(__('Error, plase check your'));
    }

問題是問題:

INSERT INTO posts (title, text) VALUES (:c0, :c1);

但是我需要 :

INSERT INTO posts (title, text,user_id,picture_id) VALUES (a,b,$user_id,$picture_id);

Preoblem是我需要將其插入Controller中而不在視圖中。 有人能幫我嗎?

假設$user_id$picture_id是在控制器函數中聲明的變量,並且如果它們來自用戶輸入(例如,如果它們在URL中),則已經經過了正確的安全性檢查(例如,如果它們在URL中,則表明它們可以)並且不僅僅信任他們,因為您提供給人們鏈接的URL始終是有效的),那么以下內容應該有效:

$post = $this->Posts->patchEntity($post,
    array_merge($this->request->getData(), compact('user_id', 'picture_id'))
);

暫無
暫無

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

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