I've imported two tables with data that were given to me into the Symfony database and want to join the two by a foreign key.
Table Main has columns: ID, indate, dano, partno, batchno
Table Sub has columns: ID, main_id, rackno, code, qty
I want to join the tables together as a one to many(?) since one main can have many subs. For example, one dano can have many rackno. I am guessing the *main_id* in Table Sub and id in Table Main are the keys that will be used to join.
How do I go about joining the two with Doctrine? I have the tables separated by its own Entity files.
On Main.php
class Main
{
/**
* @var integer
* @ORM\ManyToOne(targetEntity="Sub", mappedBy="mainId")
*/
private $id;
/**
* @var \DateTime
*/
private $indate;
/**
* @var string
*/
private $dano;
/**
* @var string
*/
private $partno;
/**
* @var integer
*/
private $batchno;
On Sub.php:
class Sub
{
/**
* @var integer
*/
private $id;
/**
* @var integer
* @ORM\ManyToOne(targetEntity="Main", inversedBy="id")
* @ORM\JoinColumn(name="id", referencedColumnName="id")
*/
private $mainId;
/**
* @var string
*/
private $rackno;
/**
* @var string
*/
private $code;
/**
* @var integer
*/
private $qty;
You can't process like this. If you need an ManyToOne relation, you need this:
In your Main class:
/**
* @ORM\OneToMany(targetEntity="Sub", mappedBy="main")
*/
private $sub;
And in your Sub class:
/**
* @ORM\ManyToOne(targetEntity="Main", inversedBy="sub")
* @ORM\JoinColumn(name="main_id", referencedColumnName="id")
*/
private $main;
And don't forget to sync your database ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.