1
0
Fork 0
mirror of synced 2025-04-20 01:21:01 +00:00

proper add & save methods for AbstractSerializableModel

This commit is contained in:
Pavel 2020-08-01 10:50:42 +03:00
parent c7a06d9f60
commit 97e1cf0070
2 changed files with 96 additions and 6 deletions

View file

@ -47,11 +47,11 @@ abstract class AbstractSerializableModel
abstract public function isDeleteStatic(): bool;
/**
* Tries to save object via base class
* Tries to add object via base class
*
* @return \Bitrix\Main\ORM\Data\Result
*/
public function save(): Result
public function add(): Result
{
$result = null;
$data = $this->serialize();
@ -82,6 +82,48 @@ abstract class AbstractSerializableModel
return $this->constructResult($result);
}
/**
* Tries to save object via base class
*
* @return \Bitrix\Main\ORM\Data\Result
*/
public function save(): Result
{
$primary = $this->getPrimaryKeyData();
if (empty($primary)) {
return $this->add();
}
$result = null;
$data = $this->serialize();
$baseClass = $this->getBaseClass();
if ($this->isSaveStatic()) {
if (method_exists($baseClass, 'Update')) {
$result = call_user_func($baseClass . '::Update', $primary, $data);
} elseif (method_exists($baseClass, 'update')) {
$result = call_user_func($baseClass . '::update', $primary, $data);
}
} else {
$instance = new $baseClass();
if (method_exists($instance, 'Update')) {
$result = $instance->Update($primary, $data);
} elseif (method_exists($instance, 'update')) {
$result = $instance->update($primary, $data);
}
}
if (null === $result) {
throw new \RuntimeException(
"Neither Add(\$data) nor add(\$data) is exist in the base class or it's instance"
);
}
return $this->constructResult($result);
}
/**
* Tries to delete object via base class
*
@ -129,6 +171,10 @@ abstract class AbstractSerializableModel
$newResult->addError(new Error('No rows were affected.'));
}
if (is_int($result) && $result > 0) {
$this->setPrimaryKeyData($result);
}
return $newResult;
}
@ -148,6 +194,24 @@ abstract class AbstractSerializableModel
}
}
/**
* Tries to set primary key data
*
* @param mixed $primaryData
*
* @return mixed
*/
private function setPrimaryKeyData($primaryData)
{
if (method_exists($this, 'setId')) {
return $this->setId($primaryData);
} elseif (method_exists($this, 'setPrimary')) {
return $this->setPrimary($primaryData);
} else {
throw new \RuntimeException('AbstractSerializableModel child should implement setId or setPrimary');
}
}
/**
* Serializes current model
*

View file

@ -11,7 +11,6 @@
*/
namespace Intaro\RetailCrm\Model\Bitrix;
use Bitrix\Main\ORM\Data\Result;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
@ -21,6 +20,14 @@ use Intaro\RetailCrm\Component\Json\Mapping;
*/
class BuyerProfile extends AbstractSerializableModel
{
/**
* @var int
*
* @Mapping\Type("int")
* @Mapping\SerializedName("ID")
*/
protected $id;
/**
* @var string
*
@ -45,6 +52,25 @@ class BuyerProfile extends AbstractSerializableModel
*/
protected $personTypeId;
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int $id
*
* @return BuyerProfile
*/
public function setId(int $id): BuyerProfile
{
$this->id = $id;
return $this;
}
/**
* @return string
*/
@ -58,7 +84,7 @@ class BuyerProfile extends AbstractSerializableModel
*
* @return BuyerProfile
*/
public function setName(string $name): ?BuyerProfile
public function setName(string $name): BuyerProfile
{
$this->name = $name;
return $this;
@ -77,7 +103,7 @@ class BuyerProfile extends AbstractSerializableModel
*
* @return BuyerProfile
*/
public function setUserId(string $userId): ?BuyerProfile
public function setUserId(string $userId): BuyerProfile
{
$this->userId = $userId;
return $this;
@ -96,7 +122,7 @@ class BuyerProfile extends AbstractSerializableModel
*
* @return BuyerProfile
*/
public function setPersonTypeId(string $personTypeId): ?BuyerProfile
public function setPersonTypeId(string $personTypeId): BuyerProfile
{
$this->personTypeId = $personTypeId;
return $this;