データベースとDoctrineの使用に関するSymfony2の本( http://symfony.com/doc/2.0/book/doctrine.html )に書かれているコードを読んでフォローしています。「Entity Relationships/Associations」セクションに到達しましたが、フレームワークが意図したとおりに動作していないようです。保護された $category フィールドを Product エンティティに追加し、$products フィールドを Category エンティティに追加しました。私の製品とカテゴリのエンティティは次のとおりです。
製品:
<?php
namespace mydomain\mywebsiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table()
* @ORM\Entity
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255)
*/
private $description;
/*
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* Set description
*
* @param string $description
* @return Product
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
カテゴリー:
<?php
namespace mydomain\mywebsiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use \Doctrine\Common\Collections\ArrayCollection;
/**
* Category
*
* @ORM\Table()
* @ORM\Entity
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255)
*/
private $description;
/*
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
protected $products;
public function __construct(){
$this->products = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set description
*
* @param string $description
* @return Category
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
}
ドキュメントによると、今実行すると
$ php app/console doctrine:generate:entities mydomain
フレームワークは、Product の新しいカテゴリ フィールドと、Category の新しい製品フィールドのゲッター/セッターを生成する必要があります。
ただし、コマンドを実行すると、エンティティが更新されると思われますが、プロパティは追加されません。バックアップ (~) ファイルと比較しましたが、違いはありません。別のフィールド (例: description2) を追加し、永続化のためのドクトリン アノテーションを追加すると、プロパティが生成されます。最初はこれを無視し、マッピング フィールドのプロパティを手動で追加してから実行しました。
$php app/console doctrine:schema:update --force
新しい関連付け列を追加します。
ただし、メタデータとスキーマが最新であることがもう一度わかりました。
app/cache/dev フォルダーを削除し、システムがそれを再作成できるようにしましたが、違いはありません。
ドキュメントに記載されているようにフレームワークが動作しない理由を誰でも見ることができますか??
ありがとう