<small id='xzG6y'></small><noframes id='xzG6y'>

    1. <i id='xzG6y'><tr id='xzG6y'><dt id='xzG6y'><q id='xzG6y'><span id='xzG6y'><b id='xzG6y'><form id='xzG6y'><ins id='xzG6y'></ins><ul id='xzG6y'></ul><sub id='xzG6y'></sub></form><legend id='xzG6y'></legend><bdo id='xzG6y'><pre id='xzG6y'><center id='xzG6y'></center></pre></bdo></b><th id='xzG6y'></th></span></q></dt></tr></i><div id='xzG6y'><tfoot id='xzG6y'></tfoot><dl id='xzG6y'><fieldset id='xzG6y'></fieldset></dl></div>
      • <bdo id='xzG6y'></bdo><ul id='xzG6y'></ul>

    2. <legend id='xzG6y'><style id='xzG6y'><dir id='xzG6y'><q id='xzG6y'></q></dir></style></legend>
      <tfoot id='xzG6y'></tfoot>

        Doctrine/Symfony 如何使用数组中的特定数据更新实体

        Doctrine/Symfony how to update entity with specific data from array(Doctrine/Symfony 如何使用数组中的特定数据更新实体)
          <bdo id='nqvOe'></bdo><ul id='nqvOe'></ul>
            <tbody id='nqvOe'></tbody>

          <i id='nqvOe'><tr id='nqvOe'><dt id='nqvOe'><q id='nqvOe'><span id='nqvOe'><b id='nqvOe'><form id='nqvOe'><ins id='nqvOe'></ins><ul id='nqvOe'></ul><sub id='nqvOe'></sub></form><legend id='nqvOe'></legend><bdo id='nqvOe'><pre id='nqvOe'><center id='nqvOe'></center></pre></bdo></b><th id='nqvOe'></th></span></q></dt></tr></i><div id='nqvOe'><tfoot id='nqvOe'></tfoot><dl id='nqvOe'><fieldset id='nqvOe'></fieldset></dl></div>

              <small id='nqvOe'></small><noframes id='nqvOe'>

                <legend id='nqvOe'><style id='nqvOe'><dir id='nqvOe'><q id='nqvOe'></q></dir></style></legend>

                • <tfoot id='nqvOe'></tfoot>
                • 本文介绍了Doctrine/Symfony 如何使用数组中的特定数据更新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我环顾四周太久没有运气.我的情况是我有一个有点大的表 +60 列,它在 Doctrine Entity 中表示.在 FosREST 上工作,我想要实现的是我想发送一个带有特定数据的 JSON,例如

                  I have looking around for too long with no luck. My situation is that i have a bit large table +60 columns which is represented in Doctrine Entity. Working on FosREST and what i want to achieve is that i want to send a JSON with specific data let's say for example

                  [phone] => new_phone
                  [name] => new_name
                  [id] => 1
                  

                  就像我说的,实体包含超过 60 列,如地址、图片、类别等...

                  while like i said the entity contains over 60 columns like address, picture, category, etc...

                  电话、姓名和身份证不是我每次都想更改的,但我每次都想更改一些列.所以有时我可能想更新电话和姓名,其他时候我想第三次更改类别我想更改类别和照片和地址有这样的吗?

                  and the phone, name and id are not what I want to change every time but i want to change some columns each time. So at some time i might want to update phone and name other time i want to change the category third time i want to change category and photo and address so is there anything like this ?

                  $entity->update($parameters);
                  

                  $parameters 如前所述动态更改.附:我知道我可以用类似的东西构建一个很长的函数

                  where the $parameters are dynamically changed as explained before. ps. i know that i can build a very long function with something like

                  if(isset($parameters['name']){
                       $entity->setName($parameters['name']);
                  }
                  

                  但是有 60 个 ifs 这听起来很愚蠢,有人有其他方法吗?谢谢

                  but with 60 ifs this just sounds idiotic anyone have any other approach ? Thanks

                  推荐答案

                  1) 如果参数以属性命名(这里有下划线注解),可以这样做

                  1) If the parameters are named after the attributes (here with underscore annotations), you can do this

                  use DoctrineCommonUtilInflector;
                  // ...
                  
                  public function setParameters($params) {
                      foreach ($params as $k => $p) {
                          $key = Inflector::camelize($k);
                          if (property_exists($this, $key)) {
                              $this->$key = $p;
                          }
                      }
                      return $this;
                  }
                  

                  2) 与 setter 相同

                  2) Same thing with the setters

                  use DoctrineCommonUtilInflector;
                  // ...
                  
                  public function setParameters($params) {
                      foreach ($params as $k => $p) {
                          $key = Inflector::camelize($k);
                          if (property_exists($this, $key)) {
                              $this->{'set'.ucfirst($key)}($p); // ucfirst() is not required but I think it's cleaner
                          }
                      }
                      return $this;
                  }
                  

                  3) 如果不是同名,你可以这样做:

                  3) If its not the same name, you can do this :

                  public function setParameters($params) {
                      foreach ($params as $k => $p) {
                          switch $k {
                              case 'phone':
                                  $this->phoneNumber = $p;
                                  break;
                              // ...
                          }
                      }
                      return $this;
                  }
                  

                  最佳方法是第二,但您应该定义白名单或黑名单,以避免用户更新您不希望他更新的内容.

                  EDIT : Best approach is number two but you should define a white-list or a black-list in order to avoid the user updating something you don't want him to.

                  这篇关于Doctrine/Symfony 如何使用数组中的特定数据更新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                        <tbody id='9PVuG'></tbody>
                    • <legend id='9PVuG'><style id='9PVuG'><dir id='9PVuG'><q id='9PVuG'></q></dir></style></legend>

                      <tfoot id='9PVuG'></tfoot>
                      1. <i id='9PVuG'><tr id='9PVuG'><dt id='9PVuG'><q id='9PVuG'><span id='9PVuG'><b id='9PVuG'><form id='9PVuG'><ins id='9PVuG'></ins><ul id='9PVuG'></ul><sub id='9PVuG'></sub></form><legend id='9PVuG'></legend><bdo id='9PVuG'><pre id='9PVuG'><center id='9PVuG'></center></pre></bdo></b><th id='9PVuG'></th></span></q></dt></tr></i><div id='9PVuG'><tfoot id='9PVuG'></tfoot><dl id='9PVuG'><fieldset id='9PVuG'></fieldset></dl></div>

                            <bdo id='9PVuG'></bdo><ul id='9PVuG'></ul>
                          • <small id='9PVuG'></small><noframes id='9PVuG'>