Php 有没有办法将多个字段映射到一个属性中?
我试图用设计糟糕的PostgreSQL数据库来使用Doctrine。有些表的日期和时间字段应该是时间戳字段Php 有没有办法将多个字段映射到一个属性中?,php,orm,doctrine-orm,Php,Orm,Doctrine Orm,我试图用设计糟糕的PostgreSQL数据库来使用Doctrine。有些表的日期和时间字段应该是时间戳字段 是否可以将一对日期和时间字段映射到我的类的DateTime属性中?您还可以创建一个视图,在该视图中,这些字段合并为一个。然后从模型类中使用视图而不是实际的表。您还可以创建一个视图,在该视图中,这些字段组合成一个视图。然后从您的模型类使用视图而不是实际的表。您可以创建一个方法getTimestamp(),该方法将返回一个new\DateTime($this->getDate()-…>form
是否可以将一对日期和时间字段映射到我的类的DateTime属性中?您还可以创建一个视图,在该视图中,这些字段合并为一个。然后从模型类中使用视图而不是实际的表。您还可以创建一个视图,在该视图中,这些字段组合成一个视图。然后从您的模型类使用视图而不是实际的表。您可以创建一个方法
getTimestamp()
,该方法将返回一个new\DateTime($this->getDate()-…>format('Y-m-d')。$this->getTime()->format('H:i:s'))
对象,并创建一个setTimestamp(\DateTime$DateTime$date)
方法,通过分别获取日期和时间来设置这两个值
public function setDate(\DateTime $date)
{
$this->date = $date;
}
public function getTime()
{
return $date;
}
public function setTime(\DateTime $date)
{
$this->date = $date;
}
public function getTime()
{
return $date;
}
public function getTimeStamp() {
return new \DateTime($this->getDate()->format('Y-m-d') . ' ' . $this->getTime()->format('H:i:s'));
}
public function setTimeStamp(\DateTime $timestamp) {
$this->setDate(new \DateTime($timestamp->format('Y-m-d'));
$this->setTime(new \DateTime($timestamp->format('H:i:s'));
}
或者类似的。然后,您可以在代码中专门使用get/setTimestamp()
DQL
将时间戳作为单个参数传递,然后绑定两个参数,如下所示:
public function getSomething(\DateTime $timestamp, $someparam) {
$dql = "SELECT ... FROM ... WHERE date = :date AND time = :time"
// Create query and params binding
// param('date', $timestamp->format('Y-m-d'));
// param('time', $timestamp->format('H:i:s'));
return $result;
}
您可能需要将参数值包装在new\DateTime()
中,因为条令需要一个DateTime实例作为日期、时间和时间戳
这为您的代码提供了一个简单的接口来支持,但允许对数据库模式进行抽象。您可以创建一个方法
getTimestamp()
,该方法将返回一个new\DateTime($This->getDate()-…>format('Y-m-d')。$This->getTime()->format('H:i:s'))
对象并创建一个setTimestamp(\DateTime$date)
方法,该方法将通过分别获取日期和时间来设置这两个值
public function setDate(\DateTime $date)
{
$this->date = $date;
}
public function getTime()
{
return $date;
}
public function setTime(\DateTime $date)
{
$this->date = $date;
}
public function getTime()
{
return $date;
}
public function getTimeStamp() {
return new \DateTime($this->getDate()->format('Y-m-d') . ' ' . $this->getTime()->format('H:i:s'));
}
public function setTimeStamp(\DateTime $timestamp) {
$this->setDate(new \DateTime($timestamp->format('Y-m-d'));
$this->setTime(new \DateTime($timestamp->format('H:i:s'));
}
然后,您可以在代码中专门使用get/setTimestamp()
DQL
将时间戳作为单个参数传递,然后绑定两个参数,如下所示:
public function getSomething(\DateTime $timestamp, $someparam) {
$dql = "SELECT ... FROM ... WHERE date = :date AND time = :time"
// Create query and params binding
// param('date', $timestamp->format('Y-m-d'));
// param('time', $timestamp->format('H:i:s'));
return $result;
}
您可能需要将参数值包装在new\DateTime()
中,因为条令需要一个DateTime实例作为日期、时间和时间戳
这为您的代码提供了一个简单的接口来支持,但允许对数据库模式进行抽象。我考虑过这一点,但需要处理upserts的视图规则看起来比纯PHP解决方案更复杂。我考虑过这一点,但需要处理upserts的视图规则看起来比PHP解决方案更复杂很好的解决方案,但是我的DQL查询应该如何编写?很好的解决方案,但是我的DQL查询应该如何编写?