Data relation in a single table with ORM / Annotation
Assuming a courses table like this:
create table courses (
course_id int primary key,
course_name char(50)
);
The PREREQUISITES table would look like this:
create table prerequisites (
course_id int references courses,
prereq_course_id int references courses,
primary key (course_id, prereq_course_id)
);
but this does not comply with the organic generation of the tables in the ORM
:
An example is that in annotation
relations are implemented with this syntax:
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserCertification", mappedBy="user")
*/
private $usercertification;
and then the command is run:
php bin/console doctrine:schema:update --force
and there are relationships or tables related to the following scheme:
DROP TABLE IF EXISTS `user_certification`;
CREATE TABLE IF NOT EXISTS `user_certification` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`certification` date NOT NULL,
`expiration` date NOT NULL,
`evaluation` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`listcertification_id` int(11) DEFAULT NULL,
`eventcertification_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `usercertification` (`id`,`certification`,`expiration`,`evaluation`),
KEY `IDX_82B2C025A76ED395` (`user_id`),
KEY `IDX_82B2C0255586BCD3` (`listcertification_id`),
KEY `IDX_82B2C0257F4F8556` (`eventcertification_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
if I manually create the schema and then run this command to import the entities:
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
The result is this (it looks nothing like it):
/**
* @var int
*
* @ORM\Column(name="prereq_course_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $prereqCourseId;
my doubt is what is the syntax for the relation expressed in the first two tables using the ORM / annotation
instead of use the importation tool??
the scenario is as follows: I have a table with a list of courses ... but some of these courses have levels 1,2,3; then, in order to participate in the level 2 course, a prerequisite is required, which would be the previous level (1) ...
- Basic First Aid.
- Intermediate First Aid.
- Advanced First Aid.
But I also have another scenario in the same table:
A "jungle survival" course may have prerequisites for courses such as "Intermediate First Aid", "Basic Telecommunications" and "rappelling for rescuers", in this case the requirements are not necessarily a previous level, they are simply requirements of previous courses.
So I thought that I would create a table to save the prerequisites but I don't quite understand how I should write the annotation of the ORM to generate said table and relation.
which looks nothing like onetomany or manytoone manual annotation
/**
* @var int
*
* @ORM\Column(name="prereq_course_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
When I generate it with import, it brings me something quite strange that does not resemble what is manually written.