Search code examples
angularjssymfonycontrollerentity

Calling an entity method from controller


I'm having an issue with the following.

My HTML Angular section

<button ng-click="setFollowed(store)" class="btn btn-default">Follow</button>

My app.js

  .....
$scope.setFollowed = function(aStore){
    $http.put('/set-followed-store/aStore', { params: { aStore: aStore } }).success(function(data){
        alert(aStore.nombre);
    }).
    error(function(data, status, headers, config) {
                    alert(data);
    });
}

This works fine.

The problem appears when I invoke the method inside the "set-followed-store/store" URL.

This is my method inside the controller, (I omitted the routing.yml, but it's configured to trigger this method.)

    public function setFollowedStoreAction($aStore)
{
 
    $em = $this->getDoctrine()->getManager();
    
    $follower_store = $em->getRepository('AppBundle:Store')
                ->find($this->getUser()->getId());
      
    if(!$follower_store)
        throw $this->createNotFoundException('No store has been found with id: '.$this->getUser()->getId());
    
      
    $follower_store.addFollowedStore($aStore);
    
    $em->flush();
    
    return new Response('200 OK');
    
}

$follower_store.addFollowedStore($aStore);

gives me the following error,

Attempted to call function "addFollowedStore" from namespace "AppBundle\Controller". (500 Internal Server Error)


Solution

  • You should use -> instead of . to call method on an object:

    $follower_store->addFollowedStore($aStore);