I have a question to now what is the best solution to make relations between tables: I have :
My question is that is it a good practice to have both foreign keys in the client table or just the key of delegation_id is sufficient for indexing and searching in the client table (of course with join tables)
to explain more each delegation belongs to a sector. each sector belongs to a zone and each Client belongs to a delegation that belongs to a sector that is grouped in a geographical area
this is my Entity files : Class client
class Client {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="firstName", type="string", length=255)
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="lastName", type="string", length=255)
*/
private $lastName;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="sector", type="string", length=255)
*/
private $sector;
/**
* @var int
*
* @ORM\Column(name="delegation", type="integer")
*/
private $delegation;
}
Class sector
class Sector {
/**
* @ORM\Id
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Delegation" ,mappedBy="sector")
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
private $name;
/**
* @Assert\NotBlank()
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Zone", inversedBy="id")
* @ORM\JoinColumn(nullable=false)
*/
private $zone;
}
class Delegation
class Delegation {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Sector", inversedBy="id" )
* @ORM\JoinColumn(nullable=false)
*/
private $sector;
}
Based on your updated question -
If each Client
belongs to a Delegation
, the Client
entity should have a delegation_id
foreign key.
If each Delegation
belongs to a Sector
, the Delegation
entity should have a sector_id
foreign key.
And if each Sector
belongs to a Zone
, the Sector
entity should have a zone_id
foreign key.
You would therefore remove your sector_id
foreign key from your Client
entity, and use the relationship through the Delegation
entity to find all Client
entities within a given Sector
, if this were ever required.
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.