Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ErrorException: Catchable Fatal Error: Object of class could not be converted to string - Caused by dropdown menu but why?

I have the following code, which retrieves the page slugs from the database which are needed to then create a related sub page:

        $builder->add('subtocontentoptions', 'entity', array(
            'class' => 'ShoutAdminBundle:Content',
            'property' => 'slug',
            'query_builder' => function($repository) { 
                return $repository->createQueryBuilder('p')
                ->where('p.mainpage = :main')
                ->setParameter('main', '1')
                ->orderBy('p.created', 'ASC');
            }
        ));

The code works, as it displays a drop down menu of all the parent pages I have. However, when I go to save the data to the database, I am given the following error:

ErrorException: Catchable Fatal Error: Object of class Shout\AdminBundle\Entity\Content could not be converted to string in C:\wamp\www\vendor\doctrine-dbal\lib\Doctrine\DBAL\Statement.php line 131

I have checked the contents of the Content entity file, and here is the variable being declared:

/**
 * @var integer $subtocontentoptions
 *
 * @ORM\Column(name="SubToContentOptions", type="integer", nullable=false)
 */
private $subtocontentoptions;

And lower down the Content entity file:

/**
 * Set subtocontentoptions
 *
 * @param integer $subtocontentoptions
 */
public function setSubtocontentoptions($subtocontentoptions)
{
    $this->subtocontentoptions = $subtocontentoptions;
}

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

The rest of the code does work, once this drop down has been taken out. I'm not sure why the drop down is causing this error?

Thanks

like image 963
mickburkejnr Avatar asked Sep 27 '11 13:09

mickburkejnr


1 Answers

Was having the same issue with a sf2/doctrine2 project, implementing the __toString method resolved this issue for me :

public function __toString()
{
    return strval($this->id);
}
like image 163
ıɾuǝʞ Avatar answered Nov 15 '22 08:11

ıɾuǝʞ