2016-12-06 37 views
0

當我嘗試delete or clear速遞公司時,它會拋出以下錯誤。我認爲我沒有很好的調試技巧。任何人都可以幫助我,這背後的原因是什麼?這裏是控制器,視圖和模態。實體必須被管理或計劃在Symfony/twig中進行單個計算

Entity has to be managed or scheduled for removal for single computation Sokosimu\PostalDeliveryBundle\Entity\[email protected] 

控制器

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')); 
} 
} 

嫩枝(查看)

<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

你能解釋是什麼意思$ em-> persist($ rate)和$ em-> remove($ rate)?爲什麼你試圖堅持$ rate(和postalcharge一樣),並立即刪除它? (你期望什麼行爲?) – Timurib

回答

1

據我所知,你要刪除你的送貨公司的每一個郵政費用。你似乎缺乏學說對象表示的基本知識,我建議你閱讀和理解的文件(特別是在一對多部分):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; 
} 
相關問題