1

I'm new to symfony and have followed this amazing tutorial to setup my bundle and and entitys using the the command line using doctrine via the console.

http://symfony.com/blog/symfony2-getting-easier-interactive-generators

the only issue is that the class I have setup has hidden fields. for example date created and date updated. This is just for my records so I can see if people have been doing things they shouldn't have.

the only issue is they are showing on the front end and I don't know how to hide them, without rewriting the code that was generated and adding the fields one by one.

Here is the function in the controller

public function createAction(Request $request)
{
    $entity = new Client();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('client_show', array('id' => $entity->getId())));
    }

    return $this->render('AcmeClientMoodBundle:Client:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

/**
* Creates a form to create a Client entity.
*
* @param Client $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Client $entity)
{      
    $form = $this->createForm(new ClientType(), $entity, array(
        'action' => $this->generateUrl('client_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

and here is the entity/class

namespace Acme\ClientMoodBundle\Entity;

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

/**
 * Client
 */
class Client
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $logo;

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

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

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

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

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


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

    /**
     * Set name
     *
     * @param string $name
     * @return Client
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set logo
     *
     * @param string $logo
     * @return Client
     */
    public function setLogo($logo)
    {
        $this->logo = $logo;

        return $this;
    }

    /**
     * Get logo
     *
     * @return string
     */
    public function getLogo()
    {
        return $this->logo;
    }

    /**
     * Set moodId
     *
     * @param integer $moodId
     * @return Client
     */
    public function setMoodId($moodId)
    {
        $this->moodId = $moodId;

        return $this;
    }

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

    /**
     * Set projectManagerId
     *
     * @param integer $projectManagerId
     * @return Client
     */
    public function setProjectManagerId($projectManagerId)
    {
        $this->projectManagerId = $projectManagerId;

        return $this;
    }

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

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Client
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

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

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return Client
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

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

    /**
     * Set active
     *
     * @param integer $active
     * @return Client
     */
    public function setActive($active)
    {
        $this->active = $active;

        return $this;
    }

How is the best way to achieve this?

1 Answer 1

2

You should look into your class ClientType and remove the fields you don't want.

Field creation looks like :

$builder->add('created');

Remove this line.

Sign up to request clarification or add additional context in comments.

4 Comments

Woo that is so easy. I love symfony! thanks for your fast response.
One last thing how would I pass the date variable to the database. EG: the below message, I will need to pass some thing like this $date->format('d-m-Y H:i:s'); 'INSERT INTO Client (name, logo, mood_id, project_manager_id, created, updated, active) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["Test", "\/logo.jpg", 1, 1, null, null, true]:
Before persisting the entity, you have to use something like $entity->setCreated(new \Datetime("now"));
I invite you to read this section of the documentation. The example is exactly what you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.