Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Uncaught Error: Call to a member function add() on string

Pessoal alguem consegue me ajudar ja vi e revi o codigo e nada retorna esse log no cmd

C:\Users\Marco Antonio\Desktop\Alura-PHP\PHP-Doctrine>php commands\criar-aluno.php "Marco Antonio" "(14) 99999-9999" "(14) 2222-2222"

PHP Fatal error: Uncaught Error: Call to a member function add() on string in C:\Users\Marco Antonio\Desktop\Alura-PHP\PHP-Doctrine\src\Entity\Aluno.php:51 Stack trace:

#0 C:\Users\Marco Antonio\Desktop\Alura-PHP\PHP-Doctrine\commands\criar-aluno.php(21): Alura\Doctrine\Entity\Aluno->addTelefone(Object(Alura\Doctrine\Entity\Telefone))

#1 {main} thrown in C:\Users\Marco Antonio\Desktop\Alura-PHP\PHP-Doctrine\src\Entity\Aluno.php on line 51

Fatal error: Uncaught Error: Call to a member function add() on string in C:\Users\Marco Antonio\Desktop\Alura-PHP\PHP-Doctrine\src\Entity\Aluno.php:51 Stack trace:

#0 C:\Users\Marco Antonio\Desktop\Alura-PHP\PHP-Doctrine\commands\criar-aluno.php(21): Alura\Doctrine\Entity\Aluno->addTelefone(Object(Alura\Doctrine\Entity\Telefone))

#1 {main} thrown in C:\Users\Marco Antonio\Desktop\Alura-PHP\PHP-Doctrine\src\Entity\Aluno.php on line 51

classe Aluno <?php

namespace Alura\Doctrine\Entity;

use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection;

/**

  • @Entity / class Aluno { /*

    • @Id

    • @GeneratedValue

    • @column(type="integer") / private int $id; /*

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

    • @OneToMany(targetEntity="Telefone",mappedBy="aluno") */ private string $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($nome): self { $this->nome = $nome; return $this; }

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

      return $this; }

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

classe Telefone <?php

namespace Alura\Doctrine\Entity;

/**

  • @Entity / class Telefone { /*

    • @Id

    • @GeneratedValue

    • @column(type="integer") / private int $id; /*

    • @column(type="string") / private string $numero; /*

    • @ManyToOne(targetEntity="Aluno") */ private string $aluno;

      public function getAluno(): Aluno { return $this->aluno; }

      public function setAluno(Aluno $aluno): self { $this->aluno = $aluno; return $this; }

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

      public function getNumero(): string { return $this->numero; }

      public function setNumero(string $numero) { $this->numero = $numero; return $this; } }

arquivo 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(); $entityManager = $entityManagerFactory->getEntityManager();

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

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

$entityManager->persist($telefone);
$aluno->addTelefone($telefone);

}

$entityManager->persist($aluno);

$entityManager->flush();

1 resposta
solução!

Consegui resolver refiz o modulo do curso deu certo rs