2011-09-27 8 views
5

I持って、関連するサブページを作成するために必要なデータベースからページスラッグを取得し、次のコード、:それはすべての親ページのドロップダウンメニューを表示するようErrorException:キャッチ可能な致命的なエラー:クラスのオブジェクトを文字列に変換できませんでした - 原因はドロップダウンメニューですが、なぜですか?

 $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'); 
      } 
     )); 

コードは、動作します私が持っています。私は、データベースにデータを保存するために行くときしかし、私は次のエラーを与えています:

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

私はコンテンツの実体ファイルの内容をチェックして、そしてここで、変数が宣言されている:

/** 
* @var integer $subtocontentoptions 
* 
* @ORM\Column(name="SubToContentOptions", type="integer", nullable=false) 
*/ 
private $subtocontentoptions; 
コンテンツ実体ファイルダウン

下:

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

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

このドロップダウンが取り出された後のコードの残りの部分は、作業を行います。ドロップダウンが原因でこのエラーが発生する理由がわかりません。

+0

ドロップダウンのオプションの価値は何ですか?それはテーブルのint値に従っていますか? – Tjorriemorrie

+0

ええ、テーブルのInt(ID)フィールドを使用します。私はこれを変更して、異なる値を使用しようとしています。 – mickburkejnr

+0

プライベートの代わりに 'subtocontentoptions'を保護しようとしてください – Reuven

答えて

10

__toString方法が私のために、この問題を解決し、実装、SF2/doctrine2プロジェクトと同じ問題を抱えていた

ありがとう:

public function __toString() 
{ 
    return strval($this->id); 
} 
+0

このエラーの原因は何か分かりますか? –

+0

外部キーがヌルの場合、エラーが発生します。理由は分かりません。 – Basit

関連する問題

 関連する問題