Implement method __toString ()

0

I have a folder Previta and when I enter the path of previta.new gives me error.

  The "__toString ()" method was not found on the objects of type "RoqSys \ Control \ MaintenanceBundle \ Entity \ Machine" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path.

Does anyone know what I have to do?

My entity Machine 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%}

Prevista.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;
    }
}
    
asked by anonymous 12.02.2015 / 16:03

1 answer

1

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

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 __toString :

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

    public function __toString()
    {
        return (string)$this->getId(); // deve-se retornar uma string
    }
}
    
12.02.2015 / 16:10