Class_parents error in Entity/Repository - php

In a bundle I'm developing and was working correctly I added a new functionality which involves adding a repository to the entity. Now, when I execute the newly added method I get the following error:
Warning: class_parents() [function.class-parents]: Class CmsPages does not exist and could not be loaded in /Applications/MAMP/htdocs/symfony-standard-2.1/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php line 40
The newly added code is:
Controller:
/**
* Returns an json formated tree
*
* #Route("/getTree", name="admin_cmsPages_getTree", options={"expose"=true})
*/
public function getTreeAction()
{
$em = $this->getDoctrine()->getManager();
$tree = $em->getRepository('CmsPages')->loadTree();
$response = new Response(json_encode( $tree ));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
Repository:
namespace Yanic\CmsBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
class CmsPagesRepository extends EntityRepository
{
public function loadTree()
{
$q = $this
->createQueryBuilder('p')
->select('p')
->orderBy( 'p.lft' )
->getQuery()
;
return $q->getArrayResult();
}
}
That's all that has changed... if any more code is needed for clarification I will post it.
So could anybody tell me what I'm doing wrong? I couldn't find anything neither on SO nor on Google.
Thanks in advance

I just found the error myself... the line
$tree = $em->getRepository('CmsPages')->loadTree();
has to be
$tree = $em->getRepository('CmsBundle:CmsPages')->loadTree();

Related

symfony2 method undefined in EntityRepository

i want create a request from my EntityRepository so this is my code:
<?php
namespace zhr\myprojectBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* myprojectdbEntityRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class myprojectdbEntityRepository extends EntityRepository
{
public function getAll()
{
$qb = $this->createQueryBuilder('s');
$query = $qb;
$result = $qb->getQuery()->execute();
}
}
i want use it in my controller file so this is my code:
public function searchusersAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('myprojectBundle:User');
$utilisateur = $repository->getAll();
var_dump($utilisateur); die();
//return $this->render('mypageBundle:Admin:adminindex.html.twig', array('sheet'=>$utilisateur));
}
i get a error:
Undefined method 'getAll'. The method name must start with either findBy or findOneBy!
??? normaly all methode in repository file must be defined in my controller file no ?
thanks first guys
You have to use getResult() to return a result as follow:
public function getAll()
{
return $this->createQueryBuilder('s')->getQuery()->getResult();
}
But you really don't need this kind of function since EntityRepository class already has findAll() function to get all rows from an entity:
$em = $this->getDoctrine()->getManager();
$rows = $em->getRepository('myprojectBundle:User')->findAll();
I suggest you to check documentation for QueryBuilder and EntityRepository api.

PHPUnit Laravel Testing Controller that uses a Repository

I have read so many examples and cannot see what I am doing wrong, please if someone could help.
I am getting an error when running tests (error at the bottom of post), that doens't happen when viewing the page in the browser. I think this is because the repository isn't being instantiated properly so the relevant method not fired? Or some issue with the API call in the mock.
Controller:
namespace ShopApp\Http\Controllers\StoreFront;
use Illuminate\Http\Request;
use ShopApp\Http\Requests;
use ShopApp\Http\Controllers\Controller;
use ShopApp\Repositories\Contracts\CategoryRepositoryContract;
use ShopApp\Repositories\Contracts\PublicationRepositoryContract;
class PagesController extends Controller
{
private $publication;
private $category;
public function __construct(PublicationRepositoryContract $publication, CategoryRepositoryContract $category){
$this->publication = $publication;
$this->category = $category;
}
/**
* Homepage.
* #return view
* #internal param PublicationRepositoryContract $publication
* #internal param CategoryRepositoryContract $category
*/
public function home()
{
$mostRecent = $this->publication->getRecent();
return view('pages/home')->with(compact('mostRecent'));
}
}
Publication Repository:
<?php
namespace ShopApp\Repositories;
use ShopApp\Models\API\APIModel;
use GuzzleHttp\Client as GuzzleClient;
use Illuminate\Support\Facades\Config;
use ShopApp\Repositories\Contracts\PublicationRepositoryContract;
class localPublicationRepository extends APIModel implements PublicationRepositoryContract
{
private $end_point; // where are we talking to?
public $response; //what did we get back?
public function __construct(GuzzleClient $client){
parent::__construct(new $client(['base_uri' => Config::get('customerprovider.local.api.base_uri'), 'http_errors' => true]));
$this->end_point = 'Publications';
}
/**
* Get all publications
*/
public function getAll(){
$this->response = $this->get($this->end_point);
$publications_with_slugs = $this->assembleSlugs($this->response);
return $publications_with_slugs;
}
/**
* Get recent publications
*/
public function getRecent(){
return $this->getAll(); //#todo - update this to just get the most recent
}
}
Test:
<?php
namespace Tests\Unit\Controllers;
use Tests\TestCase;
use Mockery as m;
class PagesControllerTest extends TestCase
{
public $publicationRepositoryContract;
/**
* Setup mocks etc
*/
public function setUp()
{
parent::setup();
$this->publicationRepositoryContract = m::mock('ShopApp\Repositories\Contracts\PublicationRepositoryContract');
}
/**
* Teardown mocks
*/
public function tearDown()
{
m::close();
parent::tearDown();
}
/**
* A basic test example.
*
* #return void
*/
public function testHomepage()
{
$this->publicationRepositoryContract
->shouldReceive('getRecent')
->once();
$this->app->instance('ShopApp\Repositories\Contracts\PublicationRepositoryContract', $this->publicationRepositoryContract);
$response = $this->call('GET', '/');
$response->assertStatus(200);
// getData() returns all vars attached to the response.
$mostRecent = $response->original->getData()['mostRecent'];
$response->assertViewHas('mostRecent');
$this->assertInstanceOf('Array', $mostRecent);
}
}
Test Error:
Expected status code 200 but received 500.
Failed asserting that false is true.
/home/vagrant/Code/imsnews-site/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:61
/home/vagrant/Code/imsnews-site/tests/Unit/Controllers/PagesControllerTest.php:53
Contents of Response ($response->Content()):
<span class="exception_title"><abbr title="ErrorException">ErrorException</abbr> in <a title="/home/vagrant/Code/imsnews-site/storage/framework/views/229655ca372490c9c0b1f5e7e2d4e91e6d3bbf6c.php line 262">229655ca372490c9c0b1f5e7e2d4e91e6d3bbf6c.php line 262</a>:</span>\n
<span class="exception_message">Invalid argument supplied for foreach() (View: /home/vagrant/Code/imsnews-site/resources/views/pages/home.blade.php)</span>\n
Line 262 from home.blade.php:
#foreach ($mostRecent as $key => $publication)
It seems clear that the method ->getRecent(), which in turn, calls ->getAll() on the publications repository is not returning an array as it should, but I don't know why.
Blade isn't complaining about the variable mostRecent not existing, it's complaining about it being invalid in a foreach.
Could this have something to do with Guzzle and the fact it's calling my API from the mocked test object?
Please help, hours have been lost..
Thanks.
Try mocking the concrete repository, and swap it out for the contract in the container. It seems you are mocking the contract, and then swapping it out for the same contract in your container.
TL;DR :
The key was you HAVE to have ->andReturn([]); on the test, like so:
$this->publicationRepositoryContract
->shouldReceive('getRecent')
->once()->andReturn([]);
My test only had:
$this->publicationRepositoryContract
->shouldReceive('getRecent')
->once();
Thanks to Ayo for pointing this out. It only became clear after deleting other parts of my test.

symfony2 creating json from object

Hi I am trying to generate json as response using symfony2.
Here is my code.
/**
* #Route("/viewComments/{taskId}")
*
*/
public function viewCommentsAction($taskId)
{
$commentsList = $this->getDoctrine()->getRepository("AppBundle:Comments")
->findBy(array("task" => $taskId));
if (!$commentsList) {
throw $this->createNotFoundException("non comments to show");
}
$serializer = $this->get('serializer');
$json = $serializer->serialize(
$commentsList,
'json', array('groups' => array('group1'))
);
return new Response($json);
}
unfortunately I am getting response:
[[],[],[],[]]
Any suggestions?
Try this, creates a file: CommentRepository
class CommentRepository extends EntityRepository
{
public function findCommentsList($id)
{
$query = $this->getEntityManager()->createQuery('YOUR QUERY HERE');
...
return $query->getArrayResult();
}
}
as you can see, I return an Array and now you can see values on the response.
Later, changes the next line in your Action Controller:
...
$commentsList = $this->getDoctrine()->getRepository("AppBundle:Comments")
->findCommentsList($taskId));
...
Note: You should call your entities in singular

Method 'QueryBuilder' not found in class ObjectRepository

New to Symfony & Doctrine
I trying to fetch a selection of objects from a MySQL database via Doctrine in a Symfony project. I am doing this with createQueryBuilder;
$repository = $this->getDoctrine()
->getRepository('mcsyncBundle:DB');
$query = $repository->createQueryBuilder('p')
->select('p')
->where('(p.created=:)' . $till)
->setParameter('created', $till)
->orderBy('p.created', 'ASC')
->getQuery();
$PackDB = $query->getResult();
But I keep getting the error:
*Method 'QueryBuilder' not found in class \Doctrine\Common\Persistence\ObjectRepository*.
Anyone that can explain (fix) this problem?
EDIT: This error is coming from inside PHPStorm by the way and NOT from Symfony itself
I suppose you wrote that code in a Controler file. QueryBuilders are meant to be in Repository files. For /Entity/Plop.php, you should have /Entity/PlopRepository.php as well.
PlopRepository.php
namespace Foo\BarBundle\Entity;
use Doctrine\ORM\EntityRepository;
class PlopRepository extends EntityRepository
{
public function getCreatedP()
{
$qb = $this->createQueryBuilder('p')
->select('p')
->where('p.created = :created')
->setParameter('created', $till)
->orderBy('p.created', 'ASC');
return $qb->getQuery()
->getResults();
}
// ...
}
EDIT 1 : and there was a mistake in your ->where('...') statement I fixed ;)
EDIT 2 : to be complete, controller part :
TotoController.php
public function getPlopAction()
{
$entityManager = $this->getDoctrine()->getManager();
$plopRepository = $entityManager->getRepository('FooBarBundle:Plop');
$plops = $plopRepository->getCreatedP();
// ...
}

Sonata Admin Bundle custom query for list to use existing Repository

I have my admin class creating a custom list using createQuery method
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$query->andWhere(
....
);
....
return $query;
}
It all works just fine, but since I have repository with this query already defined and tests already written for that repository I was wondering if it was possible to utilize doctrine repository method instead of this?
Thanks
Of course you can, as far as you return a Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery instance :
/**
* {#inheritDoc}
*/
public function createQuery($context = 'list')
{
$repository = $this->modelManager->getEntityManager($this->getClass())->getRepository($this->getClass());
$query = new ProxyQuery($repository->createMyCustomQueryBuilder());
foreach ($this->extensions as $extension) {
$extension->configureQuery($this, $query, $context);
}
return $query;
}

Categories