I don't know why im getting this error every time when I wanna save a new model into my collection. My route is matched but the error appears everytime. I have a table with id, title, document and user_id. I just wanna save my title. Can anyone help me?
Error: Failed to start the session because headers have already been sent by "" at line 0.
PHP code:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use AppBundle\Entity\Document;
use Doctrine\ORM\Mapping as ORM;
class DocumentQueryController extends Controller
{
public function documentAction()
{
$id = $this->getUser()->getId();
$em = $this->getDoctrine()->getManager();
$documents = $em->getRepository('AppBundle:Document')->findAllOrderedById();
$titles = array();
foreach($documents as $document)
{
$user_id = $document['user_id'];
$title = $document['title'];
if($user_id == $id)
{
$titles[] = $document;
}
}
return new JsonResponse($titles);
//return new JsonResponse($document);
//return new JsonResponse($documents);
//dump(json_encode($documents));
//return $this->render('default/doc.html.twig', array('documents' => $documents, 'id' => $id));
}
public function createAction(Request $request)
{
$content = $this->get("request")->getContent();
$json = json_decode($content);
$id = $this->getUser()->getId();
$document = new Document();
$user_id = $id;
$document->setTitle($request->get('title'));
$document->setUserId($user_id);
//$document->setBody($content);
$em = $this->getDoctrine()->getManager();
$em->persist($document);
$em->flush();
return new JsonResponse($document);
}
}