title: Angularjs ajax request in Symfony2 and Doctrine json response with relationships
body: <p>I am trying to work with Symfony2, Doctrine and Angujarjs. There is no problem with Symfony2 or Doctrine but I have issues using an ajax request with angularjs. Either it doesn't load anything and I did make a mistake while loading the json (json comes from Symfony and its working) or if it's working, but the json doesn't contain any of the relationship's data.</p>
  
  <p>So, what's the correct way to
  A: create a response for angularjs with relationship data (such as articles and categories)
  B: load the requested json into an angularjs variable</p>
  
  <p>Here is my controller.js </p>
  
  <pre><code>var app = angular.module("MyApp", []) .config(['$interpolateProvider', function ($interpolateProvider) {
  
      $interpolateProvider.startSymbol('[[');
      $interpolateProvider.endSymbol(']]');
  
  }]);
  
  app.filter('offset', function() {
  
      return function(input, start) {
          start = parseInt(start, 10);
          return input.slice(start);    
      };
  
  });
  
  app.filter('htmlToPlaintext', function() {
  
      return function(text) {
          return String(text).replace(/&lt;[^&gt;]+&gt;/gm, '');
      };
  
  });
  
  app.controller("PaginationCtrl", function($scope, $http) {
  
      $scope.articlesPerPage = 8;
      $scope.currentPage = 0;
  
          function htmlToPlaintext(text) {
              return String(text).replace(/&lt;[^&gt;]+&gt;/gm, '');
          }
  
      // this should load the json from '/admin/jsonallarticles' into the articles variable
      $http.get('/admin/jsonallarticles').success(function(data) {
          $scope.articles = data;
      });
  
      $scope.range = function() {
          var rangeSize = 5;
          var ret = [];
          var start;
          start = $scope.currentPage;
  
          if ( start &gt; $scope.pageCount()-rangeSize ) {
              start = $scope.pageCount()-rangeSize+1;
          }
  
          for (var i=start; i&lt;start+rangeSize; i++) {
              ret.push(i);
          }
  
          return ret;
      };
  
      $scope.prevPage = function() {
          if ($scope.currentPage &gt; 0) {
          $scope.currentPage--;
          }
      };
  
      $scope.prevPageDisabled = function() {
          return $scope.currentPage === 0 ? "disabled" : "";
      };
  
      $scope.pageCount = function() {
          return Math.ceil($scope.articles.length/$scope.articlesPerPage)-1;
      };
  
      $scope.nextPage = function() {
          if ($scope.currentPage &lt; $scope.pageCount()) {
              $scope.currentPage++;
          }
      };
  
      $scope.nextPageDisabled = function() {
          return $scope.currentPage === $scope.pageCount() ? "disabled" : "";
      };
  
      $scope.setPage = function(n) {
      $scope.currentPage = n;
      };
  
  });
  </code></pre>
  
  <p>This is my symfony2 function</p>
  
  <pre><code> public function jsonallarticlesAction() {
  
      $articles = $this-&gt;getDoctrine()
          -&gt;getRepository('AcmeBlogBundle:Articles');
  
      if ( !$articles ) {
           throw $this-&gt;createNotFoundException(
                   'Keine Beiträge gefunden!');
       }
  
     $queryArticles = $articles-&gt;createQueryBuilder('a')
              -&gt;where('a.status = :status')
              -&gt;setParameter('status', 0)
              -&gt;orderBy('a.createdDate', 'DESC')
              -&gt;getQuery()
              -&gt;getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);;
  
      $articles = array_values($queryArticles);   
  
      $response = new Response();
      $response-&gt;setContent(json_encode($articles));
  
      $response-&gt;headers-&gt;set('Content-Type', 'application/json');
  
      return $response;
  
   }
  </code></pre>
  
  <p><strong>EDITED CONTROLLER</strong></p>
  
  <p>I tried using the serializer which comes with Symfony</p>
  
  <pre><code>    $encoders = array(new XmlEncoder(), new JsonEncoder());
      $normalizers = array(new GetSetMethodNormalizer());
  
      $serializer = new Serializer($normalizers, $encoders);
  
      $articles = $this-&gt;getDoctrine()
              -&gt;getRepository('AcmeBlogBundle:Articles')
              -&gt;findAll();
  
      if ( !$articles ) {
           throw $this-&gt;createNotFoundException(
                   'Keine Artikel gefunden!');
       }
  
      $serializer-&gt;serialize($articles, 'json');
      return new Response(json_encode($json));
  </code></pre>
  
  <p>But I receive an error: </p>
  
  <pre><code>    A circular reference has been detected (configured limit: 1). 
  </code></pre>