| @@ 16-101 (lines=86) @@ | ||
| 13 | * |
|
| 14 | * class for handling social logins |
|
| 15 | */ |
|
| 16 | class PersonModel extends BaseModel implements IModel |
|
| 17 | { |
|
| 18 | ||
| 19 | /** @var string */ |
|
| 20 | protected $table = 'kk_persons'; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @param Context $database |
|
| 24 | */ |
|
| 25 | public function __construct(Context $database) |
|
| 26 | { |
|
| 27 | $this->setDatabase($database); |
|
| 28 | } |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @return ActiveRow |
|
| 32 | */ |
|
| 33 | public function all() |
|
| 34 | { |
|
| 35 | return; |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @param int $id |
|
| 40 | * @return PersonEntity |
|
| 41 | */ |
|
| 42 | public function find($id): PersonEntity |
|
| 43 | { |
|
| 44 | $block = parent::find($id); |
|
| 45 | return $this->hydrate($block); |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * @param IEntity $entity |
|
| 50 | * @return bool|mixed |
|
| 51 | * @throws Exception |
|
| 52 | */ |
|
| 53 | public function save(IEntity $entity) |
|
| 54 | { |
|
| 55 | if ($entity->getId() === null) { |
|
| 56 | $values = $entity->toArray(); |
|
| 57 | ||
| 58 | $result = $this->create($values); |
|
| 59 | //$result = $this->setIdentity($row, $row->id); |
|
| 60 | } else { |
|
| 61 | $values = $entity->toArray(); |
|
| 62 | $result = $this->update($entity->getId(), $values); |
|
| 63 | } |
|
| 64 | ||
| 65 | return $result; |
|
| 66 | } |
|
| 67 | ||
| 68 | /** |
|
| 69 | * @param $values |
|
| 70 | * @return PersonEntity |
|
| 71 | */ |
|
| 72 | public function hydrate($values): PersonEntity |
|
| 73 | { |
|
| 74 | $entity = new PersonEntity(); |
|
| 75 | //$this->setIdentity($entity, $values->id); |
|
| 76 | ||
| 77 | // unset($values['id']); |
|
| 78 | foreach ($values as $property => $value) { |
|
| 79 | $entity->$property = $value; |
|
| 80 | } |
|
| 81 | ||
| 82 | return $entity; |
|
| 83 | } |
|
| 84 | ||
| 85 | ||
| 86 | /** |
|
| 87 | * @param $item |
|
| 88 | * @param $id |
|
| 89 | * @return mixed |
|
| 90 | */ |
|
| 91 | private function setIdentity($item, $id) |
|
| 92 | { |
|
| 93 | $ref = new ClassType($item); |
|
| 94 | $idProperty = $ref->getProperty('data'); |
|
| 95 | $idProperty->setAccessible(true); |
|
| 96 | $idProperty->setValue($item, $id); |
|
| 97 | ||
| 98 | return $item; |
|
| 99 | } |
|
| 100 | ||
| 101 | } |
|
| 102 | ||
| @@ 16-102 (lines=87) @@ | ||
| 13 | * |
|
| 14 | * class for handling social logins |
|
| 15 | */ |
|
| 16 | class UserModel extends BaseModel implements IModel |
|
| 17 | { |
|
| 18 | ||
| 19 | /** @var string */ |
|
| 20 | protected $table = 'kk_users'; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @param Context $database |
|
| 24 | */ |
|
| 25 | public function __construct(Context $database) |
|
| 26 | { |
|
| 27 | $this->setDatabase($database); |
|
| 28 | } |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @return ActiveRow |
|
| 32 | */ |
|
| 33 | public function all() |
|
| 34 | { |
|
| 35 | return; |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @param int $id |
|
| 40 | * @return UserEntity |
|
| 41 | */ |
|
| 42 | public function find($id): UserEntity |
|
| 43 | { |
|
| 44 | $block = parent::find($id); |
|
| 45 | return $this->hydrate($block); |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * @param IEntity $entity |
|
| 50 | * @return bool|mixed |
|
| 51 | * @throws Exception |
|
| 52 | */ |
|
| 53 | public function save(IEntity $entity) |
|
| 54 | { |
|
| 55 | if ($entity->getId() === null) { |
|
| 56 | $values = $entity->toArray(); |
|
| 57 | ||
| 58 | $result = $this->create($values); |
|
| 59 | //$result = $this->setIdentity($row, $row->id); |
|
| 60 | } else { |
|
| 61 | $values = $entity->toArray(); |
|
| 62 | $result = $this->update($entity->getId(), $values); |
|
| 63 | } |
|
| 64 | ||
| 65 | return $result; |
|
| 66 | } |
|
| 67 | ||
| 68 | /** |
|
| 69 | * @param $values |
|
| 70 | * @return UserEntity |
|
| 71 | */ |
|
| 72 | public function hydrate($values): UserEntity |
|
| 73 | { |
|
| 74 | $entity = new UserEntity(); |
|
| 75 | //$this->setIdentity($entity, $values->id); |
|
| 76 | ||
| 77 | // unset($values['id']); |
|
| 78 | foreach ($values as $property => $value) { |
|
| 79 | $entity->$property = $value; |
|
| 80 | } |
|
| 81 | ||
| 82 | return $entity; |
|
| 83 | } |
|
| 84 | ||
| 85 | ||
| 86 | /** |
|
| 87 | * @param $item |
|
| 88 | * @param $id |
|
| 89 | * @return mixed |
|
| 90 | */ |
|
| 91 | private function setIdentity($item, $id) |
|
| 92 | { |
|
| 93 | $ref = new ClassType($item); |
|
| 94 | $idProperty = $ref->getProperty('data'); |
|
| 95 | //dd($idProperty->id); |
|
| 96 | $idProperty->setAccessible(true); |
|
| 97 | $idProperty->setValue($item, $id); |
|
| 98 | dd($item); |
|
| 99 | return $item; |
|
| 100 | } |
|
| 101 | ||
| 102 | } |
|
| 103 | ||