php - Error when trying to get related table using Symfony 2 -
$em = $this->getdoctrine()->getmanager(); $products = $em->getrepository('namedemobundle:product')->findallorderedbyname($id);
function in repository:
public function findallorderedbyname($name) { return $this->getentitymanager() ->createquery( 'select p,c namedemobundle:product p join p.category c p.price = :name order p.name asc' ) ->setparameter('name', $name) ->getresult() ; }
and code works ok want category related results.
when use getsingleresult()
instead of getresult()
, code:
$category = $products->getcategory()->getname();
works fine, on getresults
when have more 1 result have error:
call member function getcategory() on non-object
can please explain why?
getresult returns array of object, if have 1 result, first have parse $products each $product object
foreach ( $products $product ) { $product->getcategory()->getname(); }
hope :)
Comments
Post a Comment