Doctrine 데이터 형식 'datetime', 'time'및 'date'의 기본값 인 PHP의 기본 DateTime 대신 Zend_Date를 사용하도록 기본 데이터 형식을 재정의 할 수 있습니다.
먼저 응용 프로그램 부트 스트랩 파일에서 Doctrine EntityManager를 인스턴스화하기 전에 다음을 추가하십시오. 이 코드는 다른 교리 코드 앞에 와야한다 :
Doctrine\DBAL\Types\Type::overrideType('datetime', 'yournamespace\types\DateTimeType');
Doctrine\DBAL\Types\Type::overrideType('date', 'yournamespace\types\DateType');
Doctrine\DBAL\Types\Type::overrideType('time', 'yournamespace\types\Time');
지금 당신은 단순히 3 개 클래스를 구현해야합니다. 이를 달성하기 위해 해당 Doctrine 클래스를 확장하는 것이 가장 쉽습니다. 코드는 실제로 모든 3 개의 클래스에서 동일합니다. 유일한 차이점은 확장하는 클래스와 클래스 이름입니다.
namespace yournamespace\type;
use Doctrine\DBAL\Types\DateTimeType as DoctrineDateTimeType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Override 'datetime' type in Doctrine to use Zend_Date
*/
class DateTimeType extends DoctrineDateTimeType
{
/**
* Convert from db to Zend_Date
*
* @param string $value
* @param AbstractPlatform $platform
* @return \Zend_Date|null
*/
public function convertToPhpValue($value, AbstractPlatform $platform)
{
if (is_null($value)) {
return null;
}
\Zend_Date::setOptions(array('format_type' => 'php',));
$phpValue = new \Zend_Date($value, $platform->getDateTimeFormatString());
\Zend_Date::setOptions(array('format_type' => 'iso',));
return $phpValue;
}
/**
* Convert from Zend_Date to db
*
* @param string $value
* @param AbstractPlatform $platform
* @return string|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (is_null($value)) {
return null;
}
\Zend_Date::setOptions(array('format_type' => 'php',));
$dbValue = $value->toString($platform->getDateTimeFormatString());
\Zend_Date::setOptions(array('format_type' => 'iso',));
return $dbValue;
}
}
지금 당신은 여전히 교리 주석을 @Column (유형 = "날짜")를 사용할 수 있습니다 예를 들면 다음과 같습니다과 DateTimeType 클래스입니다. 데이터베이스에 저장할 때 "datetime"유형의 엔티티 속성을 Zend_Date 인스턴스에 저장할 수 있습니다. 또한 데이터베이스에서 엔티티를 가져올 때 "datetime"유형의 특성이 Zend_Dates가됩니다.
다음과 같이 백 슬래시를 사용해야합니다. new \ DateTime() ... 전역 네임 스페이스를 사용합니다. 코드가 네임 스페이스 인 경우에만 – PachinSV
;) – arnaud576875