Cakephp 3表格,插入FOREIGN KEY

时间:2018-01-17 18:57:04

标签: php mysql cakephp cakephp-3.0

这是我的鳕鱼:

 $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);

但我需要:

 Set <Character> setChar = map.keySet();
 int returnC = 0;
 for (Character c : setChar) {
       if (map.get(c) > 1) { 
             returnC++;
       } 
 }
 return returnC;

Preoblem是我需要在Controller中插入它而不是在视图中。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

假设$user_id$picture_id是已在控制器函数中声明的变量,并且如果它们来自用户输入,则已经过适当的安全检查(例如,如果它们来自网址,您已经确认它们没有问题,而且不仅仅是信任它们,因为您为人们提供链接的网址始终有效),那么以下内容应该有效:

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