我正在学习符号2。在文档中,我看到了多对一关系的示例。我试着在我的代码中这样做。我有两个实体:产品和类别。
/**
* @ORM'ManyToOne(targetEntity="Category",inversedBy="products")
* @ORM'JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
在实体产品中,我有这样的代码。我执行了CCD_ 1和CCD_。表Category已经出现,但在表产品中我没有字段Category_id。我清除了缓存,但它不起作用。怎么了?
产品
<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use AppBundle'Entity'Category;
/**
* Product
* @ORM'Entity
* @ORM'Table(name="product")
*/
class Product
{
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(type="string",length=100)
*/
private $name;
/**
* @ORM'Column(type="decimal", scale=2)
*/
private $price;
/**
* @ORM'Column(type="text")
*/
private $description;
/**
* @ORM'ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM'JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Product
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set price
*
* @param integer $price
* @return Product
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return integer
*/
public function getPrice()
{
return $this->price;
}
/**
* 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;
}
}
类别
<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Doctrine'Common'Collections'ArrayCollection;
/**
* Category
* @ORM'Entity
* @ORM'Table(name="category")
*/
class Category
{
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(type="string", length=64)
*/
private $name;
/**
* @ORM'OneToMany(targetEntity="Product", mappedBy="category")
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
UPD2在php应用程序''控制台原则之后:模式:更新--转储sql
c:'xampp1'htdocs'first_project
# php app'console doctrine:schema:update --dump-sql
CREATE TABLE category (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NUL
L, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE =
InnoDB;
CREATE TABLE product (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(128) NOT NULL
, price INT NOT NULL, description LONGTEXT NOT NULL, PRIMARY KEY(id)) DEFAULT CH
ARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
COMP323 c:'xampp1'htdocs'first_project
#
请在带有对Category实体的完整命名空间引用的注释中尝试此操作。
/**
*@ORM'ManyToOne(targetEntity="AppBundle'Entity'Category",inversedBy="products")
*@ORM'JoinColumn(name="category_id", referencedColumnName="id")
*/
这两个在symfony 2.8(lts)中为我工作。
使用php app/console doctrine:schema:update --force --dump-sql
更新数据库
我已经删除了可以使用生成的getter和setter
php app/console doctrine:generate:entities AppBundle:Product
php app/console doctrine:generate:entities AppBundle:Category
Product.php
<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use AppBundle'Entity'Category;
/**
* Product
* @ORM'Entity
* @ORM'Table(name="product")
*/
class Product
{
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(type="string",length=100)
*/
private $name;
/**
* @ORM'Column(type="decimal", scale=2)
*/
private $price;
/**
* @ORM'Column(type="text")
*/
private $description;
/**
* @ORM'ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM'JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
Category.php
<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Doctrine'Common'Collections'ArrayCollection;
/**
* Category
* @ORM'Entity
* @ORM'Table(name="category")
*/
class Category
{
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(type="string", length=64)
*/
private $name;
/**
* @ORM'OneToMany(targetEntity="Product", mappedBy="category")
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
}
上述代码将生成以下SQL:
CREATE TABLE category (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(64) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE product (id INT AUTO_INCREMENT NOT NULL, category_id INT DEFAULT NULL, name VARCHAR(100) NOT NULL, price NUMERIC(10, 2) NOT NULL, description LONGTEXT NOT NULL, INDEX IDX_D34A04AD12469DE2 (category_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE product ADD CONSTRAINT FK_D34A04AD12469DE2 FOREIGN KEY (category_id) REFERENCES category (id);
在您的产品实体中添加以下代码,然后为db和cache运行这些命令,然后您将在表中看到这一列。(通过命令创建实体时无法创建映射,因此必须手动为这些列创建变量。)
/**
* @var categoryId
*
* @ORM'Column(name="category_id", type="integer" , nullable = false)
*/
private $categoryId;