Search code examples
doctrinesymfonyentitymanager

Symfony 3 Notice: Object of class Doctrine\ORM\EntityManager could not be converted to int


I am new with Symfony 3 and I am implementing a simple web application. I'm trying to get data from FORM but when get the request and put data in Entity manager instance I got a error, let me explain with code:

This is the Controller (DefaultController)

namespace Database\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Database\TestBundle\Entity\Products;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class DefaultController extends Controller
{
  public function addAction(Request $request)
  {
    $product = new Products();

    $form = $this->createFormBuilder($product)
                 ->add('name', TextType::class)
                 ->add('price', TextType::class)
                 ->add('description', TextareaType::class)
                 ->add('save', SubmitType::class, array('label' => 'Save Product'))
                 ->getForm();

    $form->handleRequest($request);

    if($form->isValid())
    {
        $product = $form->getData();

        $em = $this->getDoctrine()->getManager();
        $em->persist($product);
        $em-flush();  //I got the error in this line

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

    return $this->render('DatabaseTestBundle:Default:add.html.twig', array(
    'form' => $form->createView()));
 }
}

This is my Entity (Products)

namespace Database\TestBundle\Entity;

/**
* Products
*/
class Products
{
/**
 * @var int
 */
private $id;

/**
 * @var string
 */
private $name;

/**
 * @var int
 */
private $price;

/**
 * @var string
 */
private $description;

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

/**
 * Set name
 *
 * @param string $name
 *
 * @return Products
 */
public function setName($name)
{
    $this->name= $name;

    return $this;
}

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

/**
 * Set price
 *
 * @param integer $price
 *
 * @return Products
 */
public function setPrice($price)
{
    $this->price= $price;

    return $this;
}

/**
 * Get price
 *
 * @return int
 */
public function getPrice()
{
    return $this->price;
}

/**
 * Set description
 *
 * @param string $description
 *
 * @return Products
 */
public function setDescription($description)
{
    $this->description= $description;

    return $this;
}

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

This is my View (add)

{% extends '::frontend.html.twig' %}
{% block title %}List of Products{% endblock %}

{% block body %}
<h1 class="clase">Add Product</h1>
<hr>

<a href="{{asset('test/list')}}" class="btn btn-warning" title="Return to list">Return to list</a>

<br/>
<br/>

{{ form_start(form, {'attr': {'class': 'form-horizontal'}}) }}
<div class="row">
    <div class="col-sm-12">
        <div class="form-group">
            <label class="col-sm-1 control-label required" for="form_name">Name</label>
            <div class="col-sm-3">
                {{form_widget(form.name, {'attr': {'class': 'form-control col-md-12'}})}}
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-1 control-label required" for="form_price">Price</label>
            <div class="col-sm-3">
                {{form_widget(form.price, {'attr': {'class': 'form-control col-md-12'}})}}
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-1 control-label required" for="form_description">Description</label>
            <div class="col-sm-3">
                {{form_widget(form.description, {'attr': {'class': 'form-control col-md-12'}})}}
            </div>
        </div>

        <hr/>
        {{form_widget(form.save, {'attr': {'class': 'btn btn-default'}})}}
        </p>

    </div>
</div>
{{ form_end(form) }}

 {% endblock %}

And this is the Error Message:

Stack Trace

in src\Database\TestBundle\Controller\DefaultController.php at line 48  

46     $em = $this->container->get('doctrine')->getManager();
47     $em->persist($product);
48     $em-flush();
49     return $this->redirect($this->generateUrl('database_test_list'));

 at ErrorHandler ->handleError ('8', 'Object of class Doctrine\ORM\EntityManager could not be converted to int', 
'C:\xampp\htdocs\taller_symfony\src\Database\TestBundle\Controller\DefaultController.php', '48', array('request' => object(Request), 'producto' => object(Productos), 'form' => object(Form), 'em' => object(EntityManager)))
 in src\Database\TestBundle\Controller\DefaultController.php at line 48

Somebody has any idea regarding this error?? Anyone can give me a hand with this please, thanks a lot.


Solution

  • You have syntax error, which is > sign missing.

    You have:

    $em-flush();
    

    while it should be:

    $em->flush();
    

    PHP didn't throw syntax error because it's actually correct PHP syntax, but not the one that you were expected it to be. You tried to make arithmetical (subtraction) operation on object.