2017-04-13 60 views
-1

我與Symfony2的工作,我面臨着一個錯誤:錯誤:方法__toString()不得拋出一個異常的Symfony2

Error: Method Proxies__CG__\Sprint\SiteBundle\Entity\Task::__toString() must not throw an exception

下面是這個這個代碼在我Entity\Task.php

/** 
* @return string 
*/ 
public function __toString() 
{ 
    return $this->title; 
} 

主要的問題,在我的本地版本的網站上一切正常,我只有在我的生活網站上有這個錯誤。但他們是相似的!

這裏我實體\ Task.php代碼:

class Task 
    { 

    use TraitDateTimeFields; 

    use TraitCreatorTimeFields; 

    use TraitTaskTeamFields; 

    use TraitTimeTrackFields; 

    use TraitTagFields; 
    /** 
    * @ORM\ManyToMany(targetEntity="Sprint\SiteBundle\Entity\Tag") 
    * @ORM\JoinTable(
    * name="s_task_tags", 
    * joinColumns={ 
    *  @ORM\JoinColumn(name="task_id", referencedColumnName="id") 
    * }, 
    * inverseJoinColumns={ 
    *  @ORM\JoinColumn(name="tag_id", referencedColumnName="id") 
    * } 
    * ) 
    */ 
    private $tags; 

    /** 
    * @ORM\Column(type="integer", name="id") 
    * @ORM\Id 
    * @ORM\GeneratedValue 
    * @Gedmo\TreePathSource 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="integer", name="depth", nullable=true) 
    * @Gedmo\TreeLevel 
    * @var integer 
    */ 
    private $depth; 

    /** 
    * @ORM\Column(type="string", name="path", length=255, nullable=true) 
    * @Gedmo\TreePath 
    * @var string 
    */ 
    private $path; 

    /** Это группа 
    * @ORM\Column(type="boolean", name="flag_group") 
    * @var string 
    */ 
    private $flagGroup = false; 

    /** Архив 
    * 
    * @ORM\Column(type="boolean", name="flag_archive") 
    * @var string 
    */ 
    private $flagArchive = false; 

    /** 
    * @ORM\Column(type="string", name="number", nullable=false) 
    * @var string 
    */ 
    protected $number = ''; 

    /** 
    * @ORM\Column(type="string", name="title", nullable=true) 
    * @var string 
    */ 
    protected $title; 

    /** 
    * @ORM\Column(type="integer", name="resource") 
    * @var string 
    * @deprecated 
    */ 
    protected $resource = 0; 

    /** 
    * @ORM\Column(type="integer", name="bonus") 
    * @var string 
    */ 
    protected $bonus = 0; 

    /** 
    * @ORM\Column(type="integer", name="state") 
    * @var string 
    */ 
    protected $state = TaskState::C_WORKING; 

    /** 
    * @ORM\Column(type="integer", name="ttype") 
    * @var string 
    */ 
    protected $type = TaskType::C_INNER; 

    /** Начало 
    * 
    * @ORM\Column(type="datetime", name="start_to", nullable=true) 
    * @var \DateTime 
    */ 
    private $startTo; 

    /** Дедлайн 
    * 
    * @ORM\Column(type="datetime", name="deadline_to", nullable=true) 
    * @var \DateTime 
    */ 
    private $deadlineTo; 

    /** Срочно 
    * 
    * @ORM\Column(type="boolean", name="flag_quicly") 
    * @var string 
    */ 
    private $flagQuickly = false; 

    /** Формирование бюджета 
    * 
    * @ORM\Column(type="boolean", name="flag_budget") 
    * @var string 
    */ 
    private $flagBudget = true; 

    /** 
    * @ORM\OneToMany(targetEntity="Sprint\SiteBundle\Entity\EstimateOperatingRow", mappedBy="task", cascade={"detach","persist"}, orphanRemoval=false) 
    * @var \Doctrine\Common\Collections\ArrayCollection 
    */ 
    protected $estimateRows; 


    /** 
    * @ORM\ManyToOne(targetEntity="Sprint\SiteBundle\Entity\Project", inversedBy="tasks") 
    * @ORM\JoinColumn(name="project_id", referencedColumnName="id", onDelete="CASCADE") 
    * @var \Sprint\SiteBundle\Entity\Project 
    */ 
    protected $project; 

    /** 
    * @ORM\ManyToOne(targetEntity="Sprint\SiteBundle\Entity\Task", inversedBy="children") 
    * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE") 
    * @Gedmo\TreeParent 
    * @var \Sprint\SiteBundle\Entity\Task 
    */ 
    protected $parent; 

    /** Подзадачи 
    * 
    * @ORM\OneToMany(targetEntity="Sprint\SiteBundle\Entity\Task", mappedBy="parent", cascade={"all"}, orphanRemoval=true) 
    * @var \Doctrine\Common\Collections\ArrayCollection 
    */ 
    public $children; 

    /** Задания 
    * 
    * @ORM\OneToMany(targetEntity="Sprint\SiteBundle\Entity\TaskAssignment", mappedBy="task", cascade={"all"}, orphanRemoval=true) 
    * @var \Doctrine\Common\Collections\ArrayCollection 
    */ 
    public $assignment; 

    /** Активность 
    * 
    * @ORM\OneToMany(targetEntity="Sprint\SiteBundle\Entity\TaskActivityItem", mappedBy="task", cascade={"all"}, orphanRemoval=true) 
    * @var \Doctrine\Common\Collections\ArrayCollection 
    */ 
    public $activity; 

    private $activitySummary = []; 

    protected $operationItems = []; 

    /** 
    * Task constructor. 
    */ 
    public function __construct() 
    { 
     $this->children  = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->activity  = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->auditors  = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->staff   = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->tags   = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->estimateRows = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->assignment = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * @return string 
    */ 
    public function __toString() 
    { 
     return $this->title; 
    } 

    public function getTreeTitle() 
    { 
     return str_pad('', ($this->depth-1)*6*4, ' ', STR_PAD_LEFT) . ($this->flagGroup ? ' ' : ' ') . $this->title; 
    } 

    /** 
    * @return array 
    */ 
    public function getPaySummary() 
    { 
     $result = [ 
      'total' => 0, 
      'paid' => 0, 
      'remains' => 0, 
      'credit' => 0 
     ]; 

     $now = new \DateTime(); 

     foreach ($this->getOperationItems() as $opt) { 
      $optSummary = $opt->getSummary(); 

      $result['total'] += $optSummary['total']; 
      $result['paid'] += $optSummary['paid']; 
      $result['remains'] += $optSummary['remains']; 

      if ($opt->getExpectedAt()) { 
       $df = $now->diff($opt->getExpectedAt()); 

       if ($df->invert) { 
        $result['credit'] += $optSummary['remains']; 
       } 
      } 
     } 

     return $result; 
    } 

    /** 
    * @return array 
    */ 
    public function getActivitySummary($hourPrice, $forced = false) 
    { 
     if ($forced) { 
      $this->activitySummary = []; 
     } 
     if ($this->activitySummary) { 
      return $this->activitySummary; 
     } 
     $result = [ 
      'plannedHours' => 0, 
      'plannedSmens' => 0, 
      'plannedPrice' => 0, 

      'plannedHoursRemains' => 0, 
      'plannedSmensRemains' => 0, 
      'plannedPriceRemains' => 0, 

      'developHours' => 0, 
      'developSmens' => 0, 
      'developPrice' => 0, 

      'developHoursRemains' => 0, 
      'developSmensRemains' => 0, 
      'developPriceRemains' => 0, 

      'innerHours' => 0, 
      'innerSmens' => 0, 
      'innerPrice' => 0, 

      'innerHoursRemains' => 0, 
      'innerSmensRemains' => 0, 
      'innerPriceRemains' => 0, 

      'outherHours' => 0, 
      'outherSmens' => 0, 
      'outherPrice' => 0, 

      'outherHoursRemains' => 0, 
      'outherSmensRemains' => 0, 
      'outherPriceRemains' => 0, 

      'resource'  => 0 
     ]; 


     if ($this->flagGroup) { 
      $childs = $this->getChildren(); 

      if (!$childs->isEmpty()) { 
       $cnt   = $childs->count(); 
       $planSummary = 0.0; 

       foreach ($childs as $c) { 
        $planSummary += $c->getPlannedHours(); 
        $cRes = $c->getActivitySummary($hourPrice, $forced); 

        foreach ($cRes as $k => $v) { 
         $result[$k] += $v; 
        } 
       } 

       $this->setPlannedHours($planSummary); 
      } 
     } else { 
      foreach ($this->activity as $a) { 
       switch ($a->getActivity()) { 
        case TaskActivity::C_DEVELOP : 
         $result['plannedHours'] += $a->getSpend(); 
         $result['developHours'] += $a->getSpend(); 
         break; 
        case TaskActivity::C_INNER : 
         $result['plannedHours'] += $a->getSpend(); 
         $result['innerHours'] += $a->getSpend(); 
         break; 
        case TaskActivity::C_OUTHER : 
         $result['plannedHours'] += $a->getSpend(); 
         $result['outherHours'] += $a->getSpend(); 
         break; 
       } 
      } 

      $result['plannedHoursRemains'] = $this->getPlannedHours() - $result['plannedHours']; 
      $result['developHoursRemains'] = $this->getDevelopHours() - $result['developHours']; 
      $result['innerHoursRemains'] = $this->getInnerHours() - $result['innerHours']; 
      $result['outherHoursRemains'] = $this->getOutherHours() - $result['outherHours']; 
     } 

     $result['plannedSmens'] = $result['plannedHours']/8; 
     $result['developSmens'] = $result['developHours']/8; 
     $result['innerSmens'] = $result['innerHours'] /8; 
     $result['outherSmens'] = $result['outherHours']/8; 

     $result['plannedPrice'] = $result['plannedHours'] * $hourPrice; 
     $result['developPrice'] = $result['developHours'] * $hourPrice; 
     $result['innerPrice'] = $result['innerHours'] * $hourPrice; 
     $result['outherPrice'] = $result['outherHours'] * $hourPrice; 

     $result['plannedSmensRemains'] = $result['plannedHoursRemains']/8; 
     $result['developSmensRemains'] = $result['developHoursRemains']/8; 
     $result['innerSmensRemains'] = $result['innerHoursRemains'] /8; 
     $result['outherSmensRemains'] = $result['outherHoursRemains']/8; 

     $result['plannedPriceRemains'] = $result['plannedHoursRemains'] * $hourPrice; 
     $result['developPriceRemains'] = $result['developHoursRemains'] * $hourPrice; 
     $result['innerPriceRemains'] = $result['innerHoursRemains'] * $hourPrice; 
     $result['outherPriceRemains'] = $result['outherHoursRemains'] * $hourPrice; 


     if ($this->plannedHours) { 
      $result['resource'] = round(
       ($result['developHours'] + $result['innerHours'] + $result['outherHours'])/$this->plannedHours * 100 
      ); 
     } 

     $this->activitySummary = $result; 
     return $this->activitySummary; 
    } 

    /** 
    * @return string 
    */ 
    public function getResource() 
    { 
     return $this->resource; 
    } 

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

    /** 
    * @return string 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * @param string $title 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 
    } 

    /** 
    * @return bool|\DateInterval 
    */ 
    public function getTimeToDeadline() 
    { 
     return $this->deadlineTo ? $this->deadlineTo->diff(new \DateTime()) : null; 
    } 

    /** 
    * @return string 
    */ 
    public function getFlagQuickly() 
    { 
     $interval = $this->getTimeToDeadline(); 
     return ($interval && $interval->invert && $interval->days*24 < 8); 
    } 

    /** 
    * @param string $flagQuickly 
    */ 
    public function setFlagQuickly($flagQuickly) 
    { 
     $this->flagQuickly = $flagQuickly; 
    } 

    /** 
    * @return string 
    */ 
    public function getFlagBudget() 
    { 
     return $this->flagBudget; 
    } 

    /** 
    * @param string $flagBudget 
    */ 
    public function setFlagBudget($flagBudget) 
    { 
     $this->flagBudget = $flagBudget; 
    } 

    /** 
    * @param Project $project 
    * @return Task 
    */ 
    public function setProject($project) 
    { 
     $this->project = $project; 

     return $this; 
    } 

    /** 
    * @return Project 
    */ 
    public function getProject() 
    { 
     return $this->project; 
    } 

    /** 
    * @return ArrayCollection 
    */ 
    public function getChildren() 
    { 
     return $this->children; 
    } 

    /** 
    * @return \Doctrine\Common\Collections\Collection|static 
    */ 
    public function getDisplayedChildren() 
    { 
     $criteria = Criteria::create(); 
     $criteria->orderBy(['createdAt' => 'DESC']); 
     $criteria->where(Criteria::expr()->eq('parent', $this)); 
     return $this->children->matching($criteria); 
    } 

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

     return $this; 
    } 

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

    /** 
    * @param \DateTime $startTo 
    * @return Task 
    */ 
    public function setStartTo($startTo) 
    { 
     $this->startTo = $startTo; 

     return $this; 
    } 

    /** 
    * @return \DateTime 
    */ 
    public function getStartTo() 
    { 
     return $this->startTo; 
    } 

    /** 
    * @param \DateTime $deadlineTo 
    * @return Task 
    */ 
    public function setDeadlineTo($deadlineTo) 
    { 
     $this->deadlineTo = $deadlineTo; 

     return $this; 
    } 

    /** 
    * @return \DateTime 
    */ 
    public function getDeadlineTo() 
    { 
     return $this->deadlineTo; 
    } 

    /** 
    * @param string $number 
    * @return Task 
    */ 
    public function setNumber($number) 
    { 
     $this->number = $number; 

     return $this; 
    } 

    /** 
    * @return string 
    */ 
    public function getNumber() 
    { 
     return $this->number; 
    } 

    /** 
    * @param string $resource 
    * @return Task 
    */ 
    public function setResource($resource) 
    { 
     $this->resource = $resource; 

     return $this; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getEstimateRows() 
    { 
     return $this->estimateRows; 
    } 

    /** 
    * @param $estimateRow 
    */ 
    public function addEstimateRow($estimateRow) 
    { 
     $estimateRow->setTask($this); 
     $this->estimateRows->add($estimateRow); 
    } 

    /** 
    * @param $estimateRow 
    */ 
    public function removeEstimateRow($estimateRow) 
    { 
     $estimateRow->setTask(null); 
     $this->estimateRows->removeElement($estimateRow); 
    } 

    /** 
    * @param mixed $estimateRows 
    */ 
    /* 
    public function setEstimateRows($estimateRows) 
    { 
     $this->estimateRows = $estimateRows; 
    } 
    */ 

    /** 
    * @return mixed 
    */ 
    public function getState() 
    { 
     return $this->state; 
    } 

    /** 
    * @param mixed $state 
    */ 
    public function setState($state) 
    { 
     $this->state = $state; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getType() 
    { 
     return $this->type; 
    } 

    /** 
    * @param mixed $type 
    */ 
    public function setType($type) 
    { 
     $this->type = $type; 
    } 

    /** 
    * @return string 
    */ 
    public function getTags() 
    { 
     return $this->tags; 
    } 

    /** 
    * @param string $tags 
    */ 
    public function setTags($tags) 
    { 
     $this->tags = $tags; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getBonus() 
    { 
     return $this->bonus; 
    } 

    /** 
    * @param mixed $bonus 
    */ 
    public function setBonus($bonus) 
    { 
     $this->bonus = $bonus; 
    } 

    /** 
    * @return ArrayCollection 
    */ 
    public function getActivity() 
    { 
     return $this->activity; 
    } 

    /** 
    * @return ArrayCollection 
    */ 
    public function getAssignment() 
    { 
     return $this->assignment; 
    } 

    /** 
    * @return \Doctrine\Common\Collections\Collection|static 
    */ 
    public function getDisplayedActivity() 
    { 
     $criteria = Criteria::create(); 
     $criteria->orderBy(['createdAt' => 'DESC']); 
     $criteria->where(Criteria::expr()->isNull('parent')); 
     return $this->activity->matching($criteria); 
    } 

    /** 
    * @param $activity 
    */ 
    public function addActivity($activity) 
    { 
     $this->activity->add($activity); 
    } 

    /** 
    * @param $activity 
    */ 
    public function removeActivity($activity) 
    { 
     $this->activity->removeElement($activity); 
    } 

    /** 
    * @return mixed 
    */ 
    public function getDepth() 
    { 
     return $this->depth; 
    } 

    /** 
    * @param mixed $depth 
    */ 
    public function setDepth($depth) 
    { 
     $this->depth = $depth; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getPath() 
    { 
     return $this->path; 
    } 

    /** 
    * @param mixed $path 
    */ 
    public function setPath($path) 
    { 
     $this->path = $path; 
    } 

    /** 
    * @return string 
    */ 
    public function getFlagGroup() 
    { 
     return $this->flagGroup; 
    } 

    /** 
    * @param string $flagGroup 
    */ 
    public function setFlagGroup($flagGroup) 
    { 
     $this->flagGroup = $flagGroup; 
    } 

    public function getDirector() 
    { 
     return $this->project ? $this->project->getDirector() : $this->director; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getFlagArchive() 
    { 
     return $this->flagArchive; 
    } 

    /** 
    * @param mixed $flagArchive 
    */ 
    public function setFlagArchive($flagArchive) 
    { 
     $this->flagArchive = $flagArchive; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getOperationItems() 
    { 
     return $this->operationItems; 
    } 

    /** 
    * @param mixed $operations 
    */ 
    public function setOperationItems($operations) 
    { 
     $this->operationItems = $operations; 
    } 
} 
+0

給出實體和回溯的完整代碼 – mochalygin

+0

看來你在代碼中有錯誤,它會生成一個異常。你是否聲明$ this-> title屬性? – mochalygin

+0

@mochalygin是的,我做 – Kir

回答

0

你宣佈$稱號道具爲 「可空」。這意味着,它可以是NULL。 但__toString()必須始終返回字符串值。 嘗試轉換爲字符串返回值。

public function __toString() 
{ 
    return (string)$this->title; 
} 
+0

不幸的是不工作:( – Kir

0

確保功能1.因爲當時$this不存在,2 $this->title實際上有一個值或強制轉換爲字符串,因爲否則它會返回null不叫靜態明確。

/** 
* @return string 
*/ 
public function __toString() 
{ 
    return (isset($this) && $this->title !== '') 
     ? (string)$this->title 
     : ''; 
} 
+0

不幸的是不工作:( – Kir