必须为Symfony / twig中的单个计算管理或计划删除实体

时间:2016-12-06 10:28:34

标签: symfony doctrine

当我尝试delete or clear快递公司的费率时,它会抛出以下错误。我想我没有很好的调试技巧。任何人都可以帮助我,这背后的原因是什么?这是控制器,视图和模态。

 Entity has to be managed or scheduled for removal for single computation Sokosimu\PostalDeliveryBundle\Entity\PostalCharge@0000000059f7113c00000000372b1d9d

控制器

 public function clearRatesAction(DeliveryCompany $deliveryCompany){

    $em = $this->get('doctrine')->getManager();
    $postalcharge = new PostalCharge();
    $em->getConnection()->beginTransaction();
    try {

        $rate = $postalcharge ->setDeliveryCompany($deliveryCompany);
        $em->persist($rate);
        $em->remove($rate);
        $em->flush($rate);
        $em->getConnection()->commit();
    }  catch(Exception $e){
        $em->getConnection()->rollback();
        throw $e;
    }
    return $this->redirect($this->generateUrl('sokosimu_postal_delivery_list_company'));
}
}

Twig(观点)

<tbody>
   {% for deliveryCompany in deliveryCompanyList %}
     <tr>
    <td><a href="{{ path('sokosimu_postal_delivery_company_detail',{'deliveryCompany':deliveryCompany.getid()}) }}">{{ deliveryCompany.getName() }}</td>
<td>{{ deliveryCompany.getAddress() }}</td>
 <td><a href="{{ path('sokosimu_postal_delivery_company_edit',{'deliveryCompany':deliveryCompany.getId()}) }}">Edit</a>
 <a href="{{ path('sokosimu_postal_delivery_clear_rates',{'deliveryCompany':deliveryCompany.getId()}) }}">ClearRates</a>
                                </td>
                            </tr>
 {% endfor %}
 </tbody>

邮政费用实体

     <?php
    // src/Sokosimu/PostalDeliveryBundle/Entity/PostalCharge.php

    namespace Sokosimu\PostalDeliveryBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**  *                 @ORM\Entity(repositoryClass="Sokosimu\PostalDeliveryBundle\Entity\Repository\PostalChargeRepository")
  * @ORM\Table(name="postal_charge")
  * @ORM\HasLifecycleCallbacks()
  */

class PostalCharge
{

/**
 * @ORM\Id
 * @ORM\Column(name="id",type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(name="record_type",type="string", length=225)
 */
protected $recordType;

/**
 * @ORM\Column(name="active_from",type="datetime",  nullable=True)
 */
protected $activeFrom;

/**
 * @ORM\Column(name="active_to",type="datetime",  nullable=True)
 */
protected $activeTo;

/**
 * @ORM\Column(name="active_record",type="string", length=225, nullable=True)
 */
protected $activeRecord;

/**
 * @ORM\Column(name="origin_country",type="string", length=225, nullable=True)
 */
protected $originCountry;

/**
 * @ORM\Column(name="weight_from",type="integer")
 */
protected $weightFrom;

/**
 * @ORM\Column(name="weight_to",type="integer")
 */
protected $weightTo;

/**
 * @ORM\Column(name="from_region",type="string", length=225, nullable=True)
 */
protected $fromRegion;

/**
 * @ORM\Column(name="to_region",type="string", length=225, nullable=True)
 */
protected $toRegion;

/**
 * @ORM\Column(name="priority",type="string", length=225, nullable=True)
 */
protected $priority;

/**
 * @ORM\Column(name="economy",type="string", length=225, nullable=True)
 */
protected $economy;

/**
 * @ORM\Column(name="increment_price",type="string", length=225, nullable=True)
 */
protected $incrementPrice;

/**
 * @ORM\Column(name="tax",type="string", length=225, nullable=True)
 */
protected $tax;

/**
 * @ORM\Column(name="insurance",type="string", length=225, nullable=True)
 */
protected $insurance;

/**
 * @ORM\Column(name="increment_unit",type="string", length=225, nullable=True)
 */
protected $incrementUnit;


/**
 * @ORM\Column(name="currency_code",type="string", length=225, nullable=True)
 */
protected $currencyCode;

/**
 * @ORM\Column(name="country_code",type="string", length=225, nullable=True)
 */
protected $countryCode;

/**
 * @ORM\ManyToOne(targetEntity="DeliveryCompany")
 * @ORM\JoinColumn(name="delivery_company_id",referencedColumnName="id",nullable=True)
 */
protected $deliveryCompany;


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set recordType
 *
 * @param string $recordType
 * @return PostalCharge
 */
public function setRecordType($recordType)
{
    $this->recordType = $recordType;

    return $this;
}

/**
 * Get recordType
 *
 * @return string 
 */
public function getRecordType()
{
    return $this->recordType;
}

/**
 * Set activeFrom
 *
 * @param \DateTime $activeFrom
 * @return PostalCharge
 */
public function setActiveFrom($activeFrom)
{
    $this->activeFrom = $activeFrom;

    return $this;
}

/**
 * Get activeFrom
 *
 * @return \DateTime 
 */
public function getActiveFrom()
{
    return $this->activeFrom;
}

/**
 * Set activeTo
 *
 * @param \DateTime $activeTo
 * @return PostalCharge
 */
public function setActiveTo($activeTo)
{
    $this->activeTo = $activeTo;

    return $this;
}

/**
 * Get activeTo
 *
 * @return \DateTime 
 */
public function getActiveTo()
{
    return $this->activeTo;
}

/**
 * Set activeRecord
 *
 * @param string $activeRecord
 * @return PostalCharge
 */
public function setActiveRecord($activeRecord)
{
    $this->activeRecord = $activeRecord;

    return $this;
}

/**
 * Get activeRecord
 *
 * @return string 
 */
public function getActiveRecord()
{
    return $this->activeRecord;
}

/**
 * Set originCountry
 *
 * @param string $originCountry
 * @return PostalCharge
 */
public function setOriginCountry($originCountry)
{
    $this->originCountry = $originCountry;

    return $this;
}

/**
 * Get originCountry
 *
 * @return string 
 */
public function getOriginCountry()
{
    return $this->originCountry;
}

/**
 * Set weightFrom
 *
 * @param integer $weightFrom
 * @return PostalCharge
 */
public function setWeightFrom($weightFrom)
{
    $this->weightFrom = $weightFrom;

    return $this;
}

/**
 * Get weightFrom
 *
 * @return integer 
 */
public function getWeightFrom()
{
    return $this->weightFrom;
}

/**
 * Set weightTo
 *
 * @param integer $weightTo
 * @return PostalCharge
 */
public function setWeightTo($weightTo)
{
    $this->weightTo = $weightTo;

    return $this;
}

/**
 * Get weightTo
 *
 * @return integer 
 */
public function getWeightTo()
{
    return $this->weightTo;
}

/**
 * Set fromRegion
 *
 * @param string $fromRegion
 * @return PostalCharge
 */
public function setFromRegion($fromRegion)
{
    $this->fromRegion = $fromRegion;

    return $this;
}

/**
 * Get fromRegion
 *
 * @return string 
 */
public function getFromRegion()
{
    return $this->fromRegion;
}

/**
 * Set toRegion
 *
 * @param string $toRegion
 * @return PostalCharge
 */
public function setToRegion($toRegion)
{
    $this->toRegion = $toRegion;

    return $this;
}

/**
 * Get toRegion
 *
 * @return string 
 */
public function getToRegion()
{
    return $this->toRegion;
}

/**
 * Set priority
 *
 * @param string $priority
 * @return PostalCharge
 */
public function setPriority($priority)
{
    $this->priority = $priority;

    return $this;
}

/**
 * Get priority
 *
 * @return string 
 */
public function getPriority()
{
    return $this->priority;
}

/**
 * Set economy
 *
 * @param string $economy
 * @return PostalCharge
 */
public function setEconomy($economy)
{
    $this->economy = $economy;

    return $this;
}

/**
 * Get economy
 *
 * @return string 
 */
public function getEconomy()
{
    return $this->economy;
}

/**
 * Set incrementPrice
 *
 * @param string $incrementPrice
 * @return PostalCharge
 */
public function setIncrementPrice($incrementPrice)
{
    $this->incrementPrice = $incrementPrice;

    return $this;
}

/**
 * Get incrementPrice
 *
 * @return string 
 */
public function getIncrementPrice()
{
    return $this->incrementPrice;
}

/**
 * Set tax
 *
 * @param string $tax
 * @return PostalCharge
 */
public function setTax($tax)
{
    $this->tax = $tax;

    return $this;
}

/**
 * Get tax
 *
 * @return string 
 */
public function getTax()
{
    return $this->tax;
}

/**
 * Set insurance
 *
 * @param string $insurance
 * @return PostalCharge
 */
public function setInsurance($insurance)
{
    $this->insurance = $insurance;

    return $this;
}

/**
 * Get insurance
 *
 * @return string 
 */
public function getInsurance()
{
    return $this->insurance;
}

/**
 * Set incrementUnit
 *
 * @param string $incrementUnit
 * @return PostalCharge
 */
public function setIncrementUnit($incrementUnit)
{
    $this->incrementUnit = $incrementUnit;

    return $this;
}

/**
 * Get incrementUnit
 *
 * @return string 
 */
public function getIncrementUnit()
{
    return $this->incrementUnit;
}

/**
 * Set currencyCode
 *
 * @param string $currencyCode
 * @return PostalCharge
 */
public function setCurrencyCode($currencyCode)
{
    $this->currencyCode = $currencyCode;

    return $this;
}

/**
 * Get currencyCode
 *
 * @return string 
 */
public function getCurrencyCode()
{
    return $this->currencyCode;
}

/**
 * Set countryCode
 *
 * @param string $countryCode
 * @return PostalCharge
 */
public function setCountryCode($countryCode)
{
    $this->countryCode = $countryCode;

    return $this;
}

/**
 * Get countryCode
 *
 * @return string 
 */
public function getCountryCode()
{
    return $this->countryCode;
}

/**
 * Set deliveryCompany
 *
 * @param \Sokosimu\PostalDeliveryBundle\Entity\DeliveryCompany $deliveryCompany
 * @return PostalCharge
 */
public function setDeliveryCompany(\Sokosimu\PostalDeliveryBundle\Entity\DeliveryCompany $deliveryCompany = null)
{
    $this->deliveryCompany = $deliveryCompany;

    return $this;
}

/**
 * Get deliveryCompany
 *
 * @return \Sokosimu\PostalDeliveryBundle\Entity\DeliveryCompany 
 */
public function getDeliveryCompany()
{
    return $this->deliveryCompany;
}
}

1 个答案:

答案 0 :(得分:1)

据我了解,您想要从您的送货公司中删除每个邮政费用。您似乎缺乏学说对象表示的基本知识,我建议您阅读并理解文档(特别是oneToMany部分):http://symfony.com/doc/current/doctrine.html

但是你应该这样做:

 public function clearRatesAction(DeliveryCompany $deliveryCompany){
$em = $this->get('doctrine')->getManager();
$em->getConnection()->beginTransaction();

try {

    foreach($deliveryCompany->getRates() as $postalCharge) {
       $em->remove($postalCharge);
    }
    $em->flush( );
    $em->getConnection()->commit();
}  catch(Exception $e){
    $em->getConnection()->rollback();
    throw $e;
}