orm usage

This commit is contained in:
Yuri Kuznetsov
2020-07-31 15:11:06 +03:00
parent 1d6889879d
commit a37d380440
2 changed files with 82 additions and 77 deletions
+29 -25
View File
@@ -167,38 +167,42 @@ class PhoneNumber extends \Espo\Core\Repositories\Database implements
return $entityList;
}
public function getEntityByPhoneNumberId(string $phoneNumberId, ?string $entityType = null) : Entity
public function getEntityByPhoneNumberId(string $phoneNumberId, ?string $entityType = null) : ?Entity
{
$pdo = $this->getEntityManager()->getPDO();
$sql = "
SELECT entity_phone_number.entity_type AS 'entityType', entity_phone_number.entity_id AS 'entityId'
FROM entity_phone_number
WHERE
entity_phone_number.phone_number_id = ".$pdo->quote($phoneNumberId)." AND
entity_phone_number.deleted = 0
";
$where = [
'phoneNumberId' => $phoneNumberId,
];
if ($entityType) {
$sql .= "
AND entity_phone_number.entity_type = " . $pdo->quote($entityType) . "
";
$where[] = ['entityType' => $entityType];
}
$sql .= "
ORDER BY entity_phone_number.primary DESC, FIELD(entity_phone_number.entity_type, 'User', 'Contact', 'Lead', 'Account')
";
$itemList = $this->getEntityManager()->getRepository('EntityPhoneNumber')
->sth()
->select(['entityType', 'entityId'])
->where($where)
->limit(0, 20)
->order([
['primary', 'DESC'],
['LIST:entityType:User,Contact,Lead,Account'],
])
->find();
$sth = $pdo->prepare($sql);
$sth->execute();
while ($row = $sth->fetch()) {
if (!empty($row['entityType']) && !empty($row['entityId'])) {
if (!$this->getEntityManager()->hasRepository($row['entityType'])) {
return null;
}
$entity = $this->getEntityManager()->getEntity($row['entityType'], $row['entityId']);
if ($entity) {
return $entity;
foreach ($itemList as $item) {
$itemEntityType = $item->get('entityType');
$itemEntityId = $item->get('entityId');
if (!$itemEntityType || !$itemEntityId) continue;
if (!$this->getEntityManager()->hasRepository($itemEntityType)) continue;
$entity = $this->getEntityManager()->getEntity($itemEntityType, $itemEntityId);
if ($entity) {
if ($entity->getEntityType() === 'User') {
if (!$entity->get('isActive')) continue;
}
return $entity;
}
}