title: Error: Call to a member function setPayrollperiodid() on string
body: <p>I'm having an error, when I try to create a record it gives me this error, Error: Call to a member function setPayrollperiodid() on string in C:\wamp64\www\AgripayrollsystemV2\AgriPayrollSystem\src\com\twcl\agripayrollBundle\Controller\PayrollperiodController.php. This line: ** $pWeek->setPayrollperiodid($entity);** </p>
  
  <p><strong>Entity</strong></p>
  
  <pre><code>    &lt;?php
  
  namespace com\twcl\agripayrollBundle\Entity;
  
  
  //use Doctrine\Common\Collections\ArrayCollection;
  use Symfony\Component\Validator\Constraints as Assert;
  
  
  /**
   * Payrollperiod
   *
  
   */
  class Payrollperiod
  {
      /**
       * @var integer
       *
  
       */
      private $payrollperiodid;
  
      /**
       * @var \DateTime
       *
  
       * @Assert\DateTime()
       */
  
      private $startdate;
  
      /**
       * @var \DateTime
       *
       * 
       * @Assert\Type("DateTime")
       * message="The end date must be after the start date")
       */
      private $enddate;
  
      /**
       * @var integer
       *
  
       * 
       */
      private $state;
  
      public function getPayrollperiodid() {
          return $this-&gt;payrollperiodid;
      }
  
      public function getStartdate() {
          return $this-&gt;startdate;
      }
  
      public function getEnddate() {
          return $this-&gt;enddate;
      }
  
      public function getState() {
          return $this-&gt;state;
      }
  
      public function setPayrollperiodid($payrollperiodid) {
          $this-&gt;payrollperiodid = $payrollperiodid;
      }
  
      public function setStartdate(\DateTime $startdate) {
          $this-&gt;startdate = $startdate;
      }
  
      public function setEnddate(\DateTime $enddate) {
          $this-&gt;enddate = $enddate;
      }
  
      public function setState($state) {
          $this-&gt;state = $state;
      }
  
      /**
       * Render a payrollPeriodID as a string.
       * 
       * @return string
       */
       public function __toString()
      {
       return (string) $this-&gt;getPayrollperiodid();
  
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>  public function createAction(Request $request)
      {
  
      $entity = new Payrollperiod();
  
      $form = $this-&gt;createCreateForm($entity);
  
      $form-&gt;handleRequest($request);
  
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) { 
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $payrollperiod = $em-&gt;getRepository('comtwclagripayrollBundle:Payrollperiod')-&gt;findOneBy(['startdate'=&gt;$form-&gt;get('startdate')-&gt;getData(), 'enddate'=&gt;$form-&gt;get('enddate')-&gt;getData()]);
  
          $payrollweek = $em-&gt;getRepository('comtwclagripayrollBundle:Payrollweek')-&gt;findBystartdateAndenddate($form-&gt;get('startdate')-&gt;getData(), $form-&gt;get('enddate')-&gt;getData());
  
          //If entity exists
          if ($payrollperiod){
                  $this-&gt;addFlash('error', 'Payroll Period exist.');
                  return $this-&gt;redirect($this-&gt;generateUrl('payrollperiod'));
              }
              //If PayrollWeek matches the Payrollperiod entered
             elseif ($payrollweek){
  
              foreach($payrollweek as $pWeek) { 
              **$pWeek-&gt;setPayrollperiodid($entity);** 
              //$em-&gt;persist($pWeek);
              } 
  
            $em-&gt;persist($entity);
            $em-&gt;flush();
  
              $this-&gt;addFlash('error', 'Payroll Period was added.');
              $this-&gt;addFlash('error', 'Payroll week was updated.');
              return $this-&gt;redirect($this-&gt;generateUrl('payrollperiod'));
             //return $this-&gt;redirect($this-&gt;generateUrl('payrollperiod_show', array('payrollperiodid' =&gt; $entity-&gt;getpayrollperiodid())));
               }
               else{
                 $this-&gt;addFlash('error', 'Payroll Period does not match a payroll week.');
              return $this-&gt;redirect($this-&gt;generateUrl('payrollperiod'));  
               }
         }
  
         //return $this-&gt;render('comtwclagripayrollBundle::new.html.twig',array('form' =&gt; $form-&gt;createView()));
         return array(
             'entity' =&gt; $entity,
             'form'   =&gt; $form-&gt;createView(),
         );   
  
          }
  </code></pre>
  
  <p><strong>Custom Repository</strong></p>
  
  <pre><code> return $this
          -&gt;createQueryBuilder('payrollweek')
           -&gt;select('COUNT(pw)')
           -&gt;from ('comtwclagripayrollBundle:Payrollweek','pw')
           -&gt;where ('pw.startdate = :startdate and pw.enddate = :enddate')
          -&gt;setParameter('startdate', $startdate)
          -&gt;setParameter('enddate', $enddate)
          -&gt;getQuery()
          -&gt;getOneorNullResult();
  }
  </code></pre>