Использование TBBC Money / Currency Bundle для Symfony 2.8

Я пытаюсь реализовать TBBC Money / Currency Bundle в моем приложении, но это не работает. Я думаю, что проблема исходит от моего контроллера и файла ветки. Я немного растерялся с документацией этого комплекта.

Любая помощь будет оценена!

Это мой config.yml

tbbc_money:
currencies: ["USD", "EUR"]
reference_currency: "EUR"decimals: 2
enable_pair_history: true
ratio_provider: tbbc_money.ratio_provider.google
twig:
debug:            "%kernel.debug%"strict_variables: "%kernel.debug%"form:
resources:
- 'TbbcMoneyBundle:Form:fields.html.twig'

post.php

/**
* @var integer
*
* @ORM\Column(name="price_amount", type="integer")
*/
private $priceAmount;

/**
* @var string
*
* @ORM\Column(name="price_currency", type="string", length=64)
*/
private $priceCurrency;

/**
* get Money
*
* @return Money
*/
public function getPrice()
{
if (!$this->priceCurrency) {
return null;
}
if (!$this->priceAmount) {
return new Money(0, new Currency($this->priceCurrency));
}
return new Money($this->priceAmount, new Currency($this->priceCurrency));
}

/**
* Set price
*
* @param Money $price
* @return Post
*/
public function setPrice(Money $price)
{
$this->priceAmount = $price->getAmount();
$this->priceCurrency = $price->getCurrency()->getName();

return $this;
}

PostType.php

->add('price', 'tbbc_money')

new.html.twig

(Я уверен, что эта часть кода не так, я должен заменить его на что-то еще ??)

 <div class="form-group form-group-lg form-group-icon-left">
<i class="fa fa-plane input-icon"></i>
<label>price</label>
{{ form_widget(form.price, { 'attr': {'class': 'form-control',} }) }}
{{ form_errors(form.price) }}

</div>

PostController.php

(Я не знаю, что именно я должен вставить в свой контроллер.)

<?php

namespace FLY\BookingsBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
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 FLY\BookingsBundle\Entity\Post;
use FLY\BookingsBundle\Form\PostType;

/**
* Post controller.
*
* @Route("/post")
*/
class PostController extends Controller
{

/**
* Lists all Post entities.
*
* @Route("/", name="post")
* @Method("GET")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();

$findEntities = $em->getRepository('FLYBookingsBundle:Post')->findAll();
$entities  = $this->get('knp_paginator')->paginate($findEntities,$this->get('request')->query->get('page', 1),9
);
return array(
'entities' => $entities,
);
}

/**
* Creates a new Post entity.
*
* @Route("/", name="post_create")
* @Method("POST")
* @Template("FLYBookingsBundle:Post:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Post();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);

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

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

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

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

/**
* Displays a form to create a new Post entity.
*
* @Route("/new", name="post_new")
* @Method("GET")
* @Template()
*/
public function newAction()
{
$entity = new Post();
$form   = $this->createCreateForm($entity);

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

/**
* Finds and displays a Post entity.
*
* @Route("/{id}", name="post_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();

$entity = $em->getRepository('FLYBookingsBundle:Post')->find($id);

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

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

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

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

$entity = $em->getRepository('FLYBookingsBundle:Post')->find($id);

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

$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);

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

/**
* Creates a form to edit a Post entity.
*
* @param Post $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Post $entity)
{
$form = $this->createForm(new PostType(), $entity, array(
'action' => $this->generateUrl('post_update', array('id' => $entity->getId())),
'method' => 'PUT',
));

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

return $form;
}
/**
* Edits an existing Post entity.
*
* @Route("/{id}", name="post_update")
* @Method("PUT")
* @Template("FLYBookingsBundle:Post:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();

$entity = $em->getRepository('FLYBookingsBundle:Post')->find($id);

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

$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);

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

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

return array(
'entity'      => $entity,
'edit_form'   => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Post entity.
*
* @Route("/{id}", name="post_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);

if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('FLYBookingsBundle:Post')->find($id);

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

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

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

/**
* Creates a form to delete a Post 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('post_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}

}

РЕДАКТИРОВАТЬ:

у меня было это в моем контроллере:

 public function myAction()
{
$moneyFormatter = $this->get('tbbc_money.formatter.money_formatter');
$price = new Money(123456789, new Currency('EUR'));

// best method (added in 2.2+ version)
\Locale::setDefault('fr_FR');
$formatedPrice = $moneyFormatter->localizedFormatMoney($price);
// 1 234 567,89 €
$formatedPrice = $moneyFormatter->localizedFormatMoney($price, 'en');
// €1,234,567.89

// old method (before v2.2)
$formattedPrice = $moneyFormatter->formatMoney($price);
// 1 234 567,89

$formattedCurrency = $moneyFormatter->formatCurrency($price);
// €
}

тогда я получил эту ошибку:

Исключение было сгенерировано во время рендеринга шаблона.
(«Catchable Fatal Error: объект класса Money \ Money не может быть
преобразован в строку «) в
src \ FLY \ BookingsBundle \ Resources \ views \ Post \ show.html.twig в строке 30.

Это моя строка 30 в show.html.twig

<span class="price"><small>Price</small>${{ entity.price }}</span>

2

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]