0

我在 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';
}
}

谢谢你的帮助!

4

1 回答 1

3

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

编辑:

在 gist 创建示例:https ://gist.github.com/3086241

于 2012-07-10T08:46:40.587 回答