Symfony2:将IP显示为字符串并将IP保存为INT

时间:2012-07-10 08:38:22

标签: symfony doctrine-orm ip

我在Symfony中有一个名为Ip的实体,我将我的IP地址保存为整数 - 我也使用IP作为主键。

但是当我在表单或列表中显示并输入IP时,我想将其转换为IP,例如127.0.0.1保存为2130706433。

我使用CRUD生成器创建了表单。

我的实体来到这里:

<?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->clearance = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Set ip
 *
 * @param bigint $ip
 */
public function setIp($ip)
{
    $this->ip = $ip;
}


/**
 * Get ip
 *
 * @return bigint
 */
public function getIp()
{
    return $this->ip;
}


/**
 * Set high
 *
 * @param integer $high
 */
public function setHigh($high)
{
    $this->high = $high;
}

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

/**
 * Set hoster
 *
 * @param string $hoster
 */
public function setHoster($hoster)
{
    $this->hoster = $hoster;
}

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

/**
 * Set scandate
 *
 * @param datetime $scandate
 */
public function setScandate($scandate)
{
    $this->scandate = $scandate;
}

/**
 * Get scandate
 *
 * @return datetime
 */
public function getScandate()
{
    return $this->scandate;
}

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

/**
 * Add clearance
 *
 * @param IS\ClearanceBundle\Entity\Clearance $clearance
 */
public function addClearance(\IS\ClearanceBundle\Entity\Clearance $clearance)
{
    $this->clearance[] = $clearance;
}

/**
 * Get clearance
 *
 * @return Doctrine\Common\Collections\Collection
 */
public function getClearance()
{
    return $this->clearance;
}
}

这是我的控制器:

<?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->getDoctrine()->getEntityManager();

    $entities = $em->getRepository('ISClearanceBundle:Ip')->findAll();

    return array('entities' => $entities);
}

/**
 * Finds and displays a Ip entity.
 *
 * @Route("/{id}/show", name="ip_show")
 * @Template()
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('ISClearanceBundle:Ip')->find($id);

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

    $deleteForm = $this->createDeleteForm($id);

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

/**
 * Displays a form to create a new Ip entity.
 *
 * @Route("/new", name="ip_new")
 * @Template()
 */
public function newAction()
{
    $entity = new Ip();
    $form   = $this->createForm(new IpType(), $entity);

    return array(
        'entity' => $entity,
        'form'   => $form->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->getRequest();
    $form    = $this->createForm(new IpType(), $entity);
    $form->bindRequest($request);

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

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

    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView()
    );
}

/**
 * Displays a form to edit an existing Ip entity.
 *
 * @Route("/{id}/edit", name="ip_edit")
 * @Template()
 */
public function editAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('ISClearanceBundle:Ip')->find($id);

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

    $editForm = $this->createForm(new IpType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->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->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('ISClearanceBundle:Ip')->find($id);

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

    $editForm   = $this->createForm(new IpType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    $request = $this->getRequest();

    $editForm->bindRequest($request);

    if ($editForm->isValid()) {
        $em->persist($entity);
        $em->flush();

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

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

/**
 * Deletes a Ip entity.
 *
 * @Route("/{id}/delete", name="ip_delete")
 * @Method("post")
 */
public function deleteAction($id)
{
    $form = $this->createDeleteForm($id);
    $request = $this->getRequest();

    $form->bindRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('ISClearanceBundle:Ip')->find($id);

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

        $em->remove($entity);
        $em->flush();
    }

    return $this->redirect($this->generateUrl('ip'));
}

private function createDeleteForm($id)
{
    return $this->createFormBuilder(array('id' => $id))
        ->add('id', 'hidden')
        ->getForm()
    ;
}
}

这里有表格:

 <?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
        ->add('ip')
        ->add('high')
        ->add('hoster')
        ->add('scandate')
        ->add('clearance','entity', array('class'=>'IS\ClearanceBundle\Entity\Clearance', 'property'=>'id','required'=>false, 'multiple'=>true))
    ;
}

public function getName()
{
    return 'is_clearancebundle_iptype';
}
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

IMO如果你想这样做,你应该使用数据转换器http://symfony.com/doc/current/cookbook/form/data_transformers.html并为你的实体IP实现__toString。

编辑:

在要点上创建样本:https://gist.github.com/3086241