gpt4 book ai didi

php - 仅当子对象尚不存在时才将子对象添加到父对象

转载 作者:可可西里 更新时间:2023-10-31 22:49:52 25 4
gpt4 key购买 nike

我正忙于为 parent 开发一个 Action ,它应该添加用户输入给定的子级数。

child 有 3 个属性,将它们结合起来,每个 child 应该始终是唯一的。

我使用 Symfony 和 Doctrine,我的基本父类如下所示:

class Parent
{
/**
* @var Child[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity="Child", mappedBy="parent")
* @ORM\OrderBy({"dateCreated": "DESC"})
*/
private $childs;

/**
* Add child
*
* @param \AppBundle\Entity\Child $child
*
* @return Parent
*/
public function addChild(\AppBundle\Entity\Child $child)
{
$this->childs[] = $child;
$child->setParent($this);
return $this;
}

/**
* Remove child
*
* @param \AppBundle\Entity\Child $child
*/
public function removeChild(\AppBundle\Entity\Child $child)
{
$this->childs->removeElement($child);
}

/**
* Get childs
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChilds()
{
return $this->childs;
}
}

我的子类看起来像这样(同样非常基础):

class Child
{
/**
* @var int
*
* @ORM\Column(name="cupboard", type="integer")
*/
private $cupboard;

/**
* @var int
*
* @ORM\Column(name="shelf", type="integer")
*/
private $shelf;

/**
* @var int
*
* @ORM\Column(name="item", type="integer")
*/
private $item;

/**
* @var Parent
*
* @ORM\ManyToOne(targetEntity="Parent", inversedBy="childs")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
* })
*/
private $parent;

/**
* Set cupboard
*
* @param string $cupboard
*
* @return Child
*/
public function setCupboard($cupboard)
{
$this->cupboard = $cupboard;

return $this;
}

/**
* Get cupboard
*
* @return int
*/
public function getCupboard()
{
return $this->cupboard;
}

/**
* Set shelf
*
* @param string $shelf
*
* @return Child
*/
public function setShelf($shelf)
{
$this->shelf = $shelf;

return $this;
}

/**
* Get shelf
*
* @return int
*/
public function getShelf()
{
return $this->shelf;
}

/**
* Set item
*
* @param string $item
*
* @return Child
*/
public function setItem($item)
{
$this->item = $item;

return $this;
}

/**
* Get item
*
* @return int
*/
public function getItem()
{
return $this->item;
}

/**
* Set parent
*
* @param Parent $parent
*
* @return Child
*/
public function setParent(Parent $parent)
{
$this->parent = $parent;

return $this;
}

/**
* Get parent
*
* @return Parent
*/
public function getParent()
{
return $this->parent;
}
}

然后我对每个 parent 对象都有一个 Action (有点像编辑 Action )必须为它创建子对象。当我单击链接(针对特定父级)时,会生成一个包含三个输入字段的表单:

  • 橱柜
  • 书架
  • 项目数

然后用户必须用整数指定他想要添加到哪个橱柜和哪个架子中的元素数量。如果元素(整数)已经存在于给定架子上的给定橱柜中,则不应再次出现,但应为元素使用第一个下一个可用整数。

我怎样才能让它尽可能简单?我知道我可以使用 Parent 类中的 addChild 函数,但我不确定如何使用。

到目前为止我尝试的是创建一个数组并根据橱柜和架子对所有项目进行分组,然后如果该项目不存在于该数组中则应该创建它。

这是我的代码:

public function addItemAction(Request $request, $id = null){
$parent = $this->admin->getSubject();

if (!$parent) {
throw new NotFoundHttpException(sprintf('Unable to find the object with id: %s', $id));
}

$em = $this->getDoctrine()->getManager();

$form = $this->createForm(AddItemType::class);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$kids = $popUnit->getChilds();
$formParams = $request->request->get('add_item');

$units = array();
foreach ($kids as $kid) {
$units[$kid->getCupboard()][$kid->getShelf()][$kid->getItem()] = $kid->getItem();
}

$givenShelf = $units[$formParams['cupboard']][$formParams['shelf']];

for ($i = 1; $i <= $formParams['itemAmount']; $i++) {
if (!in_array($i, $givenShelf)) {
$child = new Child();
$child->setParent($parent);
$child->setCupboard($formParams['cupboard']);
$child->setShelf($formParams['shelf']);
$child->setItem($i);

$em->persist($child);
$em->flush();
}
}
return new RedirectResponse($this->admin->generateUrl('show', array('id' => $parent->getId())));
}

return $this->render('AppBundle:Parent:add_childs.html.twig', array(
'form' => $form->createView(),
'parent' =>$parent,
));
}

有关额外信息,这是我的表单构建器的外观:

public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('cupboard', NumberType::class)
->add('shelf', NumberType::class)
->add('itemAmount', NumberType::class, array('label' => 'Number of Items'))
;
}

我怎样才能让这个 Action 尽可能简单,以确保只有唯一的元素被添加到橱柜的架子上供 parent 使用。我无法更改属性或类。我必须解决这个问题。我不想使用任何其他复杂的方式,例如创建任何监听器或其他 until 函数。

我希望我已经很好地解释了我的情况,我希望有人可以帮助我提供一些好的反馈。

最佳答案

你是正确的,你可以像这样修改 Parent 类的 addChild 函数:

    /**
* Add child
*
* @param \AppBundle\Entity\Child $child
*
* @return Parent
*/
public function addChild(Child $child)
{
if ( ! is_array($this->childs) || ! in_array($child, $this->childs)) {
$this->childs[] = $child;
$child->setParent($this);
}
return $this;
}

这个条件简单地说明如果还没有 child ,添加 child ,或者如果 child 不在 $this->childs 数组中然后添加它。

关于php - 仅当子对象尚不存在时才将子对象添加到父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43078998/

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