mysql - Cakephp 查询生成器 : update rows with an identical field as selected one

标签 mysql cakephp query-builder

我的应用程序的目标是通过特许经营安排帖子到其特许经营。 总部安排在特定日期和时间发布帖子,并附有文字和潜在图像。 它使用数据库中每个特许经营商的所有必要信息创建帖子(id、franchise_id、user_id、文本、图像、网络、post_id)

post_id 包含一个与每一行相同的 ID,除了特许经营 ID 之外,该 ID 完全相同。

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

但是当总部( super 管理员)登录时,我希望他编辑所有与 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
);

关于mysql - Cakephp 查询生成器 : update rows with an identical field as selected one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44783690/

相关文章:

mysql - 删除指定了外键的表

mysql - LEFT JOIN 成为隐式连接我如何修复它?

php - 如果我在 blob 中没有图像以避免错误

cakephp - 如何将目标参数传递给 CakePHP 重定向?

Cakephp 和 Yii : similarities/differences/speed?

php - Symfony2 QueryBuilder 按 ID 顺序返回产品查询

php - 使用 PHP 和 MySQL 管理大量读写

java - 使用 MySQL 运行 Hibernate 5 的 Maven 依赖项是什么?

php - 为什么 Codeigniter 的查询生成器返回错误的 MySQL 查询两次?

php - Laravel 5.4 查询生成器存在 groupBy 问题