【问题标题】:Attempted to call an undefined method named "getPosition" of class "App\Controller\ApiController"试图调用类“App\Controller\ApiController”的名为“getPosition”的未定义方法
【发布时间】:2020-07-27 01:37:05
【问题描述】:

我试图在我的控制器中实例化一个域对象,但我收到此错误“尝试从命名空间“App\Controller”调用函数“getPosition””。

我希望你能帮助我解决这个问题。 This is me error

但是我有get和set,那他为什么找不到呢?

ApiController

<?php

namespace App\Controller;

use App\Entity\Domain;
use App\Repository\DomainRepository;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use GeoIp2\Database\Reader;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/api")
 */
class ApiController extends AbstractFOSRestController
{
    /**
     * @Rest\Get("/domain", name="get_domain")
     * @Rest\Get("/domain/{blocked}")
     *
     * @throws
     */
    public function getDomain(Request $request, Reader $reader, DomainRepository $domainRepository, MailerInterface $mailer): JsonResponse
    {
        $record = $reader->country('87.99.79.119');
        $code   = $record->country->isoCode;
        $domain = $domainRepository->getDomain($code);
        $ip     = $request->getClientIp();
        if ($request->isXmlHttpRequest()) {
            $domain = $domainRepository->getDomain($code);
        }
        $position = getPosition();
        //
        //        $email = (new Email())
        //            ->from('noreply@makaobet.com')
        //            ->to('support@makaobet.com')
        ////            ->cc('cc@example.com')
        ////            ->bcc('bcc@example.com')
        ////            ->replyTo('fabien@example.com')
        ////            ->priority(Email::PRIORITY_HIGH)
        //            ->subject('Domain Error')
        //            ->html($domain->getDomain() . ' have problem!');
        //
        //        $mailer->send($email);

        return new JsonResponse(
            [
                'status' => 'ok',
                'domain' => $domain->getDomainName(),
            ], 200);
    }
}

域实体

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity(repositoryClass="App\Repository\DomainRepository")
 */
class Domain
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $domain_name;

    /**
     * @ORM\Column(type="array")
     */
    private $blocked_countries;

    /**
     * @Gedmo\SortablePosition
     * @ORM\Column(type="integer")
     */
    private $position;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getDomainName(): ?string
    {
        return $this->domain_name;
    }

    public function setDomainName(string $domain_name): self
    {
        $this->domain_name = $domain_name;

        return $this;
    }

    public function getBlockedCountries(): ?array
    {
        return $this->blocked_countries;
    }

    public function setBlockedCountries(array $blocked_countries): self
    {
        $this->blocked_countries = $blocked_countries;

        return $this;
    }

    public function getPosition(): ?int
    {
        return $this->position;
    }

    public function setPosition($position): self
    {
        $this->position = $position;

        return $this;
    }
}

我怎样才能做到这一点???

【问题讨论】:

  • 你调用的是一个函数,而不是一个方法,看起来你缺少$domain-&gt;
  • @msg $domain->$position = getPosition() 这样吗?
  • 反过来:$position = $domain-&gt;getPosition();

标签: php sql symfony


【解决方案1】:

为了让这个拖曳工作,你从存储库调用的任何东西都应该是这样的:

$domainRepository->getSomethig();

与设置相同:

$domainRepository->setSomething("some Value");

所以在你的情况下:

$position = $domainRepository->getPosition();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    相关资源
    最近更新 更多