如何解决如何使用 Yii3 及其 Cycle-ORM 集成设置出生日期?
前提
- MysqL 数据库 client_birthdate 字段设置为 DATE,如果用户不输入日期则可为空。
- 用户在 __form 文本框中输入客户的生日作为 MysqL 格式的字符串,无论是否为 YYYY/MM/DD。
- Yii3 的 ClientForm 获取字符串或空字符串并转换为 DATETIME,以便 Cycle Orm 可以处理它。
- Yii3 的 ClientService 使用 Cycle 的 Client Entity 的 getter 和 setter 方法和注释保存日期。
- Php 7.4.9
- 类型化的属性。以前 PHP 允许在类
public $var;
下声明此变量,现在在 public 和 $var 之间插入类型化属性,即public ?string $var = ''
排除其他类型。类型前的问号允许空值输入。所以只有两种选择。 - Understanding mySql's '0000-00-00' for non date input.
- 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 '';
}
<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());
解决方法
注意事项:
- 确保 _form 'id' 和 'name' 值,例如。 client_birthdate 对应于实体@column 和数据库表字段。即一致地使用字段名称 通过实体,注释。 ClientForm 的 getter 方法将从 __form 接收数据,它是一个字符串或空值。 getter 会将其转换为 DATETIME 或 null,以便 CYCLE ORM(螺旋框架)可以处理它。
- 确保在 Entity/Client.php 实例化区域中进行初始化,即。 BEFORE 构造和 IN 构造。
提示
- 函数上面的注释由 Cycle 的注释读取。
-
use \DateTime;
在注解之前。不要忘记反斜杠以表明 DateTime 是不在当前命名空间中的 php 类。 - mySql 在数据库中输入 DATE,在下面的注释中包含“日期”。 IE。
* @Column(type="date",nullable=true)
否则 Cycle 将无法读取它。 - 我选择使用一个简单的无类型、可为空的字符串。
.../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 = '';
- 从coalface __form 接受的值使用字符串,因此用字符串初始化ClientForm.php 的
private ?string $client_birthdate = null
不是 DateTime 函数。 - ?\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 举报,一经查实,本站将立刻删除。