92 lines
2.4 KiB
PHP
92 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Bx\Nullable\ORM;
|
|
|
|
use Bitrix\Main\ORM\Fields\DatetimeField;
|
|
use Bitrix\Main\Type\DateTime;
|
|
use Exception;
|
|
use Wt\Core\ORM\ADataManager;
|
|
|
|
class NullableTable extends ADataManager
|
|
{
|
|
|
|
public static function getTitle()
|
|
{
|
|
return 'Nullable Title';
|
|
}
|
|
|
|
public static function getRowTitle()
|
|
{
|
|
return 'Nullable Row Title';
|
|
}
|
|
|
|
public static function getRowDescription()
|
|
{
|
|
return 'Nullable Row Description';
|
|
}
|
|
|
|
public static function getTableName()
|
|
{
|
|
return 'bx_nullable_table';
|
|
}
|
|
|
|
public static function getMap()
|
|
{
|
|
return [
|
|
'ID' => new \Bitrix\Main\Entity\IntegerField('ID', [
|
|
'primary' => true,
|
|
'autocomplete' => true,
|
|
'title' => 'Id',
|
|
]),
|
|
'CREATED_AT' => new \Bitrix\Main\Entity\DatetimeField('CREATED_AT', [
|
|
'title' => 'CreatedAt',
|
|
'user_edit' => false,
|
|
]),
|
|
'UPDATED_AT' => new \Bitrix\Main\Entity\DatetimeField('UPDATED_AT', [
|
|
'title' => 'UpdatedAt',
|
|
'user_edit' => false,
|
|
]),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param $primary
|
|
* @param array $data
|
|
* @return \Bitrix\Main\ORM\Data\UpdateResult
|
|
* @throws \Bitrix\Main\ObjectException
|
|
* @throws Exception
|
|
*/
|
|
public static function update($primary, array $data)
|
|
{
|
|
foreach (static::getEntity()->getFields() as $field){
|
|
if(is_a($field, DatetimeField::class)){
|
|
if(isset($data[$field->getName()]) && !is_a($data[$field->getName()], DateTime::class)) {
|
|
$data[$field->getName()] = new \Bitrix\Main\Type\DateTime($data[$field->getName()]);
|
|
}
|
|
}
|
|
}
|
|
|
|
$data = array_merge($data, [
|
|
'UPDATED_AT' => new \Bitrix\Main\Type\DateTime(), // date(CDatabase::DateFormatToPHP(FORMAT_DATETIME))
|
|
]);
|
|
unset($data['CREATED_AT']);
|
|
|
|
return parent::update($primary, $data);
|
|
}
|
|
|
|
/**
|
|
* @param array $fields
|
|
* @return \Bitrix\Main\ORM\Data\AddResult
|
|
* @throws \Bitrix\Main\ObjectException
|
|
* @throws Exception
|
|
*/
|
|
public static function add(array $fields)
|
|
{
|
|
$fields = array_merge($fields, [
|
|
'CREATED_AT' => new \Bitrix\Main\Type\DateTime(),
|
|
'UPDATED_AT' => new \Bitrix\Main\Type\DateTime(),
|
|
]);
|
|
|
|
return parent::add($fields);
|
|
}
|
|
} |