错误:方法__toString()不能抛出异常symfony2

时间:2017-04-13 12:16:21

标签: php symfony tostring

我正在使用Symfony2,我遇到了一个错误:

  

错误:方法代理_CG __ \ Sprint \ SiteBundle \ Entity \ Task :: __ toString()不得抛出异常

以下是我Entity\Task.php中的此代码:

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

主要问题,在我的本地版本的网站上一切都没问题,我只在我的实际网站上有这个错误。但他们是相似的!

这是我的Entity \ 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;
    }
}

2 个答案:

答案 0 :(得分:0)

你宣称$ title prop为&#34; nullable&#34;。这意味着,它可以是NULL。 但是__toString()必须始终返回字符串值。 尝试强制转换为字符串返回值。

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

答案 1 :(得分:0)

确保函数1.没有显式静态调用,因为$this不存在而且2. $this->title实际上有一个值或被转换为字符串,否则它将返回{{1} }。

null