Solucionado (ver solução)
Solucionado
(ver solução)
7
respostas

Erro ao criar aluno

PHP Fatal error: Uncaught TypeError: Argument 1 passed to Alura\Doctrine\Entity\Aluno::setNome() must be of the type string, object given, called in C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php on line 61 and defined in C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php:52 Stack trace:

#0 C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php(61): Alura\Doctrine\Entity\Aluno->setNome(Object(Alura\Doctrine\Entity\Aluno))

#1 C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\commands\criar-aluno.php(22): Alura\Doctrine\Entity\Aluno->addTelefone(Object(Alura\Doctrine\Entity\Telefone))

#2 {main} thrown in C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php on line 52

Fatal error: Uncaught TypeError: Argument 1 passed to Alura\Doctrine\Entity\Aluno::setNome() must be of the type string, object given, called in C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php on line 61 and defined in C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php on line 52

TypeError: Argument 1 passed to Alura\Doctrine\Entity\Aluno::setNome() must be of the type string, object given, called in C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php on line 61 in C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php on line 52

Call Stack: 0.0003 396320 1. {main}() C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\commands\criar-aluno.php:0 0.0912 4242640 2. Alura\Doctrine\Entity\Aluno->addTelefone($telefone = class Alura\Doctrine\Entity\Telefone { private int $id = uninitialized; private string $numero = '(24) 99999-9999'; private $aluno = NULL }) C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\commands\criar-aluno.php:22 0.0912 4243016 3. Alura\Doctrine\Entity\Aluno->setNome($nome = class Alura\Doctrine\Entity\Aluno { private int $id = uninitialized; private string $nome = 'Vinicius Dias'; private $telefones = class Doctrine\Common\Collections\ArrayCollection { private $elements = [...] } }) C:\Users\fnell\Documents\www\Alura\php\php-doctrine\php-doctrine\src\Entity\Aluno.php:61

7 respostas

Argument 1 passed to Alura\Doctrine\Entity\Aluno::setNome() must be of the type string, object given

O método setNome espera uma string. Você passou um objeto por parâmetro.

Estou passando esse código no power shell dentro da pasta commands php criar-aluno.php "Nikita Popov" "(92) 3708-0868" "(92) 99301-4072"

Mas como está seu código de "criar-aluno.php"?

<?php

use Alura\Doctrine\Entity\Aluno; use Alura\Doctrine\Entity\Telefone; use Alura\Doctrine\Helper\EntityManagerFactory;

require_once DIR . '/../vendor/autoload.php';

$entityManagerFactory = new EntityManagerFactory(); $entitiManager = $entityManagerFactory->getEntityManager();

$aluno = new Aluno(); $aluno->setNome($argv[1]);

for ($i = 2; $i < $argc; $i++) { $numeroTelefone = $argv[$i]; $telefone = new Telefone(); $telefone->setNumero($numeroTelefone);

$entitiManager->persist($telefone);

$aluno->addTelefone($telefone);

}

$entitiManager->persist($aluno);

$entitiManager->flush();

Segundo a stack de erro, o problema está no método addTelefone.

Posta todo o código da sua classe Aluno

<?php

namespace Alura\Doctrine\Entity;

use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\GeneratedValue; use Doctrine\ORM\Mapping\Id;

/**

  • @Entity / class Aluno { /*

    • @Id

    • @Column(type="integer")

    • @GeneratedValue */ private int $id;

      /**

    • @Column(type="string") */ private string $nome;

      /**

    • @ORM\OneToMany(targetEntity="Telefone", mappedBy="aluno", cascade={"remove", "persist"}) */ private $telefones;

      public function __construct() { $this->telefones = new ArrayCollection(); }

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

      public function getNome(): string { return $this->nome; }

      public function setNome(string $nome):self { $this->nome = $nome; return $this; }

      public function addTelefone(Telefone $telefone) { $this->telefones->add($telefone); $this->setNome($this);

      return $this; }

      public function getTelefones(): Collection { return $this->telefones; } }

solução!

O erro tá nessa linha:

$this->setNome($this);

Você tá definindo o nome do aluno como o próprio objeto do aluno. Não faz sentido.