微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Yii3 及其 Cycle-ORM 集成设置出生日期?

如何解决如何使用 Yii3 及其 Cycle-ORM 集成设置出生日期?

前提

  1. MysqL 数据库 client_birthdate 字段设置为 DATE,如果用户不输入日期则可为空。
  2. 用户在 __form 文本框中输入客户的生日作为 MysqL 格式的字符串,无论是否为 YYYY/MM/DD。
  3. Yii3 的 ClientForm 获取字符串或空字符串并转换为 DATETIME,以便 Cycle Orm 可以处理它。
  4. Yii3 的 ClientService 使用 Cycle 的 Client Entity 的 getter 和 setter 方法和注释保存日期。
  5. Php 7.4.9
  6. 类型化的属性。以前 PHP 允许在类 public $var; 下声明此变量,现在在 public 和 $var 之间插入类型化属性,即 public ?string $var = '' 排除其他类型。类型前的问号允许空值输入。所以只有两种选择。
  7. Understanding mySql's '0000-00-00' for non date input.
  8. Download fork of https://github.com/yiisoft/yii-demo

dateHelper.PHP(改编自 Invoiceplane)

/**
  * @return string|null
*/
public function date_from_MysqL($date,$s)
{
       //if prevIoUs input was not a date MysqL would have input '0000-00-00' 
       if ($date <> '0000-00-00') {
            //CYCLE converts all dates to DateTimeImmutable
             $date = DateTime::createFromImmutable($date);
            //$date = DateTime::createFromFormat('Y-m-d',$date);
            //eg. $date->format('Ymd') 
            return $date->format($s->setting('date_format'));
        }
        return $date;
    }
    return '';
}

__form.PHP 标题

 <div class="mb-3 form-group has-Feedback">
        <label form-label for="client_birthdate"><?= $s->trans('birthdate'); ?></label>
       <?PHP
            $bdate = $body['client_birthdate'] ?? null;
            if ($bdate && $bdate != "0000-00-00") {
                //use the DateHelper
                $datehelper = new DateHelper();
                $bdate = $datehelper->date_from_MysqL($bdate,false,$s);
            } else {
                $bdate = null;
            }
        ?>        
        <div class="input-group">
            <input type="text" name="client_birthdate" id="client_birthdate" placeholder="1900/12/01"
                   class="form-control data-datepicker"
                   value="<?= Html::encode($bdate); ?>">
            <span class="input-group-addon">
            <i class="fa fa-calendar fa-fw"></i>
        </span>
        </div>        
    </div>  

Entity/Client.PHP

declare(strict_types=1);

namespace App\Invoice\Entity;
use \DateTime;

/**
 * @Entity(
 *     repository="App\Invoice\Client\ClientRepository",*     mapper="App\Invoice\Client\ClientMapper",*     constrain="App\Invoice\Client\Scope\activeScope"
 * )
 * @Table(
 *     indexes={
 *         @Index(columns={"client_active"}),*     }
 * )
 */
class Client
{
    /**
     * @Column(type="date",nullable=true)
     */
    private $client_birthdate = '';
    
    //CYCLE converts all date formats ie. DATE,DATETIME,to DateTimeImmutable so 
    work with DateTimeImmutable 
    
    public function __construct($client_birthdate = '')

    public function getClient_birthdate() : ?DateTimeImmutable  
    {
        if (isset($this->client_birthdate) && !empty($this->client_birthdate)){
            return $this->client_birthdate;            
        }
        if (empty($this->client_birthdate)){
            return $this->client_birthdate = null;
        }
    }    
    
    public function setClient_birthdate(?\DateTime $client_birthdate): void
    {
        $this->client_birthdate = $client_birthdate;
    }

Client/ClientForm.PHP

declare(strict_types=1);

namespace App\Invoice\Client;

use Yiisoft\Form\FormModel;
use Yiisoft\Validator\Rule\required;
use \DateTimeImmutable;
use \DateTime;

final class ClientForm extends FormModel {

private ?string $client_birthdate = null;

public function getClient_birthdate(): ?\DateTime
    {
        if (isset($this->client_birthdate) && !empty($this->client_birthdate)){
            return new DateTime($this->client_birthdate);            
        }
        if (empty($this->client_birthdate)){
            return $this->client_birthdate = null;
        } 
    }

客户/客户服务

<?PHP

declare(strict_types=1);

namespace App\Invoice\Client;

use App\Invoice\Entity\Client;
use App\User\User;

final class ClientService
{
private ClientRepository $repository;

    public function __construct(ClientRepository $repository)
    {
        $this->repository = $repository;;
    }

    public function saveClient(User $user,Client $model,ClientForm $form): void
    {
    $model->setClient_birthdate($form->getClient_birthdate());

解决方法

注意事项:

  1. 确保 _form 'id' 和 'name' 值,例如。 client_birthdate 对应于实体@column 和数据库表字段。即一致地使用字段名称 通过实体,注释。 ClientForm 的 getter 方法将从 __form 接收数据,它是一个字符串或空值。 getter 会将其转换为 DATETIME 或 null,以便 CYCLE ORM(螺旋框架)可以处理它。
  2. 确保在 Entity/Client.php 实例化区域中进行初始化,即。 BEFORE 构造和 IN 构造。

提示

  1. 函数上面的注释由 Cycle 的注释读取。
  2. use \DateTime; 在注解之前。不要忘记反斜杠以表明 DateTime 是不在当前命名空间中的 php 类。
  3. mySql 在数据库中输入 DATE,在下面的注释中包含“日期”。 IE。 * @Column(type="date",nullable=true) 否则 Cycle 将无法读取它。
  4. 我选择使用一个简单的无类型、可为空的字符串。

.../Entity/Client.php

   public function getClient_birthdate() : ?\DateTimeImmutable  

   public function setClient_birthdate(?\DateTime $client_birthdate): void

...src/Invoice/Entity/Client.php...

     /**
     * @Column(type="date",nullable=true)
     */
    private $client_birthdate = '';   
  1. 从coalface __form 接受的值使用字符串,因此用字符串初始化ClientForm.php 的private ?string $client_birthdate = null 不是 DateTime 函数。
  2. ?\DateTime 之前的问号允许空值。在函数声明中一致使用,如下所示。

...src/Invoice/Client/ClientForm.php

    public function getClient_birthdate(): ?\DateTime
    {
        if (isset($this->client_birthdate) && !empty($this->client_birthdate)){
            return new DateTime($this->client_birthdate);            
        }
        if (empty($this->client_birthdate)){
            return $this->client_birthdate = null;
        } 
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?