Implement __toString() method

Asked

Viewed 194 times

0

I have a foresight printer and when I step in the path of foresight.new give me error.

A "__toString()" method was not found on the Objects of type "Roqsys Control Manutencaobundle Entity Maquina" passed to the Choice field. To read a custom getter Instead, set the option "Property" to the desired Property path.

Someone knows what I have to do?

My Machine Essence is:

<?php

namespace RoqSys\Control\ManutencaoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use RoqSys\Control\ControlBundle\Entity\Posto as Posto;

/**
* Maquina
*
* 
* @ORM\Entity(repositoryClass="RoqSys\Control\ManutencaoBundle\Entity\MaquinaRepository")
* 
*/
class Maquina 
{
    /**
     * @var
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO") integer
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(type="bigint", nullable=true, name="ciclo")
     */
    private $ciclo=0;

    /**
     * @var \DateTime
     *
     * @ORM\Column(type="date", nullable=true, name="ultima")
     */
    private $ultima;

    /**
     * @ORM\OneToOne(targetEntity="RoqSys\Control\ControlBundle\Entity\Posto", inversedBy="maquina")
     * @ORM\JoinColumn(name="posto_id", referencedColumnName="id", unique=true)
     */
    private $posto;

    /**
     * @ORM\OneToMany(targetEntity="RoqSys\Control\ManutencaoBundle\Entity\Avaria", mappedBy="maquina")
     */
    private $avaria;

    /**
     * @ORM\OneToMany(targetEntity="RoqSys\Control\ManutencaoBundle\Entity\Intervencao", mappedBy="maquina")
     */
    private $intervencao;

    /**
     * @ORM\OneToMany(targetEntity="RoqSys\Control\ManutencaoBundle\Entity\Prevista", mappedBy="maquina")
     */
    private $prevista;

    /**
     * Set id
     *
     * @param integer $id
     * @return Maquina
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set ciclo
     *
     * @param integer $ciclo
     * @return Maquina
     */
    public function setCiclo($ciclo)
    {
        $this->ciclo = $ciclo;

        return $this;
    }

    /**
     * Get ciclo
     *
     * @return integer 
     */
    public function getCiclo()
    {
        return $this->ciclo;
    }

    /**
     * Set ultima
     *
     * @param \DateTime $ultima
     * @return Maquina
     */
    public function setUltima($ultima)
    {
        $this->ultima = $ultima;

        return $this;
    }

    /**
     * Get ultima
     *
     * @return \DateTime 
     */
    public function getUltima()
    {
        return $this->ultima;
    }

    /**
     * Set posto
     *
     * @param Posto $posto
     * @return Maquina
     */
    public function setPosto($posto)
    {
        $this->posto = $posto;

        return $this;
    }

    /**
     * Get posto
     *
     * @return Posto
     */
    public function getPosto()
    {
        return $this->posto;
    }

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

And the machine.new

{% block content -%}
    <h1>Nova Máquina</h1>
    <br>
    <ul class="record_actions">
        <li>
            <a href="{{ path('manutencao_maquina') }}">
                Voltar para a lista
            </a>
        </li>
    </ul>

    <table class="table table-striped table-hover table-condensed">
        <thead>
            <tr>
                <th>Id</th>
                <th>Número</th>
                <th>Descrição</th>
                <th>Endereço</th>
                <th>Estação</th>
                <th>Protocolo</th>
                <th>Ativo</th>
                <th>Ler</th>
                <th>Opções</th>
            </tr>
        </thead>
        <tbody>
            {% for posto in postos %}
                <tr>
                    <td><a href="{{ path('gestao_posto_show', { 'id': posto.id }) }}" >{{ posto.id }}</a></td>
                    <td>{{ posto.numero }}</td>
                    <td>{{ posto.descricao }}</td>
                    <td>{{ posto.endereco }}</td>
                    <td>{{ posto.estacao }}</td>
                    <td>{{ posto.protocolo }}</td>
                    <td>{{ posto.ativo }}</td>
                    <td>{{ posto.ler }}</td>
                    <td>
                        <ul>
                            <li>
                                <a href="{{ path('manutencao_maquina_create', { 'id': posto.id }) }}">Converter</a>
                            </li>
                        </ul>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock%}

Predicted.php

<?php

namespace RoqSys\Control\ManutencaoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use RoqSys\Control\ManutencaoBundle\Entity\Maquina as Maquina;

/**
 * Prevista
 *
 * 
 * @ORM\Entity(repositoryClass="RoqSys\Control\ManutencaoBundle\Entity\PrevistaRepository")
 */
class Prevista {

    /**
     * @var integer
     *
     * @ORM\Column(type="integer", name="id")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(type="integer", nullable=true, name="contagem")
     */
    private $contagem;

    /**
     * @var integer
     *
     * @ORM\Column(type="integer", nullable=true, name="duracao")
     */
    private $duracao;

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

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(type="date", nullable=true, name="ultima")
     */
    private $ultima;

    /**
     * @ORM\ManyToOne(targetEntity="RoqSys\Control\ManutencaoBundle\Entity\Maquina", inversedBy="prevista")
     * @ORM\JoinColumn(name="maquina_id", referencedColumnName="id")
     */
    private $maquina;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set contagem
     *
     * @param integer $contagem
     * @return Prevista
     */
    public function setContagem($contagem) {
        $this->contagem = $contagem;

        return $this;
    }

    /**
     * Get contagem
     *
     * @return integer 
     */
    public function getContagem() {
        return $this->contagem;
    }

    /**
     * Set duracao
     *
     * @param integer $duracao
     * @return Prevista
     */
    public function setDuracao($duracao) {
        $this->duracao = $duracao;

        return $this;
    }

    /**
     * Get duracao
     *
     * @return integer 
     */
    public function getDuracao() {
        return $this->duracao;
    }

    /**
     * Set titulo
     *
     * @param string $titulo
     * @return Prevista
     */
    public function setTitulo($titulo) {
        $this->titulo = $titulo;

        return $this;
    }

    /**
     * Get titulo
     *
     * @return string 
     */
    public function getTitulo() {
        return $this->titulo;
    }

    /**
     * Set ficha
     *
     * @param string $ficha
     * @return Prevista
     */
    public function setFicha($ficha) {
        $this->ficha = $ficha;

        return $this;
    }

    /**
     * Get ficha
     *
     * @return string 
     */
    public function getFicha() {
        return $this->ficha;
    }

    /**
     * Set descricao
     *
     * @param string $descricao
     * @return Prevista
     */
    public function setDescricao($descricao) {
        $this->descricao = $descricao;

        return $this;
    }

    /**
     * Get descricao
     *
     * @return string 
     */
    public function getDescricao() {
        return $this->descricao;
    }

    /**
     * Set ultima
     *
     * @param \DateTime $ultima
     * @return Prevista
     */
    public function setUltima($ultima) {
        $this->ultima = $ultima;

        return $this;
    }

    /**
     * Get ultima
     *
     * @return \DateTime 
     */
    public function getUltima() {
        return $this->ultima;
    }

    /**
     * Set maquina
     *
     * @param Maquina $maquina
     * @return Maquina
     */
    public function setMaquina($maquina) {
        $this->maquina = $maquina;

        return $this;
    }

    /**
     * Get maquina
     *
     * @return \Maquina
     */
    public function getMaquina() {
        return $this->maquina;
    }
}

1 answer

1

You are trying to select an entity of the type Maquina but the application doesn’t know which attribute to display. You need to define in the form type which attribute to display or implement the method __toString in the entity Maquina.

If you prefer to change the form type, put something like this:

$builder
    ->add('maquina', null, [
        'property' => 'id' // ou qualquer outra propriedade que você queira
    ])

If you prefer to implement the __toString:

class Maquina
{
    /* propriedades, getters e setters */

    public function __toString()
    {
        return (string)$this->getId(); // deve-se retornar uma string
    }
}
  • I put this in the class Machine { /* properties, getters and setters */ public Function __toString() { Return $this->getId(); } } and now it seemed like this error. Catchable Fatal Error: Method Roqsys Control Manutencaobundle Entity Machine::__toString() must Return a string value in /var/www/roqsys/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Choicelist/Objectchoicelist.php line 254

  • Symfony expects a string at the return of this method. Use (string)$this->getId() or strval($this->getId()). I edited my answer.

  • error An Exception has been thrown During the Rendering of a template ("Notice: Array to string Conversion in /var/www/roqsys/vendor/symfony/symfony/src/Symfony/Component/Translation/Identitytranslator.php line 65") in "/var/www/roqsys/vendor/braincrafted/bootstrap-Bundle/Braincrafted/Bundle/Bootstrapbundle/Resources/views/Form/bootstrap.html.Twig"

  • Is the attribute you want to return an array or a collection of entities? You need to return something that identifies that entity.

  • Look, my answer is what I did, but it makes a mistake. An Exception has been thrown During the Rendering of a template ("Notice: Array to string Conversion in /var/www/roqsys/vendor/symfony/symfony/src/Symfony/Component/Translation/Identitytranslator.php line 65") in "/var/www/roqsys/vendor/braincrafted/bootstrap-Bundle/Braincrafted/Bundle/Bootstrapbundle/Resources/views/Form/bootstrap.html.Twig".

  • From what I noticed in the error message, you managed to fix the question error but are giving translation problem somewhere in your view. Symfony is trying to translate a string but is receiving an array instead.

  • but that’ll be where? in Controller?

  • You are using the method trans in templates or trans() in PHP? It is probably in one of these that you are accusing the error.

  • neither one nor the other

  • how do I define this: To read a custom getter Instead, set the option "Property" to the desired Property path.

  • It’s the first example I put in the answer. To "translate" a model into a form (and vice versa) you use form types. Place the line above in the post form type.

  • Rodrigo Rigotti I still can’t solve the problem An Exception has been thrown During the Rendering of a template ("Notice: Array to string Conversion in /var/www/roqsys/vendor/symfony/symfony/src/Symfony/Component/Translation/Identitytranslator.php line 65") in "/var/www/roqsys/vendor/braincrafted/bootstrap-Bundle/Braincrafted/Bundle/Bootstrapbundle/Resources/views/Form/bootstrap.html.Twig". I wonder if you can help me?

  • You don’t want to share your code, and so you can analyze it further?

  • what code is that share, the code is on top, on the tag.

  • The problem is that the error you mentioned is very generic and can originate in several ways. From the looks of it, I believe it’s some form you’re trying to render and one of its components is set up wrong. But like I said, it’s one of the possibilities of making that mistake.

  • but I’m not getting to solve the problem.

  • If the question problem has been solved (that is, if the error message that originated the question no longer appears), validate the answer and create another question with emphasis on this error that we are discussing. Surely there will be more people trying to help with this problem.

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.