Abid
Abid

Reputation: 74

Symfony Catchable Fatal Error: Object of class AppBundle\Entity\Email could not be converted to string

i'm new in symfony, i created a small DB schema attached: enter image description here

Email Class:

    <?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Email
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\EmailRepository")
 */
class Email extends Service
{

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




    /**
     * Set emailAddress
     *
     * @param string $emailAddress
     *
     * @return Email
     */
    public function setEmailAddress($emailAddress)
    {
        $this->emailAddress = $emailAddress;

        return $this;
    }

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


}

and my service class:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;

/**
 * Service
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ServiceRepository")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"newsletter" = "Newsletter", "email" = "Email", "service" = "Service"})
 * 
 */
class Service
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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


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

    /**
     * Set serviceTitle
     *
     * @param string $serviceTitle
     *
     * @return Service
     */
    public function setServiceTitle($serviceTitle)
    {
        $this->serviceTitle = $serviceTitle;

        return $this;
    }

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


    /**
     * @ORM\ManyToOne(targetEntity="Service", inversedBy="children")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="Service", mappedBy="parent")
     */
    private $children;
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set parent
     *
     * @param \AppBundle\Entity\Service $parent
     *
     * @return Service
     */
    public function setParent(\AppBundle\Entity\Service $parent = null)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return \AppBundle\Entity\Service
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Add child
     *
     * @param \AppBundle\Entity\Service $child
     *
     * @return Service
     */
    public function addChild(\AppBundle\Entity\Service $child)
    {
        $this->children[] = $child;

        return $this;
    }

    /**
     * Remove child
     *
     * @param \AppBundle\Entity\Service $child
     */
    public function removeChild(\AppBundle\Entity\Service $child)
    {
        $this->children->removeElement($child);
    }

    /**
     * Get children
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChildren()
    {
        return $this->children;
    }




}

then i generate crud for email, it was working perfect, now i actually want to add two fields for service (SERVICE TITLE, and PARENT SERVICE)

what i did, i just opened email type form and added two fields for (service title and Parent service):

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class EmailType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('emailAddress')
            ->add('serviceTitle')
            ->add('parent')
            ;


    }

after saving, when i create new email service browser return exception:

Catchable Fatal Error: Object of class AppBundle\Entity\Email could not be converted to string

right now i just add parent ID in text box (input type text) but actually i want to use (choice lise) from which user can set a parent service at the time of creating new service, and that choice list having all previously created services

Upvotes: 0

Views: 1249

Answers (1)

Frank B
Frank B

Reputation: 3697

add a __toString() method to your email class:

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

Upvotes: 1

Related Questions