Cakephp Querybuilder:使用与所选字段相同的字段更新行

Cakephp Querybuilder : update rows with an identical field as selected one

我的应用程序的目标是通过特许经营安排 posts 到其特许经营。 总部在特定日期和时间安排 post,带有文字和潜在图像。 它在数据库中为每个特许经营(id,franchise_id,user_id,文本,图像,网络,post_id)

创建包含所有必要信息的 post

post_id 包含除了 franchise_id.

之外完全相同的每一行都相同的 id

当我添加 post 时,效果很好。但在编辑时,由于它获取了 post 的 ID,因此它只会编辑与该 ID 匹配的 post。 当它是特许经营时这很好,然后它将 post_id 更改为自定义值,并且将独立于其他值。

但是当 HQ(超级管理员)登录时,我希望他编辑所有与 post_id 所选择的相匹配的内容。

查询生成器不是我习惯的东西,有时我考虑过将其删除以用于标准 SQL,但如果它在那里也是有原因的,所以我希望您能帮助我使用 Cakephp 解决这个问题查询生成器。

public function edit($id = null){

    $event = $this->Events->get($id);

    if ($this->request->is(['post', 'put'])) {
        $event = $this->Events->patchEntity($event, $this->request->data,['associated' => ['Networks'], ]);
        if($isuper == 'true'){//if logged in user is superadmin

        }else{
        $event->user_id = $this->Auth->user('id');
        }


        if ($this->Events->save($event)) {



            $this->Flash->success(__('your post has been updated'));
            return $this->redirect(
                [
                    'action' => 'index',
                    date('Y', $event->date->getTimestamp()),
                    date('m', $event->date->getTimestamp()),
                    $event->company_id
                ]
            );
        }
        $this->Flash->error(__('unable to update your post'));
    }
    $this->set('event', $event);
    $this->layout = 'ajax';
}

您可以尝试使用 updateAll

进行批量更新

类似于:

$this->Events->updateAll(
    ['field' => true], // whatever fields you are updating
    ['post_id' => 'some_id'] // the selected post_id
);