2015-12-30 30 views
-1

我無法將對象保存到數據庫。 這是這是DefaultController至極位於的appbundle \控制器錯誤link to error試圖調用名爲「setName」的未定義方法

<?php 

namespace AppBundle\Controller; 

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use AppBundle\Entity\Product; 
class DefaultController extends Controller 
{ 
    /** 
    * @Route("/", name="homepage") 
    */ 
    public function indexAction(Request $request) 
    { 
     // replace this example code with whatever you need 
     return $this->render('default/index.html.twig', [ 
      'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'), 
     ]); 
    } 
/** 
* @Route("/content", name="db") 
*/ 
    public function createAction() 
    { 
     $product = new Product(); 
     $product->setName('A Foo Bar'); 
     $product->setPrice('19.99'); 
     $product->setDescription('Lorem ipsum dolor'); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($product); 
     $em->flush(); 
     return new Response('Created product id '.$product->getId()); 
    } 
} 

這是對的appbundle \實體產品實體的字段名稱:

namespace AppBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 
/** 
* @ORM\Entity 
* @ORM\Table(name="product") 
*/ 
class Product 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    protected $name; 
    /** 
    * @ORM\Column(type="decimal", scale=2) 
    */ 
    protected $price; 
    /** 
    * @ORM\Column(type="text") 
    */ 
    protected $description; 


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

你可以發表你的實體? –

+1

很明顯,你的實體中沒有'setName'方法('Product' class) – Ostin

+0

我已經添加了我的實體 –

回答

3

你是有此錯誤的原因:

  • 產品實體已經不是setter SETNAME
  • 您正在加載錯誤的實體類使用命名空間檢查使用命名空間
+0

我添加了實體。也許你能夠通過? –

+0

你需要添加setter和getters到你的實體setName,setPrice和setDescription也是getters –

+0

我已經像這樣添加了他們** php app/console doctrine:generate:entities AppBundle/Entity/Product ** –

2

這是一個直接的錯誤,因爲你沒有生成你的getters和setters。看看Generating Getters and Setters

這是你必須做的:

$ php app/console doctrine:generate:entities AppBundle/Entity/Product 
+0

我已經生成 #生成AppBundle中的所有實體 ** $ php app/console doctrine:generate:entities AppBundle ** –

+0

我看到您已經在跟蹤文檔。您應該再次生成它們,但現在僅適用於實體產品。 「這是一個安全的命令 - 你可以反覆運行它。」 –

+0

我已經試過這樣做 –

相關問題