title: Symfony2: Show IP as string and save IP as INT
body: <p>I have an Entity in Symfony called Ip and I save my IP address as integer - I use the IP as primary key, too.</p>
  
  <p>But when I display and enter the IP in a form or list I want to convert it to a IP, e.g. 127.0.0.1 is saved as 2130706433.</p>
  
  <p>I created the forms with the CRUD generator.</p>
  
  <p>My entity comes here:</p>
  
  <pre><code>&lt;?php
  
  namespace IS\ClearanceBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * IS\ClearanceBundle\Entity\Ip
  */
  class Ip
  {
  /**
   * @var bigint $ip
   */
  private $ip;
  
  /**
   * @var integer $high
   */
  private $high;
  
  /**
   * @var string $hoster
   */
  private $hoster;
  
  /**
   * @var datetime $scandate
   */
  private $scandate;
  
  /**
   * @var integer $id
   */
  private $id;
  
  /**
   * @var IS\ClearanceBundle\Entity\Clearance
   */
  private $clearance;
  
  public function __construct()
  {
      $this-&gt;clearance = new \Doctrine\Common\Collections\ArrayCollection();
  }
  
  /**
   * Set ip
   *
   * @param bigint $ip
   */
  public function setIp($ip)
  {
      $this-&gt;ip = $ip;
  }
  
  
  /**
   * Get ip
   *
   * @return bigint
   */
  public function getIp()
  {
      return $this-&gt;ip;
  }
  
  
  /**
   * Set high
   *
   * @param integer $high
   */
  public function setHigh($high)
  {
      $this-&gt;high = $high;
  }
  
  /**
   * Get high
   *
   * @return integer
   */
  public function getHigh()
  {
      return $this-&gt;high;
  }
  
  /**
   * Set hoster
   *
   * @param string $hoster
   */
  public function setHoster($hoster)
  {
      $this-&gt;hoster = $hoster;
  }
  
  /**
   * Get hoster
   *
   * @return string
   */
  public function getHoster()
  {
      return $this-&gt;hoster;
  }
  
  /**
   * Set scandate
   *
   * @param datetime $scandate
   */
  public function setScandate($scandate)
  {
      $this-&gt;scandate = $scandate;
  }
  
  /**
   * Get scandate
   *
   * @return datetime
   */
  public function getScandate()
  {
      return $this-&gt;scandate;
  }
  
  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Add clearance
   *
   * @param IS\ClearanceBundle\Entity\Clearance $clearance
   */
  public function addClearance(\IS\ClearanceBundle\Entity\Clearance $clearance)
  {
      $this-&gt;clearance[] = $clearance;
  }
  
  /**
   * Get clearance
   *
   * @return Doctrine\Common\Collections\Collection
   */
  public function getClearance()
  {
      return $this-&gt;clearance;
  }
  }
  </code></pre>
  
  <p>And here is my Controller:</p>
  
  <pre><code>&lt;?php
  
  namespace IS\ClearanceBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use IS\ClearanceBundle\Entity\Ip;
  use IS\ClearanceBundle\Form\IpType;
  
  /**
  * Ip controller.
  *
  * @Route("/ip")
  */
  class IpController extends Controller
  {
  /**
   * Lists all Ip entities.
   *
   * @Route("/", name="ip")
   * @Template()
   */
  public function indexAction()
  {
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
      $entities = $em-&gt;getRepository('ISClearanceBundle:Ip')-&gt;findAll();
  
      return array('entities' =&gt; $entities);
  }
  
  /**
   * Finds and displays a Ip entity.
   *
   * @Route("/{id}/show", name="ip_show")
   * @Template()
   */
  public function showAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
      $entity = $em-&gt;getRepository('ISClearanceBundle:Ip')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Ip entity.');
      }
  
      $deleteForm = $this-&gt;createDeleteForm($id);
  
      return array(
          'entity'      =&gt; $entity,
          'delete_form' =&gt; $deleteForm-&gt;createView(),        );
  }
  
  /**
   * Displays a form to create a new Ip entity.
   *
   * @Route("/new", name="ip_new")
   * @Template()
   */
  public function newAction()
  {
      $entity = new Ip();
      $form   = $this-&gt;createForm(new IpType(), $entity);
  
      return array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView()
      );
  }
  
  /**
   * Creates a new Ip entity.
   *
   * @Route("/create", name="ip_create")
   * @Method("post")
   * @Template("ISClearanceBundle:Ip:new.html.twig")
   */
  public function createAction()
  {
      $entity  = new Ip();
      $request = $this-&gt;getRequest();
      $form    = $this-&gt;createForm(new IpType(), $entity);
      $form-&gt;bindRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('ip_show', array('id' =&gt; $                                                                            entity-&gt;getId())));
  
      }
  
      return array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView()
      );
  }
  
  /**
   * Displays a form to edit an existing Ip entity.
   *
   * @Route("/{id}/edit", name="ip_edit")
   * @Template()
   */
  public function editAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
      $entity = $em-&gt;getRepository('ISClearanceBundle:Ip')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Ip entity.');
      }
  
      $editForm = $this-&gt;createForm(new IpType(), $entity);
      $deleteForm = $this-&gt;createDeleteForm($id);
  
      return array(
          'entity'      =&gt; $entity,
          'edit_form'   =&gt; $editForm-&gt;createView(),
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      );
  }
  
  /**
   * Edits an existing Ip entity.
   *
   * @Route("/{id}/update", name="ip_update")
   * @Method("post")
   * @Template("ISClearanceBundle:Ip:edit.html.twig")
   */
  public function updateAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
      $entity = $em-&gt;getRepository('ISClearanceBundle:Ip')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Ip entity.');
      }
  
      $editForm   = $this-&gt;createForm(new IpType(), $entity);
      $deleteForm = $this-&gt;createDeleteForm($id);
  
      $request = $this-&gt;getRequest();
  
      $editForm-&gt;bindRequest($request);
  
      if ($editForm-&gt;isValid()) {
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('ip_edit', array('id' =&gt; $                                                                            id)));
      }
  
      return array(
          'entity'      =&gt; $entity,
          'edit_form'   =&gt; $editForm-&gt;createView(),
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      );
  }
  
  /**
   * Deletes a Ip entity.
   *
   * @Route("/{id}/delete", name="ip_delete")
   * @Method("post")
   */
  public function deleteAction($id)
  {
      $form = $this-&gt;createDeleteForm($id);
      $request = $this-&gt;getRequest();
  
      $form-&gt;bindRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $entity = $em-&gt;getRepository('ISClearanceBundle:Ip')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Ip entity.'                                                                            );
          }
  
          $em-&gt;remove($entity);
          $em-&gt;flush();
      }
  
      return $this-&gt;redirect($this-&gt;generateUrl('ip'));
  }
  
  private function createDeleteForm($id)
  {
      return $this-&gt;createFormBuilder(array('id' =&gt; $id))
          -&gt;add('id', 'hidden')
          -&gt;getForm()
      ;
  }
  }
  </code></pre>
  
  <p>And here the form:</p>
  
  <pre><code> &lt;?php
  
   namespace IS\ClearanceBundle\Form;
  
   use Symfony\Component\Form\AbstractType;
   use Symfony\Component\Form\FormBuilder;
  
   class IpType extends AbstractType
   {
   public function buildForm(FormBuilder $builder, array $options)
   {
      $builder
          -&gt;add('ip')
          -&gt;add('high')
          -&gt;add('hoster')
          -&gt;add('scandate')
          -&gt;add('clearance','entity', array('class'=&gt;'IS\ClearanceBundle\Entity\Clearance', 'property'=&gt;'id','required'=&gt;false, 'multiple'=&gt;true))
      ;
  }
  
  public function getName()
  {
      return 'is_clearancebundle_iptype';
  }
  }
  </code></pre>
  
  <p>Thanks for any help!</p>