0

I am new to symfony.

I'm trying to follow some tutorial but I'm stuck with the use of ORM

I generated an entity with the following command:

php bin/console make:entity

My entity has a field "name" and therefore a function setName ()

I generated a controller with the command:

php bin/console make:controller TestController

In this controller I try to register a new entry but I have this error that appears:

Attempted to call function "setName" from namespace "App \ Controller".

What I'm doing wrong ?

TestController.php:

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use App\Entity\Team;

class TestController extends Controller
{
    /**
     * @Route("/test", name="test")
     */
    public function index()
    {
        $entityManager = $this->getDoctrine()->getManager();
        $team = new Team();
        $team.setName("Marvels 6");

        $entityManager->persist($team);
        $entityManager->flush();

        return $this->json([
            'message' => 'Welcome to your new controller!',
            'path' => 'src/Controller/TestController.php',
        ]);
    }
}

Team.php:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Poule", inversedBy="teams")
     */
    private $poule;

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

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getPoule(): Poule
    {
        return $this->poule;
    }

    public function setPoule(Poule $poule): self
    {
        $this->poule = $poule;

        return $this;
    }
}

1 Answer 1

5

Ist that your copy&pasted code?

$team.setName("Marvels 6");

should probably be:

$team->setName("Marvels 6");
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, you're right. I made too much javascript lately. Thanks to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.