私は教義に不慣れで、最初のエンティティ関係/関連付けを設定しようとして苦労しています。
次の2つのテーブルが必要です。
Kitchen
- ID
- Name
- Description
- MainImage
KitchenImage
- ID
- KitchenID (ref Kitchen.ID)
- Image
各キッチンにはメイン写真とたくさんのサブ写真があります。
これが現時点での私の 2 つのドクトリン ファイルです (フォーマットのために get と setter を削除しました):
キッチン
/**
* @ORM\Entity
* @ORM\Table(name="kitchen")
*/
class Kitchen
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\Column(type="string", length=255)
*/
protected $image;
/**
* @ORM\OneToMany(targetEntity="KitchenImage", mappedBy="kitchen")
*/
protected $images;
public function __construct()
{
$this->images = new ArrayCollection();
}
}
キッチン_イメージ
/**
* KitchenImage
*
* @ORM\Table(name="kitchen_image")
* @ORM\Entity
*/
class KitchenImage
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255)
*/
protected $image;
/**
* @ORM\ManyToOne(targetEntity="Kitchen", inversedBy="images")
* @ORM\JoinColumn(name="kitchen_id", referencedColumnName="id")
*/
protected $kitchen;
}
PHP 関数:
public function createAction()
{
$kitchen = new Kitchen();
$kitchen->setName('Example Kitchen');
$kitchen->setDescription('Big amount of text to go here...');
$kitchen->setImage('front-image.jpg');
$image1 = new KitchenImage();
$image1->setImage('1.jpg');
$image2 = new KitchenImage();
$image2->setImage('2.jpg');
$kitchen->addImage($image1);
$kitchen->addImage($image2);
$em = $this->getDoctrine()->getManager();
$em->persist($kitchen);
$em->flush();
return new Response('success');
}
エラー応答
A new entity was found through the relationship 'PWD\WebsiteBundle\Entity\Kitchen#images' that was not configured to cascade persist operations for entity: PWD\WebsiteBundle\Entity\KitchenImage@0000000009670754000000003a9b3dc9. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'PWD\WebsiteBundle\Entity\KitchenImage#__toString()' to get a clue.
私は何を間違っていますか?