orm usage

This commit is contained in:
Yuri Kuznetsov
2020-07-24 15:33:36 +03:00
parent 8890aa6618
commit 246d42469f
4 changed files with 77 additions and 59 deletions
+36 -30
View File
@@ -31,6 +31,8 @@ namespace Espo\Repositories;
use Espo\ORM\Entity;
use Espo\Entities\EmailAddress as EmailAddressEntity;
use Espo\Core\Di;
class EmailAddress extends \Espo\Core\Repositories\Database implements
@@ -86,45 +88,49 @@ class EmailAddress extends \Espo\Core\Repositories\Database implements
return $ids;
}
public function getEmailAddressData(Entity $entity)
public function getEmailAddressData(Entity $entity) : array
{
$data = [];
$dataList = [];
$pdo = $this->getEntityManager()->getPDO();
$sql = "
SELECT email_address.name, email_address.lower, email_address.invalid, email_address.opt_out AS optOut, entity_email_address.primary
FROM entity_email_address
JOIN email_address ON email_address.id = entity_email_address.email_address_id AND email_address.deleted = 0
WHERE
entity_email_address.entity_id = ".$pdo->quote($entity->id)." AND
entity_email_address.entity_type = ".$pdo->quote($entity->getEntityType())." AND
entity_email_address.deleted = 0
ORDER BY entity_email_address.primary DESC
";
$sth = $pdo->prepare($sql);
$sth->execute();
if ($rows = $sth->fetchAll()) {
foreach ($rows as $row) {
$obj = new \StdClass();
$obj->emailAddress = $row['name'];
$obj->lower = $row['lower'];
$obj->primary = ($row['primary'] == '1') ? true : false;
$obj->optOut = ($row['optOut'] == '1') ? true : false;
$obj->invalid = ($row['invalid'] == '1') ? true : false;
$data[] = $obj;
}
$emailAddressList = $this
->select(['name', 'lower', 'invalid', 'optOut', ['ee.primary', 'primary']])
->join([[
'EntityEmailAddress',
'ee',
[
'ee.emailAddressId:' => 'id',
]
]])
->where([
'ee.entityId' => $entity->id,
'ee.entityType' => $entity->getEntityType(),
'ee.deleted' => false,
])
->order('ee.primary', true)
->find();
foreach ($emailAddressList as $emailAddress) {
$item = (object) [
'emailAddress' => $emailAddress->get('name'),
'lower' => $emailAddress->get('lower'),
'primary' => $emailAddress->get('primary'),
'optOut' => $emailAddress->get('optOut'),
'invalid' => $emailAddress->get('invalid'),
];
$dataList[] = $item;
}
return $data;
return $dataList;
}
public function getByAddress($address)
public function getByAddress(string $address) : ?EmailAddressEntity
{
return $this->where(array('lower' => strtolower($address)))->findOne();
return $this->where(['lower' => strtolower($address)])->findOne();
}
public function getEntityListByAddressId($emailAddressId, $exceptionEntity = null, $entityType = null, $onlyName = false)
{
public function getEntityListByAddressId(
string $emailAddressId, ?Entity $exceptionEntity = null, ?string $entityType = null, bool $onlyName = false
) {
$entityList = [];
$pdo = $this->getEntityManager()->getPDO();
+33 -29
View File
@@ -31,6 +31,8 @@ namespace Espo\Repositories;
use Espo\ORM\Entity;
use Espo\Entities\PhoneNumber as PhoneNumberEntity;
use Espo\Core\Di;
class PhoneNumber extends \Espo\Core\Repositories\Database implements
@@ -84,45 +86,47 @@ class PhoneNumber extends \Espo\Core\Repositories\Database implements
return $ids;
}
public function getPhoneNumberData(Entity $entity)
public function getPhoneNumberData(Entity $entity) : array
{
$data = array();
$dataList = [];
$pdo = $this->getEntityManager()->getPDO();
$sql = "
SELECT phone_number.name, phone_number.type, entity_phone_number.primary, phone_number.opt_out AS optOut, phone_number.invalid
FROM entity_phone_number
JOIN phone_number ON phone_number.id = entity_phone_number.phone_number_id AND phone_number.deleted = 0
WHERE
entity_phone_number.entity_id = ".$pdo->quote($entity->id)." AND
entity_phone_number.entity_type = ".$pdo->quote($entity->getEntityType())." AND
entity_phone_number.deleted = 0
ORDER BY entity_phone_number.primary DESC
";
$sth = $pdo->prepare($sql);
$sth->execute();
if ($rows = $sth->fetchAll()) {
foreach ($rows as $row) {
$obj = new \StdClass();
$obj->phoneNumber = $row['name'];
$obj->primary = ($row['primary'] == '1') ? true : false;
$obj->type = $row['type'];
$obj->optOut = ($row['optOut'] == '1') ? true : false;
$obj->invalid = ($row['invalid'] == '1') ? true : false;
$numberList = $this
->select(['name', 'type', 'invalid', 'optOut', ['en.primary', 'primary']])
->join([[
'EntityPhoneNumber',
'en',
[
'en.phoneNumberId:' => 'id',
]
]])
->where([
'en.entityId' => $entity->id,
'en.entityType' => $entity->getEntityType(),
'en.deleted' => false,
])
->order('en.primary', true)
->find();
$data[] = $obj;
}
foreach ($numberList as $number) {
$item = (object) [
'phoneNumber' => $number->get('name'),
'type' => $number->get('type'),
'primary' => $number->get('primary'),
'optOut' => $number->get('optOut'),
'invalid' => $number->get('invalid'),
];
$dataList[] = $item;
}
return $data;
return $dataList;
}
public function getByNumber($number)
public function getByNumber(string $number) : ?PhoneNumberEntity
{
return $this->where(array('name' => $number))->findOne();
return $this->where(['name' => $number])->findOne();
}
public function getEntityListByPhoneNumberId($phoneNumberId, $exceptionEntity = null)
public function getEntityListByPhoneNumberId(string $phoneNumberId, ?Entity $exceptionEntity = null)
{
$entityList = [];
@@ -14,6 +14,10 @@
},
"optOut": {
"type": "bool"
},
"primary": {
"type": "bool",
"notStorable": true
}
},
"links": {
@@ -19,6 +19,10 @@
},
"optOut": {
"type": "bool"
},
"primary": {
"type": "bool",
"notStorable": true
}
},
"links": {