ManyToOne(Address)

This commit is contained in:
Marc Ducobu 2014-10-03 10:42:29 +02:00
parent 9f3d89380d
commit c2869edbed
17 changed files with 688 additions and 14 deletions

2
.gitignore vendored
View File

@ -21,4 +21,4 @@ app/config/parameters.yml
.DS_Store .DS_Store
*bower_components *bower_components
bin/* bin/*
/tmp/* /tmp/*

View File

@ -0,0 +1,224 @@
<?php
namespace CL\CustomFieldsBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use CL\CustomFieldsBundle\Entity\Adress;
use CL\CustomFieldsBundle\Form\AdressType;
/**
* Adress controller.
*
*/
class AdressController extends Controller
{
/**
* Lists all Adress entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('CLCustomFieldsBundle:Adress')->findAll();
return $this->render('CLCustomFieldsBundle:Adress:index.html.twig', array(
'entities' => $entities,
));
}
/**
* Creates a new Adress entity.
*
*/
public function createAction(Request $request)
{
$entity = new Adress();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('adress_show', array('id' => $entity->getId())));
}
return $this->render('CLCustomFieldsBundle:Adress:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Creates a form to create a Adress entity.
*
* @param Adress $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Adress $entity)
{
$form = $this->createForm(new AdressType(), $entity, array(
'action' => $this->generateUrl('adress_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Adress entity.
*
*/
public function newAction()
{
$entity = new Adress();
$form = $this->createCreateForm($entity);
return $this->render('CLCustomFieldsBundle:Adress:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Finds and displays a Adress entity.
*
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CLCustomFieldsBundle:Adress')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Adress entity.');
}
$deleteForm = $this->createDeleteForm($id);
return $this->render('CLCustomFieldsBundle:Adress:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing Adress entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CLCustomFieldsBundle:Adress')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Adress entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('CLCustomFieldsBundle:Adress:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Creates a form to edit a Adress entity.
*
* @param Adress $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Adress $entity)
{
$form = $this->createForm(new AdressType(), $entity, array(
'action' => $this->generateUrl('adress_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Adress entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CLCustomFieldsBundle:Adress')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Adress entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('adress_edit', array('id' => $id)));
}
return $this->render('CLCustomFieldsBundle:Adress:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a Adress entity.
*
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CLCustomFieldsBundle:Adress')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Adress entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('adress'));
}
/**
* Creates a form to delete a Adress entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('adress_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace CL\CustomFieldsBundle\Entity;
/**
* Adress
*/
class Adress
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $data;
public function __toString()
{
return $this->data;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set data
*
* @param string $data
*
* @return Adress
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get data
*
* @return string
*/
public function getData()
{
return $this->data;
}
}

View File

@ -27,6 +27,8 @@ class BlopEntity
*/ */
private $customField; private $customField;
private $adress;
/** /**
* Get id * Get id
@ -38,6 +40,23 @@ class BlopEntity
return $this->id; return $this->id;
} }
public function setAdress($a)
{
$this->adress = $a;
return $this;
}
/**
* Get field1
*
* @return string
*/
public function getAdress()
{
return $this->adress;
}
/** /**
* Set field1 * Set field1
* *

View File

@ -0,0 +1,39 @@
<?php
namespace CL\CustomFieldsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class AdressType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('data')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CL\CustomFieldsBundle\Entity\Adress'
));
}
/**
* @return string
*/
public function getName()
{
return 'cl_customfieldsbundle_adress';
}
}

View File

@ -20,6 +20,7 @@ class BlopEntityType extends AbstractType
$builder $builder
->add('field1') ->add('field1')
->add('field2') ->add('field2')
->add('adress', new AdressType())
->add('customField',new CustomFieldType($entityManager)) ->add('customField',new CustomFieldType($entityManager))
; ;
} }
@ -30,7 +31,8 @@ class BlopEntityType extends AbstractType
public function setDefaultOptions(OptionsResolverInterface $resolver) public function setDefaultOptions(OptionsResolverInterface $resolver)
{ {
$resolver->setDefaults(array( $resolver->setDefaults(array(
'data_class' => 'CL\CustomFieldsBundle\Entity\BlopEntity' 'data_class' => 'CL\CustomFieldsBundle\Entity\BlopEntity',
'cascade_validation' => true
)); ));
// supprimer ça en definissant dans services // supprimer ça en definissant dans services

View File

@ -3,16 +3,127 @@
namespace CL\CustomFieldsBundle\Form\DataTransformer; namespace CL\CustomFieldsBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Common\Persistence\ObjectManager;
class JsonCustomFieldToArrayTransformer implements DataTransformerInterface { class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
public function transform($jsonCustomField) /**
{ * @var ObjectManager
return json_decode($jsonCustomField,true); */
} private $om;
public function reverseTransform($array) /**
{ * @param ObjectManager $om
return json_encode($array); */
} public function __construct(ObjectManager $om)
} {
$this->om = $om;
$customFields = $this->om
->getRepository('CLCustomFieldsBundle:CustomField')
->findAll();
$customFieldsLablels = array_map(
function($e) { return $e->getLabel(); },
$customFields);
$customFieldsByLabel = array_combine($customFieldsLablels, $customFields);
/*
echo "<br> - - <br>";
var_dump($customFields);
echo "<br> - - <br>";
var_dump($customFieldsLablels);
echo "<br> - - <br>";
var_dump($customFieldsByLabel);
*/
$this->customField = $customFieldsByLabel;
}
public function transform($customFieldsJSON)
{
echo $customFieldsJSON;
$customFieldsArray = json_decode($customFieldsJSON,true);
/*
echo "<br> - - <br>";
var_dump($customFieldsArray);
echo "<br> - - <br>";
*/
$customFieldsArrayRet = array();
foreach ($customFieldsArray as $key => $value) {
$traited = false;
if(array_key_exists($key, $this->customField)) {
/*
echo "<br> - - - - <br>";
echo $value;
echo "<br> - - - - <br>";
*/
if($this->customField[$key]->getType() === 'ManyToOne(Address)') {
$customFieldsArrayRet[$key] = $this->om
->getRepository('CLCustomFieldsBundle:Adress')
->findOneById($value);
$traited = true;
}
}
if(! $traited) {
$customFieldsArrayRet[$key] = $value;
}
}
var_dump($customFieldsArray);
return $customFieldsArrayRet;
}
public function reverseTransform($customFieldsArray)
{
/*
echo "<br> - - - - <br>";
var_dump($customFieldsArray);
echo "<br> - - - - <br>";
*/
$customFieldsArrayRet = array();
foreach ($customFieldsArray as $key => $value) {
$traited = false;
if(array_key_exists($key, $this->customField)) {
if($this->customField[$key]->getType() === 'ManyToOne(Address)') {
$customFieldsArrayRet[$key] = $value->getId();
/*
echo "<br> - - - - <br>";
echo $value->getId();
echo "<br> - - - - <br>";
*/
$traited = true;
}
}
if(! $traited) {
$customFieldsArrayRet[$key] = $value;
}
}
var_dump($customFieldsArrayRet);
return json_encode($customFieldsArrayRet);
}
}

View File

@ -38,9 +38,17 @@ class CustomFieldType extends AbstractType
->findAll(); ->findAll();
foreach ($customFields as $cf) { foreach ($customFields as $cf) {
$builder->add($cf->getLabel(), $cf->getType()); if($cf->getType() === 'ManyToOne(Address)') {
$builder->add($cf->getLabel(), 'entity', array(
'class' => 'CLCustomFieldsBundle:Adress',
'property' => 'data',
));
} else {
$builder->add($cf->getLabel(), $cf->getType());
}
} }
$builder->addViewTransformer(new JsonCustomFieldToArrayTransformer());
$builder->addViewTransformer(new JsonCustomFieldToArrayTransformer($this->om));
} }
public function getName() public function getName()

View File

@ -0,0 +1,14 @@
CL\CustomFieldsBundle\Entity\Adress:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
data:
type: string
length: 255
lifecycleCallbacks: { }

View File

@ -16,4 +16,8 @@ CL\CustomFieldsBundle\Entity\BlopEntity:
length: 255 length: 255
customField: customField:
type: json_array type: json_array
manyToOne:
adress:
targetEntity: CL\CustomFieldsBundle\Entity\Adress
cascade: [persist]
lifecycleCallbacks: { } lifecycleCallbacks: { }

View File

@ -1,3 +1,7 @@
cl_custom_fields_adress:
resource: "@CLCustomFieldsBundle/Resources/config/routing/adress.yml"
prefix: /adress
cl_custom_fields_customfield: cl_custom_fields_customfield:
resource: "@CLCustomFieldsBundle/Resources/config/routing/customfield.yml" resource: "@CLCustomFieldsBundle/Resources/config/routing/customfield.yml"
prefix: /customfield prefix: /customfield

View File

@ -0,0 +1,30 @@
adress:
path: /
defaults: { _controller: "CLCustomFieldsBundle:Adress:index" }
adress_show:
path: /{id}/show
defaults: { _controller: "CLCustomFieldsBundle:Adress:show" }
adress_new:
path: /new
defaults: { _controller: "CLCustomFieldsBundle:Adress:new" }
adress_create:
path: /create
defaults: { _controller: "CLCustomFieldsBundle:Adress:create" }
requirements: { _method: post }
adress_edit:
path: /{id}/edit
defaults: { _controller: "CLCustomFieldsBundle:Adress:edit" }
adress_update:
path: /{id}/update
defaults: { _controller: "CLCustomFieldsBundle:Adress:update" }
requirements: { _method: post|put }
adress_delete:
path: /{id}/delete
defaults: { _controller: "CLCustomFieldsBundle:Adress:delete" }
requirements: { _method: post|delete }

View File

@ -0,0 +1,16 @@
{% extends '::base.html.twig' %}
{% block body -%}
<h1>Adress edit</h1>
{{ form(edit_form) }}
<ul class="record_actions">
<li>
<a href="{{ path('adress') }}">
Back to the list
</a>
</li>
<li>{{ form(delete_form) }}</li>
</ul>
{% endblock %}

View File

@ -0,0 +1,41 @@
{% extends '::base.html.twig' %}
{% block body -%}
<h1>Adress list</h1>
<table class="records_list">
<thead>
<tr>
<th>Id</th>
<th>Data</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td><a href="{{ path('adress_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
<td>{{ entity.data }}</td>
<td>
<ul>
<li>
<a href="{{ path('adress_show', { 'id': entity.id }) }}">show</a>
</li>
<li>
<a href="{{ path('adress_edit', { 'id': entity.id }) }}">edit</a>
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<ul>
<li>
<a href="{{ path('adress_new') }}">
Create a new entry
</a>
</li>
</ul>
{% endblock %}

View File

@ -0,0 +1,15 @@
{% extends '::base.html.twig' %}
{% block body -%}
<h1>Adress creation</h1>
{{ form(form) }}
<ul class="record_actions">
<li>
<a href="{{ path('adress') }}">
Back to the list
</a>
</li>
</ul>
{% endblock %}

View File

@ -0,0 +1,32 @@
{% extends '::base.html.twig' %}
{% block body -%}
<h1>Adress</h1>
<table class="record_properties">
<tbody>
<tr>
<th>Id</th>
<td>{{ entity.id }}</td>
</tr>
<tr>
<th>Data</th>
<td>{{ entity.data }}</td>
</tr>
</tbody>
</table>
<ul class="record_actions">
<li>
<a href="{{ path('adress') }}">
Back to the list
</a>
</li>
<li>
<a href="{{ path('adress_edit', { 'id': entity.id }) }}">
Edit
</a>
</li>
<li>{{ form(delete_form) }}</li>
</ul>
{% endblock %}

View File

@ -0,0 +1,55 @@
<?php
namespace CL\CustomFieldsBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class AdressControllerTest extends WebTestCase
{
/*
public function testCompleteScenario()
{
// Create a new client to browse the application
$client = static::createClient();
// Create a new entry in the database
$crawler = $client->request('GET', '/adress/');
$this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /adress/");
$crawler = $client->click($crawler->selectLink('Create a new entry')->link());
// Fill in the form and submit it
$form = $crawler->selectButton('Create')->form(array(
'cl_customfieldsbundle_adress[field_name]' => 'Test',
// ... other fields to fill
));
$client->submit($form);
$crawler = $client->followRedirect();
// Check data in the show view
$this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")');
// Edit the entity
$crawler = $client->click($crawler->selectLink('Edit')->link());
$form = $crawler->selectButton('Update')->form(array(
'cl_customfieldsbundle_adress[field_name]' => 'Foo',
// ... other fields to fill
));
$client->submit($form);
$crawler = $client->followRedirect();
// Check the element contains an attribute with value equals "Foo"
$this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]');
// Delete the entity
$client->submit($crawler->selectButton('Delete')->form());
$crawler = $client->followRedirect();
// Check the entity has been delete on the list
$this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
}
*/
}