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

如何使用Yii3及其Cycle ORM集成设置生日?,orm,date-of-birth,Orm,Date Of Birth,先决条件 mySql数据库客户端\u birthdate字段设置为日期,如果用户未输入日期,则可为空 用户以mySql格式YYYY/MM/DD或非mySql格式的字符串形式在_表单文本框中输入客户的出生日期 Yii3的ClientForm获取字符串或空字符串,并将其转换为DATETIME,以便Cycle Orm可以处理它 Yii3的ClientService使用Cycle的客户端实体的getter和setter方法和注释保存日期 类型属性。以前php允许在类public$var下面声明这个变量

先决条件

  • mySql数据库客户端\u birthdate字段设置为日期,如果用户未输入日期,则可为空
  • 用户以mySql格式YYYY/MM/DD或非mySql格式的字符串形式在_表单文本框中输入客户的出生日期
  • Yii3的ClientForm获取字符串或空字符串,并将其转换为DATETIME,以便Cycle Orm可以处理它
  • Yii3的ClientService使用Cycle的客户端实体的getter和setter方法和注释保存日期
  • 类型属性。以前php允许在类
    public$var下面声明这个变量
    现在在public和$var之间插入类型化属性,即
    public?字符串$var='
    排除其他类型。类型前的问号允许空值输入。因此,只有两种选择
  • dateHelper.php(改编自Invoiceplane)

    Client/ClientForm.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;
        }
    
    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;
            } 
        }
    
    客户端/客户端服务

    /**
      * @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 '';
    }
    
    <?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());
    

    需要注意的事情:

  • 确保表单“id”和“name”值(如客户出生日期)对应于实体@列和数据库表字段。ie始终使用字段名 通过实体、注释。ClientForm的getter方法将从_表单接收数据,该表单为字符串或null。getter将把它转换为DATETIME或null,以便CYCLE-ORM(Spiral-Framework)可以处理它
  • 确保在Entity/Client.php实例化区域(即构建前和构建中)中进行初始化
  • 提示

  • 上述函数的注释由循环的注释读取
  • use\DateTime在注释之前。不要忘记使用反斜杠表示DateTime是不在当前命名空间中的php类
  • mySql在数据库中键入日期,并在下面的注释中包含“日期”。例如,
    *@Column(type=“date”,nullable=true)
    否则Cycle将无法读取它
  • 我选择使用一个简单的非类型化、可为空的字符串
  • …/Entity/Client.php

       public function getClient_birthdate() : ?\DateTimeImmutable  
    

    …src/Invoice/Entity/Client.php

         /**
         * @Column(type="date", nullable=true)
         */
        private $client_birthdate = '';   
    
  • 从coalface _表单接受的值使用字符串,因此使用字符串初始化ClientForm.php的
    private?字符串$client_birthdate=null
    不是日期时间函数
  • ?之前的问号\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;
            } 
        }
    
         /**
         * @Column(type="date", nullable=true)
         */
        private $client_birthdate = '';   
    
        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;
            } 
        }