问题描述
我有相关商品的销售,商品可能有变化或与订单无关,我希望产品返回与订单相关的变化,如下面的 json 所示。 它的方式给我带来了项目的所有变化,而不是与订单相关的变化
$order = $this->em->getRepository(\App\Domain\Order::class);
return $order->findOneBy(['id' => $id]);
预期结果
{
"id": 10
"items": [{
"id": 1,"product": {
"id": 1,"name": "Product A ","variation": {
"id": 10,"name": "Red"
}
}
}
]
}
订购商品
id | product_id | variation_id |
---|---|---|
1 | 1 | 10 |
2 | 2 | 15 |
<?php
namespace App\Domain;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
/**
* @ORM\Entity(repositoryClass="ItemRepository")
* @ORM\Table(name="items")
*/
class Item
{
/**
* @ORM\Id
* @ORM\Column(type="integer",name="id")
* @JMS\Type("int")
* @JMS\Groups ({"list","details"})
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="Product")
* @ORM\JoinColumn(name="product_id",referencedColumnName="id")
* @JMS\Type("App\Domain\Product")
* @JMS\Groups ({"list","details"})
* @var Product
*/
private $product;
}
产品
id | 姓名 |
---|---|
1 | 产品 A |
2 | 产品B |
<?php
namespace App\Domain;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Gedmo\Mapping\Annotation as LOG;
/**
* @ORM\Entity(repositoryClass="ProductRepository")
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="products")
* @LOG\Loggable
*/
class Product
{
use TimestampableTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer","details","combobox"})
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Variation",mappedBy="product",cascade={"persist","remove"},orphanRemoval=true))
* @JMS\Type("ArrayCollection<App\Domain\Variation>")
* @JMS\Groups ({"details"})
* @JMS\Accessor(getter="getVariations",setter="syncVariations")
*/
private $variations;
}
变化
id | product_id | 姓名 |
---|---|---|
10 | 1 | 红色 |
15 | 2 | 蓝色 |
<?php
namespace App\Domain;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
/**
* @ORM\Entity(repositoryClass="VariationRepository")
* @ORM\Table(name="variations")
*/
class Variation
{
/**
* @ORM\Id
* @ORM\Column(type="integer",name="id")
* @ORM\GeneratedValue(strategy="AUTO")
* @JMS\Type("int")
* @JMS\Groups ({"details"})
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Product",inversedBy="variations")
* @ORM\JoinColumn(name="product_id",referencedColumnName="id_produto")
* @JMS\Type("ArrayCollection<App\Domain\Product>")
* @JMS\Groups ({"details"})
* @JMS\Accessor(getter="getProduct",setter="setProduct")
*/
private $product;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)