Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
b24integrationbindtable.php
1<?php
3
5
22class B24IntegrationBindTable extends Main\Entity\DataManager
23{
24 public static function getTableName()
25 {
26 return 'b_sale_b24integration_bind';
27 }
28
29 public static function getMap()
30 {
31 return [
32 new Main\Entity\IntegerField('ID'),
33 new Main\Entity\IntegerField('SRC_ENTITY_TYPE_ID', [ 'primary' => true ]),
34 new Main\Entity\IntegerField('SRC_ENTITY_ID', [ 'primary' => true ]),
35 new Main\Entity\IntegerField('DST_ENTITY_TYPE_ID', [ 'primary' => true ]),
36 new Main\Entity\IntegerField('DST_ENTITY_ID', [ 'primary' => true ]),
37 new Main\Entity\DatetimeField('CREATED_TIME'),
38 new Main\Entity\DatetimeField('LAST_UPDATED_TIME')
39 ];
40 }
41
42 public static function upsert(array $data)
43 {
44 $result = new Main\Entity\AddResult();
45
46 $srcEntityTypeID = isset($data['SRC_ENTITY_TYPE_ID']) ? (int)$data['SRC_ENTITY_TYPE_ID'] : \CCrmOwnerType::Undefined;
47 $srcEntityID = isset($data['SRC_ENTITY_ID']) ? (int)$data['SRC_ENTITY_ID'] : 0;
48
49 $dstEntityTypeID = isset($data['DST_ENTITY_TYPE_ID']) ? (int)$data['DST_ENTITY_TYPE_ID'] : \CCrmOwnerType::Undefined;
50 $dstEntityID = isset($data['DST_ENTITY_ID']) ? (int)$data['DST_ENTITY_ID'] : 0;
51
52 $now = Main\Type\DateTime::createFromTimestamp(time() + \CTimeZone::GetOffset());
53
54 $insertFields = [
55 'SRC_ENTITY_TYPE_ID' => $srcEntityTypeID,
56 'SRC_ENTITY_ID' => $srcEntityID,
57 'DST_ENTITY_TYPE_ID' => $dstEntityTypeID,
58 'DST_ENTITY_ID' => $dstEntityID,
59 'CREATED_TIME' => $now,
60 'LAST_UPDATED_TIME' => $now
61 ];
62
63 $updateFields = [
64 'LAST_UPDATED_TIME' => $now,
65 'SRC_ENTITY_TYPE_ID' => $srcEntityTypeID,
66 'SRC_ENTITY_ID' => $srcEntityID,
67 'DST_ENTITY_TYPE_ID' => $dstEntityTypeID,
68 'DST_ENTITY_ID' => $dstEntityID
69 ];
70
71 $connection = Main\Application::getConnection();
72 $queries = $connection->getSqlHelper()->prepareMerge(
73 static::getTableName(),
74 [
75 'SRC_ENTITY_TYPE_ID',
76 'SRC_ENTITY_ID',
77 'DST_ENTITY_TYPE_ID',
78 'DST_ENTITY_ID'
79 ],
80 $insertFields,
81 $updateFields
82 );
83
84 foreach($queries as $query)
85 {
86 $connection->queryExecute($query);
87 }
88
89 $result->setId(
90 $connection->getInsertedId());
91
92 return $result;
93 }
94}