Call repository function with entity manager but getting namespace error - symfony

I am calling repository function with following detail
$ratingData = $em->getRepository(PatientFeedback::class)->getRatingReviewData($doctorId, $this->timezone);
and my repository class is like:
namespace App\Repository;
class PatientFeedbackRepository extends ServiceEntityRepository
{
}
getting error like:
Attempted to call function \"getRatingReviewData\" from namespace \"Api\\Controller\".
is anything specific I am missing to use entity repository?

You have a syntax error:
$em->getRepository(PatientFeedback::class)>getRatingReviewData(...)
to:
$em->getRepository(PatientFeedback::class)->getRatingReviewData(...)
Without the -, it's looking for a function nammed getRatingReviewData in the current namespace

Use EntityRepository instead of ServiceEntityRepository :
use Doctrine\ORM\EntityRepository;
class UsersRepository extends EntityRepository

Related

Symfony 6 - Cannot autowire argument Entity of Controller

I am trying to remove sensio/framework-extra-bundle from composer, however, when I do, I get the error above in my Controllers that have parameterized routes. Not finding the correct answer for a replacement?
Her is an example controller snippet.
namespace App\Controller;
use App\Entity\Ticket;
use App\Form\TicketType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/admin/ticket')]
class TicketController extends AbstractController
{
#[Route('/{id}', name: 'app_ticket_show', methods: ['GET'])]
public function show(Ticket $ticket): Response
{
return $this->render('ticket/show.html.twig', [
'ticket' => $ticket,
]);
}
}

how and where to execute custom queries in symfony 4

I am using an entity class which extends ServiceEntityRepository like this:
class Sms extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Sms::class);
}
...
}
so when I need to persist an instance of the entity class in my controller file I need to pass the ManagerRegistry as the argument for my Entity class but I couldn't find a way to access the ManagerRegistry in my controller.
can anyone help?
The problem was that ServiceEntityRepository should be extended in a repository class, not an entity class.
as it is mentioned here, there is no good description for auto-generating repositories from an existing database.
This is my entity class with its annotations:
/**
* Sms
*
* #ORM\Table(name="sms")
* #ORM\Entity(repositoryClass="App\Repository\SMSRepository")
*/
class Sms
{ ... }
this line is very important:
#ORM\Entity(repositoryClass="App\Repository\SMSRepository")
another important thing is to removing Entity from excluding in the services.YAML file.
if you set a name for the repository of your entity class by annotation, by running this command you will have your repository generated:
php bin\console make:entity --regenerate
and you can simply write you complex queries in the repository file which is generated by the aforementioned command.
for calling methods of your repository class you can use this in your controller files:
$this->getDoctrine()->getRepository(EntityFile::class)->youFunctionNameInRepositoryFile()
be careful about the argument of the getRepository which is the Entity file, not the repository file.

Class "AppBundle\Controller\HomeController" used for service "AppBundle\Controller\HomeController" cannot be found

I am using Symfony 3.4 and when I'm trying to run server there is an error:
In RegisterControllerArgumentLocatorsPass.php line 68:
Class "AppBundle\Controller\HomeController" used for service "AppBundle\Controller\HomeController" cannot be found.
How can I deal with this problem?
The same error manifests when a controller extends Symfony's AbstractController and does not import it.
Solved by adding the import to the controller:
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller
{
/**
* #Route('/')
* #return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction()
{
return $this->render('home/index.html.twig');
}
}
Exception thrown when handling an exception (Symfony\Component\Config\Exception\FileLoaderLoadException: [Syntax Error] Expected PlainValue, got ''' at position 7 in method AppBundle\Controller\HomeController::indexAction() in /home/remas/relevium_symfony/src/AppBundle/Controller/ (which is being imported from "/home/remas/relevium_symfony/app/config/routing.yml"). Make sure annotations are installed and enabled.)

Symfony 3.3.10 RunTime exception Class mismatch

Case mismatch between loaded and declared class names: "Symfony\Bundle\FrameWorkBundle\Controller\Controller" vs "Symfony\Bundle\FrameworkBundle\Controller\Controller".
In my controller i have this:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameWorkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class MainController extends Controller
{
public function homepageAction()
{
return $this->render('main/index.html.twig');
}
}
Maybe just fix what the error tells you? (sorry if that sounds harsh)
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
instead of
use Symfony\Bundle\FrameWorkBundle\Controller\Controller;

Can't extend Doctrine's Repository

I'm trying to make a custom Doctrine's ORM Repository and extend it but I can't find a way to make it work. So far this is what i have:
The original Repository
//AppBundle\Repository\LocaleRepository.php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
use JMS\DiExtraBundle\Annotation as DI;
class LocaleRepository extends EntityRepository
{
protected myCustomFunction(){
}
}
The extended Repository
//OfficeBundle\Repository\OfficeRepository.php
namespace OfficeBundle\Repository;
use AppBundle\Repository\LocaleRepository;
class OfficeRepository extends LocaleRepository
{
//Empty class
}
My entiy:
namespace OfficeBundle\Entity;
// some calls to traits
use Doctrine\ORM\Mapping as ORM;
/**
* Office
*
* #ORM\Table(name="office__office")
* #ORM\Entity(repositoryClass="OfficeBundle\Repository\OfficeRepository")
*/
class Office implements TranslatableInterface{
//...
}
And Finally the call:
$em = $this->getDoctrine()->getManager();
$this->getEntityManager();
$office=$em->getRepository('OfficeBundle:Office')->myCustomeFunction($slug);
This trows the exception:
Undefined method 'myCustomFunction'. The method name must start with either findBy or findOneBy!
If I place myCustomeFunction inside the OfficeRepository it works fine but it brings down the purpose of extendind the repository. Also, the repository loaded by the controller is the correct one, vardumping the class shows: 'OfficeBundle\Repository\OfficeRepository'.
Finally I'm using KNP DoctrineBehaviors(translatable) on the office entity.
You must make your method public if you are going to use it outside the repository class.
class LocaleRepository extends EntityRepository
{
public function myCustomFunction()
{
....
}
}

Resources