如何使用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下面声明这个变量
public$var下面声明这个变量
现在在public和$var之间插入类型化属性,即public?字符串$var='
排除其他类型。类型前的问号允许空值输入。因此,只有两种选择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());
需要注意的事情:
use\DateTime代码>在注释之前。不要忘记使用反斜杠表示DateTime是不在当前命名空间中的php类
*@Column(type=“date”,nullable=true)
否则Cycle将无法读取它 public function getClient_birthdate() : ?\DateTimeImmutable
及
…src/Invoice/Entity/Client.php
/**
* @Column(type="date", nullable=true)
*/
private $client_birthdate = '';
private?字符串$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;
}
}
/**
* @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;
}
}