0
votes

I have used CRUD to generate actions for an entity. However, I would like to modify the edit action; for instance, a user select 'inactive' represented by 1 for State then they will be shown an error message otherwise they can proceed to updating that row. Within the entity these are the variables state, paypid, startDate, endDate.

This is what the edit action currently looks like.

public function editAction($Paypid)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->find($Paypid);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Payrollperiod entity.');
    }

    $editForm = $this->createEditForm($entity);
    $deleteForm = $this->createDeleteForm($Paypid);

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );        
}

This is the payroll period entity

namespace com\twcl\agripayrollBundle\Entity;

use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert;

/** * Payrollperiod * @ORM\Entity(repositoryClass="com\twcl\agripayrollBundle\Entity\PayPeriodRepository") * * */ class Payrollperiod { /** * @var \DateTime */ private $startdate;

/**
 * @var \DateTime
 */
private $enddate;

/**
 * @var integer
 */
private $state;

/**
 * @var integer
 */
private $paypid;


/**
 * Set startdate
 *
 * @param \DateTime $startdate
 * @return Payrollperiod
 */
public function setStartdate($startdate)
{
    $this->startdate = $startdate;

    return $this;
}

/**
 * Get startdate
 *
 * @return \DateTime 
 */
public function getStartdate()
{
    return $this->startdate;
}

/**
 * Set enddate
 *
 * @param \DateTime $enddate
 * @return Payrollperiod
 */
public function setEnddate($enddate)
{
    $this->enddate = $enddate;

    return $this;
}

/**
 * Get enddate
 *
 * @return \DateTime 
 */
public function getEnddate()
{
    return $this->enddate;
}

/**
 * Set state
 *
 * @param integer $state
 * @return Payrollperiod
 */
public function setState($state)
{
    $this->state = $state;

    return $this;
}

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

/**
 * Get paypid
 *
 * @return integer 
 */
public function getPaypid()
{
    return $this->paypid;
}

public function setPayid($payid){
    $this->paypid = $payid;

} /** * Render a payrollPeriodID as a string. * * @return string */

public function __toString()
{
 return (string) $this->getstartDate()->format('d-M-y').'/'.$this->getendDate()->format('d-M-y');

}


public function __construct()
{
   $this->PayrollperiodType = new \Doctrine\Common\Collection\ArrayCollection();
}

}

Can someone assist me with this issue?

1
What is preventing you from editing your controller method to what you need it to do?Andrew Nolan
Not sure how to edit it to add an if statement when the state variable is not apart of the function. I tried this.Sue
public function editAction($Paypid) { $state="0"; if ($state=="1"){ $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')->find($Paypid);Sue
if (!$entity) { throw $this->createNotFoundException('Unable to find Payrollperiod entity.'); } $editForm = $this->createEditForm($entity); $deleteForm = $this->createDeleteForm($Paypid); return array( 'entity' => $entity, 'edit_form' => $editForm->createView(), 'delete_form' => $deleteForm->createView(), ); }else{ throw $this->createNotFoundException('You cannot edit this payroll period.'); } }Sue
and that didn't work. asking what otherway could have done it.Sue

1 Answers

0
votes

There could be multiple different ways of handling this. Normally when you generate methods using symfony or doctrine cmds such as generate:doctrine:crud, it is boilerplate code to get you going a bit faster and you will update it to meet your needs.

There are a few approaches here, but it depends on the approach and business logic you want. If you are only allowing entities that are currently in an 'Active' state to be edited, then the following will work.

You can create a custom repository class and have a method that queries for the entity based on the $Paypid provided and $state = 1.

public function findByPaypidAndActiveState($Paypid)
{
    return $this->getEntityManager()
        ->createQuery(
            'SELECT p FROM comtwclagripayrollBundle:Payrollperiod 
                WHERE paypid = :paypid AND state = :state'
        )
        ->setParameter('paypid', $Paypid)
        ->setParameter('state', 0)
        ->getResult();
}

Then in the editAction, you will query using this method instead of the using find()

Another way would be to modify the query in the editAction to find by criteria other than the id

{
    //....
    $entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')
        ->findBy(['paypid' => $Paypid, 'state' => 0]);
}

From here, you can adjust your error messaging to what you want. The throw $this->createNotFoundException('Unable to find Payrollperiod entity.'); is just boilerplate code by generating crud. This just basically returns a 404 response, which doesn't sound like what you need.

If you are using sessions, you could add a flash message to the session and display it to the user as the error message.

So overall something like this could be a possible solution

public function editAction($Paypid)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('comtwclagripayrollBundle:Payrollperiod')
        ->findBy(['paypid' => $Paypid, 'state' => 0]);

    if (!$entity) {
        $this->addFlash(
        'notice',
        'You cannot edit an inactive Payroll Period'
        );

        return $this->redirectToRoute('some_route_of_your_choosing');
    }

    $editForm = $this->createEditForm($entity);
    $deleteForm = $this->createDeleteForm($Paypid);

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );        
}