Skip to content

Commit

Permalink
Business saves but if the Many entities in the one to Many relation i…
Browse files Browse the repository at this point in the history
…s updated externally then the result is not predictable. Reconciliation attempt 1
  • Loading branch information
bizmate committed Oct 2, 2023
1 parent e9537f4 commit 307b6d8
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/Doctrine/Tests/Models/Rating/Business.php
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']
);
}
}
104 changes: 104 additions & 0 deletions tests/Doctrine/Tests/Models/Rating/Review.php
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;
}
}
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/Models/Rating/Reviews.php
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
{
}
48 changes: 48 additions & 0 deletions tests/Doctrine/Tests/Models/Rating/User.php
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;
}
}
Loading

0 comments on commit 307b6d8

Please sign in to comment.