<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
// 追加
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\SaleType;
use App\Entity\ProductImage;
use App\Entity\Member;
use App\Entity\ProductTag;
use App\Entity\ProductStatus;
#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[ORM\Table(name: "dtb_product")]
// #[ORM\Entity(repositoryClass: "Eccube\Repository\ProductRepository")]
#[ORM\HasLifecycleCallbacks]
class Product
{
// private $_calc = false;
// private $stockFinds = [];
// private $stocks = [];
// private $stockUnlimiteds = [];
// private $price01 = [];
private $price02 = [];
// private $price01IncTaxs = [];
// private $price02IncTaxs = [];
// private $codes = [];
// private $classCategories1 = [];
// private $classCategories2 = [];
// private $className1;
// private $className2;
// #[ORM\Id]
// #[ORM\GeneratedValue(strategy: "IDENTITY")]
// #[ORM\Column(type: "integer", options: ["unsigned" => true])]
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToMany(targetEntity: ProductImage::class, mappedBy: 'Product', cascade: ['persist', 'remove'])]
private Collection $productImages;
/**
* ProductCategoryエンティティとのリレーション
* 削除連鎖(cascade: ['remove'])を設定しており、Productが削除されると関連するProductCategoryも削除される
*
* @var Collection<int, ProductCategory> $productCategories
*/
#[ORM\OneToMany(targetEntity: ProductCategory::class, mappedBy: 'Product', cascade: ['remove'])]
private Collection $productCategories;
public function __construct()
{
$this->productCategories = new ArrayCollection();
$this->ProductClasses = new ArrayCollection();
$this->ProductImage = new ArrayCollection();
$this->ProductTag = new ArrayCollection();
// $this->CustomerFavoriteProducts = new ArrayCollection();
}
/**
* 中間テーブル(ProductCategory)経由で関連するカテゴリ名(category_name)のリストを取得する
*
* @return array<string> 関連カテゴリ名の配列
*/
public function getCategoryNames(): array
{
return $this->productCategories->map(function (ProductCategory $productCategory) {
return $productCategory->getCategory()->getCategoryName();
})->toArray();
}
/**
* ProductCategoryエンティティのコレクションを取得します。
* このメソッドは、ProductType.phpおよびProductController.phpで使用され、
* ProductエンティティとCategoryエンティティの関係を管理します。
*
* @return Collection<int, ProductCategory> ProductCategoryエンティティのコレクション
*/
public function getProductCategories(): Collection
{
return $this->productCategories;
}
#[ORM\Column(type: Types::SMALLINT, nullable: true)]
private ?int $product_status_id = null;
/**
* SaleTypeエンティティとのManyToOne関係
* product_status_idカラムをリレーションキーとして使用
*
* @var SaleType|null $saleType 関連するSaleTypeエンティティ
*/
#[ORM\ManyToOne(targetEntity: SaleType::class)]
#[ORM\JoinColumn(name: "product_status_id", referencedColumnName: "id")]
private ?SaleType $saleType = null;
public function getSaleType(): ?SaleType
{
return $this->saleType;
}
public function setSaleType(?SaleType $saleType): self
{
$this->saleType = $saleType;
return $this;
}
#[ORM\Column(type: "string", length: 255)]
private string $name;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $note = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description_list = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description_detail = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $search_word = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $free_area = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $create_date = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $update_date = null;
#[ORM\OneToMany(mappedBy: "Product", targetEntity: ProductCategory::class, cascade: ["persist", "remove"])]
private Collection $ProductCategories;
#[ORM\OneToMany(mappedBy: "Product", targetEntity: ProductClass::class, cascade: ["persist", "remove"])]
private Collection $ProductClasses;
#[ORM\OneToMany(mappedBy: "Product", targetEntity: ProductImage::class, cascade: ["remove"])]
#[ORM\OrderBy(["sort_no" => "ASC"])]
private Collection $ProductImage;
#[ORM\OneToMany(mappedBy: "Product", targetEntity: ProductTag::class, cascade: ["remove"])]
private Collection $ProductTag;
// #[ORM\OneToMany(mappedBy: "Product", targetEntity: CustomerFavoriteProduct::class)]
// private Collection $CustomerFavoriteProducts;
#[ORM\ManyToOne(targetEntity: Member::class)]
#[ORM\JoinColumn(name: "creator_id", referencedColumnName: "id")]
private ?Member $Creator = null;
#[ORM\ManyToOne(targetEntity: ProductStatus::class)]
#[ORM\JoinColumn(name: "product_status_id", referencedColumnName: "id")]
private ?ProductStatus $Status = null;
#[ORM\Column(length: 255)]
private ?string $discriminator_type = 'product';
// 一度、コメントアウト
// #[ORM\Column(length: 32, nullable: true)]
// private ?string $supplier_code = null;
// private bool $_calc = false;
// private array $stockFinds = [];
// private array $stocks = [];
// private array $stockUnlimiteds = [];
// private array $price01 = [];
// private array $price02 = [];
// private array $price01IncTaxs = [];
// private array $price02IncTaxs = [];
// private array $codes = [];
// private array $classCategories1 = [];
// private array $classCategories2 = [];
// private ?string $className1 = null;
// private ?string $className2 = null;
public function getId(): ?int
{
return $this->id;
}
/**
* Set the creator.
*
* @param Member|null $creator
* @return self
*/
public function setCreator(?Member $creator): self
{
$this->Creator = $creator;
return $this;
}
/**
* Get the creator.
*
* @return Member|null
*/
public function getCreator(): ?Member
{
return $this->Creator;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): self
{
$this->note = $note;
return $this;
}
public function setDescriptionList(?string $descriptionList): self
{
$this->description_list = $descriptionList;
return $this;
}
public function getDescriptionList(): ?string
{
return $this->description_list;
}
public function setDescriptionDetail(?string $descriptionDetail): self
{
$this->description_detail = $descriptionDetail;
return $this;
}
public function getDescriptionDetail(): ?string
{
return $this->description_detail;
}
public function setSearchWord(?string $searchWord): self
{
$this->search_word = $searchWord;
return $this;
}
public function getSearchWord(): ?string
{
return $this->search_word;
}
public function getFreeArea(): ?string
{
return $this->free_area;
}
public function setFreeArea(?string $freeArea): self
{
$this->free_area = $freeArea;
return $this;
}
public function getCreateDate(): ?\DateTimeInterface
{
return $this->create_date;
}
public function setCreateDate(\DateTimeInterface $createDate): self
{
$this->create_date = $createDate;
return $this;
}
public function getUpdateDate(): ?\DateTimeInterface
{
return $this->update_date;
}
public function setUpdateDate(\DateTimeInterface $updateDate): self
{
$this->update_date = $updateDate;
return $this;
}
public function addProductCategory(ProductCategory $productCategory): self
{
$this->ProductCategories[] = $productCategory;
return $this;
}
public function removeProductCategory(ProductCategory $productCategory): bool
{
return $this->ProductCategories->removeElement($productCategory);
}
public function addProductClass(ProductClass $productClass): self
{
$this->ProductClasses[] = $productClass;
return $this;
}
public function getDiscriminatorType(): ?string
{
return $this->discriminator_type;
}
protected function setDiscriminatorType(string $discriminator_type): static
{
$this->discriminator_type = $discriminator_type;
return $this;
}
public function removeProductClass(ProductClass $productClass): bool
{
return $this->ProductClasses->removeElement($productClass);
}
public function getProductClasses(): Collection
{
return $this->ProductClasses;
}
public function addProductImage(ProductImage $productImage): self
{
$this->ProductImage[] = $productImage;
return $this;
}
// 一度コメントアウト
// public function getSupplierCode(): ?string
// {
// return $this->supplier_code;
// }
// public function setSupplierCode(?string $supplier_code): static
// {
// $this->supplier_code = $supplier_code;
// return $this;
// }
public function removeProductImage(ProductImage $productImage): bool
{
return $this->ProductImage->removeElement($productImage);
}
/**
* ProductImageのコレクションを取得するメソッド
*/
public function getProductImages(): Collection
{
return $this->productImages;
}
/**
* Add productTag.
*
* @param ProductTag $productTag
*
* @return Product
*/
public function addProductTag(ProductTag $productTag): self
{
if (!$this->ProductTag->contains($productTag)) {
$this->ProductTag[] = $productTag;
$productTag->setProduct($this); // リレーションの逆側を設定
}
return $this;
}
/**
* Remove productTag.
*
* @param ProductTag $productTag
*
* @return bool TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeProductTag(ProductTag $productTag): bool
{
if ($this->ProductTag->contains($productTag)) {
$this->ProductTag->removeElement($productTag);
// 関連の削除時に逆側も切断
if ($productTag->getProduct() === $this) {
$productTag->setProduct(null);
}
return true;
}
return false;
}
/**
* Get productTag.
*
* @return Collection
*/
public function getProductTag(): Collection
{
return $this->ProductTag;
}
// public function addCustomerFavoriteProduct(CustomerFavoriteProduct $customerFavoriteProduct): self
// {
// $this->CustomerFavoriteProducts[] = $customerFavoriteProduct;
// return $this;
// }
// public function removeCustomerFavoriteProduct(CustomerFavoriteProduct $customerFavoriteProduct): bool
// {
// return $this->CustomerFavoriteProducts->removeElement($customerFavoriteProduct);
// }
// public function getCustomerFavoriteProducts(): Collection
// {
// return $this->CustomerFavoriteProducts;
// }
/**
* Set status.
*
* @param ProductStatus|null $status
*
* @return Product
*/
public function setStatus(?ProductStatus $status): self
{
$this->Status = $status;
return $this;
}
/**
* Get status.
*
* @return ProductStatus|null
*/
public function getStatus(): ?ProductStatus
{
return $this->Status;
}
public function __toString(): string
{
return $this->name;
}
/**
* エンティティの初回保存時に create_date と update_date を自動設定
*/
#[ORM\PrePersist]
public function onPrePersist(): void
{
$this->create_date = new \DateTime();
$this->update_date = new \DateTime();
}
/**
* エンティティの更新時に update_date のみを自動設定
*/
#[ORM\PreUpdate]
public function onPreUpdate(): void
{
$this->update_date = new \DateTime();
}
// ★
public function getPrice02(): ?array
{
$price02Values = [];
foreach ($this->getProductClasses() as $ProductClass) {
$price02Values[] = $ProductClass->getPrice02();
}
return $price02Values;
}
}