2013-06-19 24 views
5

Sau đây là đoạn mã của tôi, những gì tôi đang cố gắng làm là chèn ID của bảng cha vào bảng con cùng với chèn dữ liệu vào bảng con cùng một lúc. Sau đây là mã của tôi một số câu liênKhông thể tuôn ra dữ liệu vào bảng con bằng cách sử dụng học thuyết

public function addAction() 
{ 

    $ViewModel = new ViewModel(); 
    $form = new TestForm(); 
    $form->get('submit')->setValue('Add'); 
    $request = $this->getRequest(); 
    if ($request->isPost()) 
    { 
     $TestFilter = new TestFilter(); 
     $test = $this->getServiceLocator()->get('Test'); 
     $form->setInputFilter($TestFilter->getInputFilter()); 
     $form->setData($request->getPost()); 
     if ($form->isValid()) 
     { 

      $test->populate($form->getData()); 

      $this->getEntityManager()->persist($test); 
      $this->getEntityManager()->flush(); 

      $TestDetail = $this->getServiceLocator()->get('TestDetail'); 
      $TestDetail->populate($form->getData()); 
      $TestDetail->setTest($test); 
      $this->getEntityManager()->persist($TestDetail); 
      $this->getEntityManager()->flush(); 
      return $this->redirect()->toRoute('test'); 
     } 
    } 
    return array('form' => $form); 
} 

Đối tượng của tôi là như sau

<?php 

namespace Test\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Zend\Form\Annotation; 
use Test\Entity\Test; 

/** 
* An Test entity. 
* 
* @ORM\Entity 
    * @ORM\Table(name="testdetail") 
* 
* @property int $id 
* @property string $fname 
* @property string $lname 
* @property string $description 
* @property datetime $creation_date 
* @property datetime $modification_date 
* 
* @Annotation\Name("Test_Detail") 
* 
*/ 
class TestDetail 
{ 

/** 
* @ORM\Id 
* @ORM\Column(type="integer"); 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\Column(type="string") 
* 
* @Annotation\Required(true) 
*/ 
protected $first_name; 

/** 
* @ORM\Column(type="string") 
* 
* @Annotation\Required(true) 
*/ 
protected $last_name; 

/** 
* @ORM\Column(type="string") 
* 
* @Annotation\Required(true) 
*/ 
protected $description; 

/** 
* @ORM\ManyToOne(targetEntity="TestDetail", inversedBy="test_details") 
* 
* @Annotation\Required(true) 
*/ 
protected $test; 

public function populate($data) 
{ 
    $this->id = isset($data['id']) ? $data['id'] : $this->id; 
    $this->first_name = isset($data['first_name']) ? $data['first_name'] : $this->first_name; 
    $this->last_name = isset($data['last_name']) ? $data['last_name'] : $this->last_name; 
    $this->description = isset($data['description']) ? $data['description'] : $this->description; 
} 

/* 
* Constructor 
*/ 

public function __construct() 
{ 
    $now = new \DateTime("now"); 
    $this->modification_date = $now; 
} 

/** 
* Magic getter to retrieve protected properties. 
* 
* @param string $property  
*/ 
public function __get($property) 
{ 
    return $this->$property; 
} 

/** 
* Magic setter to save protected properties. 
* 
* @param string $property 
* @param mixed $value 
*/ 
public function __set($property, $value) 
{ 
    $this->$property = $value; 
} 

public function setTest(Test $test) 
{ 
    $this->test = $test; 
} 

} 

?> 


<?php 

namespace Test\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Zend\Form\Annotation; 

/** 
    * An Test entity. 
    * 
    * @ORM\Entity 
    * @ORM\Table(name="test") 
    * 
    * @property int $id 
    * @property string $name 
    * @property string $address 
    * @property smallint $status 
    * @property datetime $creation_date 
    * @property datetime $modification_date 
    * 
    * @Annotation\Name("Test") 
    * 
    */ 
class Test 
{ 

/** 
* @ORM\Id 
* @ORM\Column(type="integer"); 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\Column(type="string") 
* 
* @Annotation\Required(true) 
*/ 
protected $name; 

/** 
* @ORM\Column(type="string") 
* 
* @Annotation\Required(true) 
*/ 
protected $status; 

/** 
* @ORM\Column(type="datetime") 
* 
* @Annotation\Required(false) 
*/ 
protected $creation_date; 

/** 
* @ORM\Column(type="datetime") 
* 
* @Annotation\Required(false) 
*/ 
protected $modification_date; 

/** 
* @ORM\OneToMany(targetEntity="TestDetail", mappedBy="test", orphanRemoval=true) 
* 
* @Annotation\Required(false) 
*/ 
protected $test_details; 



    public function populate($data) { 
    $this->id = isset($data['id']) ? $data['id'] : $this->id; 
    $this->name = isset($data['name']) ? $data['name'] : $this->name; 
    $this->status = isset($data['status']) ? $data['status'] : $this->status; 
} 



/* 
* Constructor 
*/ 

public function __construct() 
{ 
    $now = new \DateTime("now"); 
    $this->creation_date = $now; 
    $this->modification_date = $now; 
} 

/** 
* Magic getter to retrieve protected properties. 
* 
* @param string $property  
*/ 
public function __get($property) 
{ 
    return $this->$property; 
} 

/** 
* Magic setter to save protected properties. 
* 
* @param string $property 
* @param mixed $value 
*/ 
public function __set($property, $value) 
{ 
    $this->$property = $value; 
} 




} 

Tôi nhận được lỗi này sau khi Flush

Found entity of type Test\Entity\Test on association Test\Entity\TestDetail#test, but expecting Test\Entity\TestDetail 

Trả lời

3
/** 
* @ORM\ManyToOne(targetEntity="Test", inversedBy="test_details") 
* 
* @Annotation\Required(true) 
*/ 
protected $test; 

Như bạn đang đề cập thử nghiệm đơn vị tổ chức mục tiêu của bạn nên có Kiểm tra. Hãy thử mã đề cập ở trên và cho tôi biết.

+0

Đã kiểm tra và kiểm tra, đây là lúc gây phiền nhiễu lần đầu tiên sau tôi đã chọn điểm tôi tự tham chiếu đến cùng một bảng. –

1

Nhìn vào TestDetails bất động sản :: kiểm tra

/** 
* @ORM\ManyToOne(targetEntity="TestDetail", inversedBy="test_details") 
* 
* @Annotation\Required(true) 
*/ 
protected $test; 

Bạn đang định tổ chức mục tiêu yo được TestDetail (cùng lớp), trong khi bạn vượt qua một thể hiện của Test để TestDetails :: setTest()

Các vấn đề liên quan