Cakephp 3更新查询

时间:2016-11-09 09:08:46

标签: cakephp-3.0

如何在cakephp 3中更新数据?

8 个答案:

答案 0 :(得分:6)

试试这个,

    hexString = hexString.toUpperCase();
    int sign = hexString.charAt(0) == '-' ? -1 : 1;
    hexString = hexString.replace("-", "");
    decResult = Integer.parseInt(hexString, 16) * sign;
    System.out.println(decResult);

答案 1 :(得分:3)

您可以使用其他方法:

$tablename = TableRegistry::get("Model");
$conditions = array('id'=>12);
$fields = array('name'=>'andro','family'=>'lpoez');
$tablename->updateAll($fields, $conditions);

答案 2 :(得分:0)

use Cake\ORM\TableRegistry;

$articlesTable = TableRegistry::get('Articles');
$article = $articlesTable->get(12); // Return article with id 12

$article->title = 'CakePHP is THE best PHP framework!';
$articlesTable->save($article);

答案 3 :(得分:0)

使用newEntity()或patchEntity()时,您还可以创建/更新连接表信息。您的POST数据应如下所示:

//添加新记录,

$table = $this->Table->newEntity();

if($this->request->is(['post'])){
   $post_data = $this->request->data;
   $table = $this->Tags->patchEntity($table, $post_data);
   $this->Table->save($table);  //save record
}

//对于更新记录

$table = $this->Table->get($id); //get data using id

if ($this->request->is(['post'])) {
   $post_data = $this->request->data;
   $table = $this->Tags->patchEntity($table, $post_data);
   $this->Table->save($table);  //update record
}

答案 4 :(得分:0)

更新数据同样容易,save()方法也用于此目的:

use Cake\ORM\TableRegistry;

$articlesTable = TableRegistry::get('Articles');
$article = $articlesTable->get($id); // Return article with id = $id (primary_key of row which need to get updated)

$article->title = 'CakePHP is THE best PHP framework!';
// other fields if necessary
 ..........
$articlesTable->save($article);

CookBook official Documentation

答案 5 :(得分:0)

有多种更新数据的方式。

您可以获取现有实体和更改值并更新: https://book.cakephp.org/3.0/en/orm/saving-data.html#updating-data

$entity = TableRegistry::get('Users')->get(1);
$entity->name = 'foo';
TableRegistry::get('Users')->save($entity);

您可以使用数组修补实体以获取实例请求数据: https://book.cakephp.org/3.0/en/orm/saving-data.html#merging-request-data-into-entities

$entity = TableRegistry::get('Users')->get(1);
TableRegistry::get('Users')->patchEntity($entity, $this->request>getData);
TableRegistry::get('Users')->save($entity);

答案 6 :(得分:0)

执行此操作的一种方法是创建编辑功能。您可以使用Postman或其他工具来处理数据。因此,您可以使用Put方法和url中的id将要修改的字段放在邮递员的正文中。 代码是这样的:

 public function edit($id)
{
    $result = NULL;
       $this->request->allowMethod(['put']);
        if ($this->request->is(['put'])) {
           $data = $this->request->getData();
           $result = $this->Articles->get($id);
           if($result) {
              $result = $this->Articles->patchEntity($result, $data);
              $status = $this->Articles->save($result);
              if ($status) {
                    $status = true;
                    $this->setResponse($this->response->withStatus(200));
                    $result = ['new_data' => $result];
              } else {
                  $this->setResponse($this->response->withStatus(400));
                  $result = $data;
              }
           } else {
              $this->setResponse($this->response->withStatus(404));
              $result = $data;
           }            
        } 
        return $result;
}

OBS:要使用此功能,您需要在表中使用bake命令生成MVC框架。

答案 7 :(得分:0)

有关系 hasOne: table-A hasOne table-B ,你可以这样做:

*在表 A 的控制器中:

$ralationTable = TableRegistry::getTableLocator()->get('table-B');

$data = $ralationTable ->newEntity();

$data->field1 = 'something';

$data->field1 = 'something';  

$this->table-A->table-B->save($data);