-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Business saves but if the Many entities in the one to Many relation i…
…s updated externally then the result is not predictable. Reconciliation attempt 1
- Loading branch information
Showing
6 changed files
with
568 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Rating; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\JoinColumn; | ||
use Doctrine\ORM\Mapping\JoinTable; | ||
use Doctrine\ORM\Mapping\ManyToMany; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
/** | ||
* @Entity | ||
* @Table(name="ratings_business") | ||
*/ | ||
class Business | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="string") | ||
* @GeneratedValue(strategy="NONE") | ||
*/ | ||
private string $id; | ||
|
||
/** | ||
* @psalm-var string|null | ||
* @Column(type="string", length=255) | ||
*/ | ||
private string $name; | ||
|
||
/** | ||
* @psalm-doc https://www.doctrine-project.org/projects/doctrine-orm/en/2.16/reference/association-mapping.html#one-to-many-unidirectional-with-join-table | ||
* @psalm-var Collection<Review> | ||
* @ManyToMany(targetEntity="Review", cascade={"all"}, orphanRemoval=true, fetch="EAGER") | ||
* @JoinTable(name="ratings_businesses_reviews", | ||
* joinColumns={@JoinColumn(name="business_id", referencedColumnName="id", onDelete="CASCADE")}, | ||
* inverseJoinColumns={@JoinColumn(name="review_id", referencedColumnName="id", unique=true, onDelete="CASCADE")} | ||
* ) | ||
*/ | ||
public $reviews; | ||
|
||
public function __construct(string $id, string $name, ?Reviews $reviews = null) | ||
{ | ||
$this->id = $id; | ||
$this->name = $name; | ||
if ($reviews === null) { | ||
$this->reviews = new ArrayCollection(); | ||
} else { | ||
$this->reviews = $reviews; | ||
} | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** @psalm-return Collection<int, Review> */ | ||
public function getReviews(): Collection | ||
{ | ||
return $this->reviews; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setReviews(Collection $reviews) | ||
{ | ||
$this->reviews = $reviews; | ||
|
||
return $this; | ||
} | ||
|
||
public static function loadMetadata(ClassMetadata $metadata): void | ||
{ | ||
$metadata->setPrimaryTable( | ||
['name' => 'rating_business'] | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Rating; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\JoinColumn; | ||
use Doctrine\ORM\Mapping\ManyToOne; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
/** | ||
* @Entity | ||
* @Table(name="ratings_review") | ||
*/ | ||
class Review | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="string") | ||
*/ | ||
private string $id; | ||
/** | ||
* @Column(type="string", length=50, nullable=true) | ||
*/ | ||
private string $text; | ||
/** | ||
* @Column(type="integer", nullable=false) | ||
*/ | ||
private int $rating; | ||
/** | ||
* why isnt , orphanRemoval=true allowed here | ||
* | ||
* @ManyToOne(targetEntity="User", cascade={"all"}) | ||
* @JoinColumn(name="user_id", referencedColumnName="id") | ||
*/ | ||
private User $user; | ||
|
||
/** | ||
* Forced by doctrine | ||
* | ||
* @var Business | ||
*/ | ||
private $business; | ||
|
||
public function __construct( | ||
string $id, | ||
string $text, | ||
int $rating, | ||
User $user | ||
) { | ||
$this->id = $id; | ||
$this->text = $text; | ||
$this->rating = $rating; | ||
$this->user = $user; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getText(): string | ||
{ | ||
return $this->text; | ||
} | ||
|
||
public function getRating(): int | ||
{ | ||
return $this->rating; | ||
} | ||
|
||
public function setText(string $text): void | ||
{ | ||
$this->text = $text; | ||
} | ||
|
||
public function setRating(int $rating): void | ||
{ | ||
$this->rating = $rating; | ||
} | ||
|
||
public function setBusiness(Business $business): void | ||
{ | ||
$this->business = $business; | ||
} | ||
|
||
public function getBusiness(): Business | ||
{ | ||
return $this->business; | ||
} | ||
|
||
public function getUser(): User | ||
{ | ||
return $this->user; | ||
} | ||
|
||
public function setUser(User $user): void | ||
{ | ||
$this->user = $user; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Rating; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
|
||
class Reviews extends ArrayCollection | ||
{ | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Rating; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
/** | ||
* @Entity | ||
* @Table(name="ratings_user") | ||
*/ | ||
class User | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="string") | ||
*/ | ||
private string $id; | ||
/** | ||
* @Column(type="string", length=255, nullable=false) | ||
*/ | ||
private string $name; | ||
|
||
public function __construct(string $id, string $name) | ||
{ | ||
$this->id = $id; | ||
$this->name = $name; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
Oops, something went wrong.