Meta.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Entity;
  3. class Meta extends ModelBase
  4. {
  5. private $entity;
  6. protected function install()
  7. {
  8. $dbPrefix = $this->getDbPrefix();
  9. $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}meta` (
  10. `entity_id` INTEGER(11) UNSIGNED NOT NULL,
  11. `entityType` VARCHAR(32) NOT NULL,
  12. `lang` VARCHAR(8) NULL,
  13. `key` VARCHAR(64) NOT NULL,
  14. `value` TEXT NULL,
  15. UNIQUE(`entity_id`, `entityType`, `lang`)
  16. )");
  17. if ($result === false)
  18. throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
  19. return true;
  20. }
  21. public function __get($key)
  22. {
  23. if ($key == "entity")
  24. return $this->entity;
  25. return parent::__get($key);
  26. }
  27. public function __set($key, $value)
  28. {
  29. if ($key == "entity")
  30. {
  31. parent::__set("entityId", $value->id);
  32. parent::__set("entityType", get_class($value));
  33. return $value;
  34. }
  35. else if ($key == "entityId" || $key == "entityType")
  36. throw new \Exception("Cannot access private field {$key}");
  37. return parent::__set($key, $value);
  38. }
  39. }