-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Flori
committed
Oct 8, 2024
1 parent
e60b368
commit 1bad27d
Showing
3 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,3 +138,29 @@ class User extends ORM\Entity | |
|
||
**\*** The methods have to follow `OPT_NAMING_SCHEME_METHODS`. So if you use `set_some_var` you should set the naming | ||
scheme to `snake_lower`. | ||
|
||
### Creating entities and working in transactions | ||
|
||
To create a new entity you can use the `new` keyword. This will create a new entity with the default values from the | ||
entity definition. You can then modify the entity and save it to the database. | ||
|
||
```php | ||
$user = new User(); | ||
$user->email = '[email protected]'; | ||
$user->save(); | ||
``` | ||
|
||
To work in a transaction you can use the `EntityManager::beginTransaction()` method. This will start a transaction or | ||
create a savepoint if a transaction is already started. You can then commit or rollback the transaction. | ||
|
||
```php | ||
$entityManager->beginTransaction(); | ||
$user = new User(); | ||
$user->email = '[email protected]'; | ||
$user->save(); | ||
$activation = new UserActivation(); | ||
$activation->setRelated('user', $user); | ||
$activation->code($code = bin2hex(random_bytes(8))); | ||
$activation->save(); | ||
$entityManager->commit(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters