如何解决传递给 App\Entity\MyOrders::setOrderstatus() 的参数 1 必须是 App\Entity\OrderStatus 的实例或 null,给定整数
拜托,我是 symfony 的新手,我正在尝试将引用 ID 保存到表中,但收到这些错误“传递给 App\Entity\MyOrders::setorderstatus() 的参数 1 必须是 App\Entity\OrderStatus 的实例或 null,给定整数,在 C:\xampp\htdocs\symphonycrud\src\Repository\MyOrdersRepository.PHP 第 69 行中调用” 请不要说此代码基于 symfony 5+,我尝试了很多选项但无济于事。 提前致谢。
namespace App\Entity;
use App\Repository\MyOrdersRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=MyOrdersRepository::class)
*/
class MyOrders
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string",length=50)
*/
private $order_code;
/**
* @ORM\Column(type="decimal",precision=10,scale=2)
*/
private $order_total;
/**
* @ORM\Column(type="decimal",scale=2)
*/
private $order_discount;
/**
* @ORM\Column(type="string",length=255)
*/
private $order_items;
/**
* @ORM\Column(type="string",length=255)
*/
private $shipping_details;
/**
* @ORM\ManyToOne(targetEntity=OrderStatus::class,inversedBy="myOrders")
* @ORM\JoinColumn(nullable=false)
*/
private $orderstatus;
public function getId(): ?int
{
return $this->id;
}
public function getorderCode(): ?string
{
return $this->order_code;
}
public function setorderCode(string $order_code): self
{
$this->order_code = $order_code;
return $this;
}
public function getorderTotal(): ?string
{
return $this->order_total;
}
public function setorderTotal(string $order_total): self
{
$this->order_total = $order_total;
return $this;
}
public function getorderdiscount(): ?string
{
return $this->order_discount;
}
public function setorderdiscount(string $order_discount): self
{
$this->order_discount = $order_discount;
return $this;
}
public function getorderItems(): ?string
{
return $this->order_items;
}
public function setorderItems(string $order_items): self
{
$this->order_items = $order_items;
return $this;
}
public function getShippingDetails(): ?string
{
return $this->shipping_details;
}
public function setShippingDetails(string $shipping_details): self
{
$this->shipping_details = $shipping_details;
return $this;
}
public function getorderstatus(): ?OrderStatus
{
return $this->orderstatus;
}
public function setorderstatus(?OrderStatus $orderstatus): self
{
$this->orderstatus = $orderstatus;
return $this;
}
}
我的第二个实体是 OrderStatus
namespace App\Entity;
use App\Repository\OrderStatusRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=OrderStatusRepository::class)
*/
class OrderStatus
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string",length=255)
*/
private $statusdescription;
/**
* @ORM\OnetoMany(targetEntity=MyOrders::class,mappedBy="orderstatus")
*/
private $myOrders;
public function __construct()
{
$this->myOrders = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getStatusdescription(): ?string
{
return $this->statusdescription;
}
public function setStatusdescription(string $statusdescription): self
{
$this->statusdescription = $statusdescription;
return $this;
}
/**
* @return Collection|MyOrders[]
*/
public function getMyOrders(): Collection
{
return $this->myOrders;
}
public function addMyOrder(MyOrders $myOrder): self
{
if (!$this->myOrders->contains($myOrder)) {
$this->myOrders[] = $myOrder;
$myOrder->setorderstatus($this);
}
return $this;
}
public function removeMyOrder(MyOrders $myOrder): self
{
if ($this->myOrders->removeElement($myOrder)) {
// set the owning side to null (unless already changed)
if ($myOrder->getorderstatus() === $this) {
$myOrder->setorderstatus(null);
}
}
return $this;
}
public function __toString()
{
return $this->statusdescription;
}
}
这是我的存储库 MyordersRepository
namespace App\Repository;
use App\Entity\MyOrders;
use App\Entity\OrderStatus;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
/**
* @method MyOrders|null find($id,$lockMode = null,$lockVersion = null)
* @method MyOrders|null findOneBy(array $criteria,array $orderBy = null)
* @method MyOrders[] findAll()
* @method MyOrders[] findBy(array $criteria,array $orderBy = null,$limit = null,$offset = null)
*/
class MyOrdersRepository extends ServiceEntityRepository
{
private $manager;
public function __construct(ManagerRegistry $registry,EntityManagerInterface $manager)
{
parent::__construct($registry,MyOrders::class);
parent::__construct($registry,OrderStatus::class);
$this->manager=$manager;
}
// /**
// * @return MyOrders[] Returns an array of MyOrders objects
// */
/*
public function findByExampleField($value)
{
return $this->createqueryBuilder('m')
->andWhere('m.exampleField = :val')
->setParameter('val',$value)
->orderBy('m.id','ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?MyOrders
{
return $this->createqueryBuilder('m')
->andWhere('m.exampleField = :val')
->setParameter('val',$value)
->getQuery()
->getoneOrNullResult()
;
}
*/
public function createorder($order_code,$order_total,$order_discount,$order_items,$shipping_details,$order_status)
{
//$entityManager = $this->getDoctrine()->getManager();
$neworders=new MyOrders();
//$os=new OrderStatus();
$neworders
->setorderCode($order_code)
->setorderTotal($order_total)
->setorderdiscount($order_discount)
->setorderItems($order_items)
->setShippingDetails($shipping_details)
->setorderstatus($order_status);
$this->manager->persist($neworders);
//$this->manager->persist($os);
$this->manager->flush();
}
这是我的控制器 MyOrdersController
/**
* @Route("/ordeRSS",methods={"GET"})
*/
public function ordeRSS(EntityManagerInterface $em,Request $request)//: JsonResponse
{
//$data = json_decode($request->getContent(),true);
$OrderStatus=new OrderStatus();
//$orders = new MyOrders();
$orderstatusss=$em->getRepository(OrderStatus::class)->findAll(['myOrders'=>$this->getUser()->getId()]);
//$orderstatusss=$em->getRepository(OrderStatus::class)->findAll();
//return new Response(array('hello'=>$orderstatusss));
$as = $orderstatusss['0'];
//dump();
//die();
$entityManager = $this->getDoctrine()->getManager();
$order_code='003';
$order_total=500;
$order_discount='0';
$order_items='dresss';
$shipping_details='test shipping';
$order_status=(8);
//$entityManager->persist($order_status);
$this->myordersrepository->createorder($order_code,$order_status);
}
解决方法
这不是 symfony 问题,您在方法 setOrderstatus 中指定只有 OrderStatus 对象或 null 可以作为参数传递,但是您在那里传递整数,因此如果您想加载对象,您需要从 DB 加载订单状态
$this->manager->getRepository(OrderStatus::class)->find($order_status);
或者由于您不需要来自该实体的所有数据,您可以只创建 reference。 所以你的创建函数看起来像这样
public function createorder($order_code,$order_total,$order_discount,$order_items,$shipping_details,$order_status)
{
//$entityManager = $this->getDoctrine()->getManager();
$neworders=new MyOrders();
//$os=new OrderStatus();
$neworders
->setOrderCode($order_code)
->setOrderTotal($order_total)
->setOrderDiscount($order_discount)
->setOrderItems($order_items)
->setShippingDetails($shipping_details)
->setOrderstatus($this->manager->getReference(OrderStatus::class,$order_status));
$this->manager->persist($neworders);
//$this->manager->persist($os);
$this->manager->flush();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。