I want to generate Entities from an Existing Database by using Doctrine tools for reverse engineering
/*
* SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `country`
-- ----------------------------
DROP TABLE IF EXISTS `country`;
CREATE TABLE `country` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `provider`
-- ----------------------------
DROP TABLE IF EXISTS `provider`;
CREATE TABLE `provider` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `provider_country`
-- ----------------------------
DROP TABLE IF EXISTS `provider_country`;
CREATE TABLE `provider_country` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`providerId` int(11) unsigned NOT NULL,
`countryId` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_provider_has_id0_idx` (`providerId`),
KEY `fk_user_country_idx` (`countryId`),
CONSTRAINT `fk_rss_has_id` FOREIGN KEY (`providerId`) REFERENCES `provider` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_user_country` FOREIGN KEY (`countryId`) REFERENCES `country` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS=1;
*/
you can ask Doctrine to import the schema and build related entity classes by executing the following two commands.
1 $ php app/console doctrine:mapping:import AcmeBlogBundle annotation
2 $ php app/console doctrine:generate:entities AcmeBlogBundle
but now the doctrine detect only ManyToOne relation in many side only "ProviderCountry" table
if i need to add the OneToMany relation i have to add the annotation by my hand by adding the follwing annotation
in Provider.php add
/**
* @ORM\OneToMany(targetEntity="ProviderCountry", mappedBy="providerId", cascade={"persist"})
*/
private $datas;
in ProviderCountry.php add
/**
* @var Provider
*
* @ORM\ManyToOne(targetEntity="Provider", inversedBy = "datas")
* @ORM\JoinColumn(name="providerId", referencedColumnName="id")
*/
private $userid;
so how can I genrate One-To-Many annotation by doctrine command
Simply this is change inside doctrine orm lib,
you can fix that by changes in
vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php
replace the following statements
change to
now if you run doctrine:mapping:import
you will find OneToMany annotation.
then run doctrine:generate:entities
you will find A unidirectional relationship on both sides.