ability to create filled AbstractSerializableModel by passing primary key value
This commit is contained in:
parent
1fa996764f
commit
bc2a54d855
3 changed files with 84 additions and 0 deletions
|
@ -12,6 +12,7 @@
|
|||
namespace Intaro\RetailCrm\Model\Bitrix;
|
||||
|
||||
use Bitrix\Main\Type\DateTime;
|
||||
use Intaro\RetailCrm\Component\Json\Deserializer;
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
use Intaro\RetailCrm\Component\Json\Serializer;
|
||||
use Bitrix\Main\ORM\Data\Result;
|
||||
|
@ -46,6 +47,45 @@ abstract class AbstractSerializableModel
|
|||
*/
|
||||
abstract public function isDeleteStatic(): bool;
|
||||
|
||||
/**
|
||||
* Should return data by provided primary key
|
||||
*
|
||||
* @param mixed $primary
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public static function getDataArrayByPrimary($primary): array;
|
||||
|
||||
/**
|
||||
* AbstractSerializableModel constructor.
|
||||
*
|
||||
* @param mixed $primary
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function __construct($primary = null)
|
||||
{
|
||||
if ($primary !== null) {
|
||||
$data = static::getDataArrayByPrimary($primary);
|
||||
|
||||
if (!empty($data)) {
|
||||
$thisClassName = get_class($this);
|
||||
$instance = Deserializer::deserializeArray($data, $thisClassName);
|
||||
|
||||
if ($instance instanceof $thisClassName) {
|
||||
$instanceReflection = new \ReflectionClass($instance);
|
||||
|
||||
foreach ($instanceReflection->getProperties() as $property) {
|
||||
$thisProperty = new \ReflectionProperty($thisClassName, $property->getName());
|
||||
$property->setAccessible(true);
|
||||
$thisProperty->setAccessible(true);
|
||||
$thisProperty->setValue($this, $property->getValue($instance));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to add object via base class
|
||||
*
|
||||
|
|
|
@ -151,4 +151,28 @@ class BuyerProfile extends AbstractSerializableModel
|
|||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getDataArrayByPrimary($primary): array
|
||||
{
|
||||
$result = \CSaleOrderUserProps::GetList([], ['ID' => $primary]);
|
||||
|
||||
if ($result) {
|
||||
$data = $result->Fetch();
|
||||
|
||||
if (!$data) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (isset($data['ID']) && $data['ID'] != $primary) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1694,4 +1694,24 @@ class User extends AbstractSerializableModel
|
|||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getDataArrayByPrimary($primary): array
|
||||
{
|
||||
$result = \CUser::GetByID($primary);
|
||||
|
||||
if ($result) {
|
||||
$data = $result->Fetch();
|
||||
|
||||
if (!$data) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue