ContextErrorException:注意:未定义的变量?

I got a problem with my code it says : ContextErrorException: Notice: Undefined variable: tpId in /tmxpage/apache/htdocsEDI/Editracker/src/Matrix/MatrixEdiBundle/Controller/MatrixController.php line 435 , im new to symfony and I don't know why,.

here is my code for MatrixController.php :

public function checkDocumentAction($docType, $direction, $senderId, $receiverId) {
    $response = 0;
    $em =$this->getDoctrine()->getManager();
    $temp = $em
        ->getRepository('MatrixEdiBundle:EdiInterchangeId')
        ->findInterchangeId($senderId);
    $countTemp = count($temp);
    if($temp != null) {
      if($countTemp == 1) {
        foreach($temp as $key) {
          $tpId = $key->getEdiTradingPartner();
        }
      } else {
        $temp1 = $em
          ->getRepository('MatrixEdiBundle:EdiInterchangeId')
          ->findInterchangeId($receiverId);
        $countTemp1 = count($temp1);
        if($temp1 != null) {
          if($countTemp1 == 1) {
            foreach($temp1 as $key) {
              $tpId = $key->getEdiTradingPartner();
            }
          } elseif($countTemp1 > 1) {
            foreach($temp1 as $key) {
              $temp2 = $key->getEdiTradingPartner();
              $temp3 = $em
                  ->getRepository('MatrixEdiBundle:EdiInterchangeId')
                  ->findTradingPartner($temp2, $senderId);
              $countTemp3 = count($temp3);
              if($countTemp3 == 1) {
                foreach($temp3 as $key) {
                  $tpId = $key->getEdiTradingPartner();
                }
              }
            }
          }
        }
      }

      if ($tpId != null) {
        $result = $em
            ->getRepository('MatrixEdiBundle:EdiTradingPartnerTransactions')
            ->getTpTrans($tpId, $docType, $direction);
        // if ($result != null) {
        //   $response = 1;
        // }
        if ($result != null) {
          foreach ($result as $key) {
            $isRequired = $key->getIsRequired();
            if ($isRequired == 1) {
              $response = 1;
            } else {
              $response = 2;
            }
          }
        }
      }
    }

    return new Response($response);
}

it's a function inside the matrixcontroller.php

Here is my snippet for rejectedTrans.html.twig where the tpId is rendered, coz it also says :An exception has been thrown during the rendering of a template ("Notice: Undefined variable: tpId, :

{% if transaction != null %}
    {% for trans in transaction %}
      <tr>
        <td style="width: 8%;">
          {{ render(controller('MatrixEdiBundle:Matrix:getTradingPartnerName', {
              'timexID' : trans.ediTransaction.receiverId,
              'customerID' : trans.ediTransaction.senderId
          })) }}
        </td>
        {% set result=render(controller('MatrixEdiBundle:Matrix:getFile', {
            'fileName' : trans.ediTransaction.fileName,
            'senderId': trans.ediTransaction.senderId ,
            'receiverId' : trans.ediTransaction.receiverId,
            'gsNumber' : trans.ediTransaction
        }))|split('+', 4) %}

$tpId initializes only in conditions. So if all conditions is false, variable will not be defined.

Initialize it with null

public function checkDocumentAction($docType, $direction, $senderId, $receiverId) {
    $response = 0;
    $tpId = null;

...