From ac1c0597ba7a80ed80119301b3fc391dc0fa0216 Mon Sep 17 00:00:00 2001 From: Ty Kayn Date: Thu, 15 Mar 2018 16:18:06 +0100 Subject: [PATCH] products relations mapped well --- .idea/workspace.xml | 187 ++++++++++------------- src/Entity/Product.php | 6 +- src/Entity/ProductCategory.php | 16 +- src/Entity/SellRecord.php | 21 +-- src/Migrations/Version20180315151516.php | 54 +++++++ 5 files changed, 162 insertions(+), 122 deletions(-) create mode 100644 src/Migrations/Version20180315151516.php diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 162fd7a9..5fda3cf2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,12 +2,10 @@ - - - - - + + + - + - - + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -88,28 +64,18 @@ - - + + - - - - - - - - - - - + - - + + @@ -118,7 +84,7 @@ - + @@ -130,7 +96,11 @@ db_user + ManyToOne + + $PROJECT_DIR$/src/Entity + @@ -330,12 +301,6 @@ - - - - - - @@ -399,27 +364,27 @@ 1520949897852 - + - - - + + @@ -428,7 +393,6 @@ - @@ -574,6 +538,7 @@ + @@ -633,22 +598,6 @@ - - - - - - - - - - - - - - - - @@ -677,22 +626,6 @@ - - - - - - - - - - - - - - - - @@ -701,10 +634,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + diff --git a/src/Entity/Product.php b/src/Entity/Product.php index 5e2e2903..b77d8bdc 100644 --- a/src/Entity/Product.php +++ b/src/Entity/Product.php @@ -25,9 +25,13 @@ class Product { */ private $price; /** - * @ORM\ManyToOne(targetEntity="ProductCategory", mappedBy="products") + * @ORM\ManyToOne(targetEntity="ProductCategory", inversedBy="products") */ private $category; + /** + * @ORM\OneToMany(targetEntity="SellRecord", mappedBy="products") + */ + private $sellRecords; /** * @return mixed diff --git a/src/Entity/ProductCategory.php b/src/Entity/ProductCategory.php index 1db00d85..95312bcd 100644 --- a/src/Entity/ProductCategory.php +++ b/src/Entity/ProductCategory.php @@ -21,10 +21,24 @@ class ProductCategory { private $name; /** - * @ORM\OneToMany(targetEntity="Product", inversedBy="category") + * @ORM\OneToMany(targetEntity="Product", mappedBy="category") */ private $products; + /** + * @return mixed + */ + public function getProducts() { + return $this->products; + } + + /** + * @param mixed $products + */ + public function setProducts( $products ) { + $this->products = $products; + } + /** * @return mixed */ diff --git a/src/Entity/SellRecord.php b/src/Entity/SellRecord.php index c0c2698d..e5d9b4b3 100644 --- a/src/Entity/SellRecord.php +++ b/src/Entity/SellRecord.php @@ -9,16 +9,9 @@ use Doctrine\ORM\Mapping as ORM; */ class SellRecord { /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") + * @ORM\ManyToOne(targetEntity="Product", inversedBy="sellRecords") */ - private $id; - - /** - * @ORM/ManyToOne(targetEntity='Product') - */ - private $product; + private $products; /** @@ -45,15 +38,15 @@ class SellRecord { /** * @return mixed */ - public function getProduct() { - return $this->product; + public function getProducts() { + return $this->products; } /** - * @param mixed $product + * @param mixed $products */ - public function setProduct( $product ) { - $this->product = $product; + public function setProducts( $products ) { + $this->products = $products; } /** diff --git a/src/Migrations/Version20180315151516.php b/src/Migrations/Version20180315151516.php new file mode 100644 index 00000000..4ea52e32 --- /dev/null +++ b/src/Migrations/Version20180315151516.php @@ -0,0 +1,54 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE facture DROP FOREIGN KEY FK_FE86641019EB6921'); + $this->addSql('ALTER TABLE transaction DROP FOREIGN KEY FK_723705D1C54C8C93'); + $this->addSql('CREATE TABLE product_category (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); + $this->addSql('CREATE TABLE product (id INT AUTO_INCREMENT NOT NULL, category_id INT DEFAULT NULL, name VARCHAR(100) NOT NULL, price NUMERIC(10, 2) DEFAULT NULL, INDEX IDX_D34A04AD12469DE2 (category_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); + $this->addSql('CREATE TABLE sell_record (date DATETIME NOT NULL, products_id INT DEFAULT NULL, INDEX IDX_F9F12A026C8A81A9 (products_id), PRIMARY KEY(date)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); + $this->addSql('ALTER TABLE product ADD CONSTRAINT FK_D34A04AD12469DE2 FOREIGN KEY (category_id) REFERENCES product_category (id)'); + $this->addSql('ALTER TABLE sell_record ADD CONSTRAINT FK_F9F12A026C8A81A9 FOREIGN KEY (products_id) REFERENCES product (id)'); + $this->addSql('DROP TABLE client'); + $this->addSql('DROP TABLE facture'); + $this->addSql('DROP TABLE transaction'); + $this->addSql('DROP TABLE transaction_type'); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE product DROP FOREIGN KEY FK_D34A04AD12469DE2'); + $this->addSql('ALTER TABLE sell_record DROP FOREIGN KEY FK_F9F12A026C8A81A9'); + $this->addSql('CREATE TABLE client (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, address VARCHAR(500) DEFAULT NULL COLLATE utf8_unicode_ci, type VARCHAR(255) DEFAULT NULL COLLATE utf8_unicode_ci, pay_delay INT DEFAULT NULL, UNIQUE INDEX UNIQ_C74404555E237E06 (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); + $this->addSql('CREATE TABLE facture (id INT AUTO_INCREMENT NOT NULL, client_id INT DEFAULT NULL, title VARCHAR(255) DEFAULT NULL COLLATE utf8_unicode_ci, description LONGTEXT DEFAULT NULL COLLATE utf8_unicode_ci, days DOUBLE PRECISION NOT NULL, total_ht DOUBLE PRECISION NOT NULL, total_ttc DOUBLE PRECISION DEFAULT NULL, sent_date DATETIME DEFAULT NULL, paid_date DATETIME DEFAULT NULL, days_to_pay INT DEFAULT NULL, INDEX IDX_FE86641019EB6921 (client_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); + $this->addSql('CREATE TABLE transaction (id INT AUTO_INCREMENT NOT NULL, type_id INT DEFAULT NULL, total_ht DOUBLE PRECISION NOT NULL, total_ttc DOUBLE PRECISION NOT NULL, title VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, date_paid DATETIME NOT NULL, date_creation DATETIME NOT NULL, INDEX IDX_723705D1C54C8C93 (type_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); + $this->addSql('CREATE TABLE transaction_type (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, UNIQUE INDEX UNIQ_6E9D69885E237E06 (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); + $this->addSql('ALTER TABLE facture ADD CONSTRAINT FK_FE86641019EB6921 FOREIGN KEY (client_id) REFERENCES client (id)'); + $this->addSql('ALTER TABLE transaction ADD CONSTRAINT FK_723705D1C54C8C93 FOREIGN KEY (type_id) REFERENCES transaction_type (id)'); + $this->addSql('DROP TABLE product_category'); + $this->addSql('DROP TABLE product'); + $this->addSql('DROP TABLE sell_record'); + } +}