1 | <?php |
||
59 | abstract class Document extends DocumentEntity implements ActiveEntityInterface |
||
60 | { |
||
61 | /** |
||
62 | * Associated collection and database names, by default will be resolved based on a class name. |
||
63 | */ |
||
64 | const DATABASE = null; |
||
65 | const COLLECTION = null; |
||
66 | |||
67 | /** |
||
68 | * When set to true publicFields() method (and jsonSerialize) will replace '_id' property with |
||
69 | * 'id'. |
||
70 | */ |
||
71 | const HIDE_UNDERSCORE_ID = true; |
||
72 | |||
73 | /** |
||
74 | * Set of indexes to be created for associated collection. Use "@options" for additional |
||
75 | * index options. |
||
76 | * |
||
77 | * Example: |
||
78 | * const INDEXES = [ |
||
79 | * ['email' => 1, '@options' => ['unique' => true]], |
||
80 | * ['name' => 1] |
||
81 | * ]; |
||
82 | * |
||
83 | * @link http://php.net/manual/en/mongocollection.ensureindex.php |
||
84 | * @var array |
||
85 | */ |
||
86 | const INDEXES = []; |
||
87 | |||
88 | /** |
||
89 | * Documents must ALWAYS have _id field. |
||
90 | */ |
||
91 | const SCHEMA = [ |
||
92 | '_id' => ObjectID::class |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * _id is always nullable. |
||
97 | */ |
||
98 | const DEFAULTS = [ |
||
99 | '_id' => null |
||
100 | ]; |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function __construct(array $fields = [], ODMInterface $odm = null, array $schema = null) |
||
106 | { |
||
107 | parent::__construct($fields, $odm, $schema); |
||
108 | |||
109 | if (!$this->isLoaded()) { |
||
110 | //Automatically force solidState for newly created documents |
||
111 | $this->solidState(true); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function isLoaded(): bool |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function primaryKey() |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | * |
||
134 | * Check model setting HIDE_UNDERSCORE_ID in order to enable/disable automatic conversion of |
||
135 | * '_id' to 'id'. |
||
136 | */ |
||
137 | public function publicValue(): array |
||
138 | { |
||
139 | $public = parent::publicValue(); |
||
140 | if (static::HIDE_UNDERSCORE_ID) { |
||
141 | //Replace '_id' property with 'id' |
||
142 | unset($public['_id']); |
||
143 | $public = ['id' => (string)$this->primaryKey()] + $public; |
||
144 | } |
||
145 | |||
146 | return $public; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | * |
||
152 | * @event create(DocumentEvent) |
||
153 | * @event created(DocumentEvent) |
||
154 | * @event update(DocumentEvent) |
||
155 | * @event updated(DocumentEvent) |
||
156 | */ |
||
157 | public function save(): int |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | * |
||
195 | * @event delete(DocumentEvent) |
||
196 | * @event deleted(DocumentEvent) |
||
197 | */ |
||
198 | public function delete() |
||
214 | } |