gpt4 book ai didi

php - Doctrine:与属性或位置的多对多关系

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:22 25 4
gpt4 key购买 nike

我有那些实体:

首先:ProductCupMain -> 像一个产品

<?php

namespace xxx\Security\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* xxx\Security\Entity\ProductCupMain
*
* @ORM\Table(name="product_cup_main")
* @ORM\Entity
*/
class ProductCupMain
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;

/**
* @var string $image
*
* @ORM\Column(name="image", type="string", length=128, nullable=true)
*/
private $image;

/**
* @var string $pdf
*
* @ORM\Column(name="pdf", type="string", length=128, nullable=true)
*/
private $pdf;

/**
* @var ProductCategories
*
* @ORM\ManyToMany(targetEntity="ProductCategories", inversedBy="productCupMain")
* @ORM\JoinTable(name="product_cup_main_has_product_categories",
* joinColumns={
* @ORM\JoinColumn(name="product_cup_main_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="product_categories_id", referencedColumnName="id")
* }
* )
*/
private $productCategories;

第二:ProductCategories -> like Categories

<?php

namespace xxx\Security\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* xxx\Security\Entity\ProductCategories
*
* @ORM\Table(name="product_categories")
* @ORM\Entity
*/
class ProductCategories
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=45, nullable=false)
*/
private $name;

/**
* @var string $image
*
* @ORM\Column(name="image", type="string", length=100, nullable=true)
*/
private $image;

/**
* @var string $shortname
*
* @ORM\Column(name="shortname", type="string", length=164, nullable=false)
*/
private $shortname;

/**
* @var integer $position
*
* @ORM\Column(name="position", type="integer", nullable=false)
*/
private $position;

/**
* @var ProductCupMain
*
* @ORM\ManyToMany(targetEntity="ProductCupMain", mappedBy="productCategories")
*/
private $productCupMain;

所以,我想在许多类别中保存许多产品。 (多对多关系)我的问题是立场。产品在类别中有不同的位置。

我想保存关系上的位置,像这样:

http://i.stack.imgur.com/Z0gzw.jpg

我需要一个像这样的方法:getPositionInCategory($categoryId) 我可以在其中获得正确的位置。你能帮帮我吗?

此外,我还发现了类似的问题here ,但我无法找到适合我的解决方案。

编辑 1

来自@mbinette 的解决方案:我创建了第三个实体供引用。对吗?

//产品类别引用

<?php

namespace xxx\Security\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* xxx\Security\Entity\ProductCategoryReference
*
* @ORM\Table(name="product_cup_main_has_product_categories")
* @ORM\Entity
*/
class ProductCategoryReference
{

/**
* @ORM\ManyToOne(targetEntity="ProductCupMain", inversedBy="categoriesHavetheProduct")
*/
private $prductCupMain;

/**
* @ORM\ManyToOne(targetEntity="ProductCategories", inversedBy="productsInCategory")
*/
private $productsCategorie;

/**
* @ORM\Column(name="postionInCategorie", type="integer", length=164, nullable=false)
*/
private $postionInCategorie;


public function getProductCupMain()
{
return $this->prductCupMain;
}

public function setProductCupMain($prductCupMain)
{
$this->prductCupMain = $prductCupMain;
}

public function getProductsCategorie()
{
return $this->productsCategorie;
}

public function setProductsCategorie($productsCategorie)
{
$this->productsCategorie = $productsCategorie;
}

public function getPostionInCategorie()
{
return $this->productsCategorie;
}

public function setPostionInCategorie($postionInCategorie)
{
$this->postionInCategorie = $postionInCategorie;
}

}

但是产品实体和类别实体是什么?

产品实体:

/**
* @var CategoryHavetheProduct
*
* @OneToMany(targetEntity="ProductCategoryReference", mappedBy="productCupMain")
*
**/
private $categoriesHavetheProduct;

//Please show me the getter and setter methods

类别实体:

/**
* @var ProductsInCategory
*
* @OneToMany(targetEntity="ProductCategoryReference", mappedBy="productsCategorie")
*
**/
private $productsInCategory;

//Please show me the getter and setter methods

最佳答案

无法向 Dotrine/Doctrine2 实体关系添加更多属性。如果您需要保存更多与关系相关的数据(除了涉及的实体),那么它不仅仅是一个简单的关系,不是吗? ;-)

您可以做的正是您在图表中所做的 - 创建 3 个不同的实体,它们通过 ManyToOne/OneToMany 关系链接。然后您可以自定义该实体并添加您需要的信息/职责。

编辑

所谓位置,是指你设定的位置吧?或者你的意思是你想保持它们被添加到你的收藏中的顺序?如果是这样,则有一些 Unresolved 票证(ticket 1ticket 2),所以也许我们将来会看到。现在,我认为您必须坚持使用 RelationshipEntity(3 个实体)。

希望这对您有所帮助。

关于php - Doctrine:与属性或位置的多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com