php - 排序数据和自动排序的插入 - 算法

标签 php algorithm symfony

好吧,我知道我可能会因为这个问题而遭到反对,但我真的需要帮助,否则我将在几个小时内脱掉头发。

我有一个这样的数组:

array(6) {
  [0]=>
  object((2) {
    ["nivId"]=>int(3)
    ["nivOrdre"]=>int(1)
  }
  [1]=>
  object((2) {
    ["nivId"]=>int(4)
    ["nivOrdre"]=>int(2)
  }
  [2]=>
  object((2) {
    ["nivId"]=>int(6)
    ["nivOrdre"]=>int(3)
  }
  [3]=>
  object((2) {
    ["nivId"]=>int(2)
    ["nivOrdre"]=>int(4)
  }
  [4]=>
  object((2) {
    ["nivId"]=>int(1)
    ["nivOrdre"]=>int(5)
  }
  [5]=>
  object((2) {
    ["nivId"]=>int(5)
    ["nivOrdre"]=>int(6)
  }
}

在我的 HTML 中,我按 nivOrdre 排序显示它们

我可以在 HTML 中为它们中的每一个修改 nivOrdre,它会在数据库中发生变化。

我想做的是当我修改一个 nivOrdre 时,所有其他更高的都增加一个。

由于 nivIdnivOrdre,我无法让循​​环正常工作,不知道如何编写该算法。

当两个值之间存在差距时,我还尝试递增。 我的代码有很多错误,我迫切希望有一天能让它工作......

这是我做的:

public function modNiveaux($niveau) {
    $niveaux = $this->getNiveauxRepository()->findBy(array(), array('nivOrdre' => 'ASC'));
    $add = false; $ite=0;
    for($i=$niveau->getNivOrdre(); $i<sizeof($niveaux); $i++) {
        echo $niveau->getNivOrdre().':'.$niveaux[$i]->getNivOrdre().'<br/>';
        if($niveau->getNivOrdre() != $niveaux[$i-1]->getNivOrdre() && $niveau->getNivOrdre() != $niveaux[$i-1]->getNivOrdre())
            $add=true;
    }

    for($i=0; $i<sizeof($niveaux); $i++){
        if($niveaux[$i]->getNivOrdre() == $niveau->getNivOrdre()){
            $ite=$i;
        }
    }

    if($add){
        for($i=$ite; $i<=sizeof($niveaux)-1; $i++){
            $niveaux[$i]->setNivOrdre($niveaux[$i]->getNivOrdre()+1);
            $this->getEntityManager()->persist($niveaux[$i]);
        }
    }

    $this->getEntityManager()->flush();
}

该代码在 Service 中,并在 Controller 中调用:

public function updateAction($id) {
    $request = $this->get('request');
    if (is_null($id)) {
        $postData = $request->get('niveaux');
        $id = $postData['id'];
    }

    $this->niveauxService = $this->get("intranet.niveaux_service");
    $niveau = $this->niveauxService->getNiveau($id);

    $form = $this->createForm(new NiveauxType(), $niveau);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $this->niveauxService->saveNiveau($niveau);
        $this->niveauxService->modNiveaux($niveau);
        $this->get('session')->getFlashBag()->add('notice', 'Objet sauvegardé avec succès');
    } else {
        $this->get('session')->getFlashBag()->add('noticeError', 'L\'objet n\'a pu être mis à jour.');
    }

    return array(
        'form' => $form->createView(),
        'id' => $id,
    );
}

如果有人有想法让它发挥作用,我将永远感激..

最佳答案

根据您的问题和评论,您要做的就是用大于或等于已更改实体的新值的 ordre 增加所有 niveaux。

由于提供给 modNiveaux 方法的实体已经分配了新值,因此在服务中您需要检索比当前实体具有更大 ordre 的实体(除了当前!) 并增加它们。

当前 实体的值已被表单更改,因此与它无关。

应该是这样的:

public function modNiveaux($niveau) {
    $criteria = new \Doctrine\Common\Collections\Criteria();
    //greater or equal nivOrdre
    $criteria->where($criteria->expr()->gte('nivOrdre', $niveau->getNivOrdre()));
    //but not the current one
    $criteria->andWhere($criteria->expr()->neq('nivId', $niveau->getNivId()));

    $niveaux = $this->getNiveauxRepository()->matching($criteria);
    //increment all of them and persist
    foreach($niveaux as $item) {
        $item->setNivOrdre($item->getNivOrdre()+1);
        $this->getEntityManager()->persist($item);
    }

    $this->getEntityManager()->flush();
}

这段代码当然没有经过测试,可能包含简单的错误,但就是这个想法。

关于php - 排序数据和自动排序的插入 - 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703322/

相关文章:

PHP:二维数组创建索引或添加(如果存在)

php - JSON 未在 php 中编译

algorithm - 设计哈希表

php - 是否有使用 symfony 中的 SwiftMailer bundle 将 NTLM 身份验证类型添加到 SwiftMailer 中的正确方法?

php - ParamConverter 可选但有效

php - 如何通过 SyliusResourceBundle 使用 Sylius 创建新模型

php - 请问laravel数据库事务锁表吗?

php - 阿拉伯语文本加密和解密添加到服务器

c - 输入太大时算法返回垃圾?

javascript - 如何最小化(二进制)搜索树的视觉宽度?