0
There is the following relationship between two entities:
class Clients
{
//...
/**
* Dealership id
*
* @ManyToOne(targetEntity="Dealerships", inversedBy="clients", fetch="EXTRA_LAZY")
* @JoinColumn(name="dealership_id", referencedColumnName="id")
*/
private $dealershipId;
//...
}
Note that there is a relationship with the entity Dealerships
across the field dealership_id
.
In the entity Dealerships
I have the following structure:
class Dealerships
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="active", type="integer")
*/
private $active;
//...
}
Notice that the property exists active
.
If there is a record with ID 1 in the table Dealerships
, in the object clients
just do the following:
$client = new Clients();
$client->setDealershipId(1);
The code above works because there is ID 1, but in the relation I want to consider 1 more field, the active
, that is, only accept the relationship if the field active
is equal to 1.
You can do this using Doctrine?
Rodrigo, I did not understand your statement not to use ids in the relations between objects. How so?
– Filipe Moraes
What I meant is that normally in the relations between objects you can omit the id’s, the Doctrine is intelligent enough to know how the relationship between one object and another takes place. Prefer to use methods such as
setDealership
andsetClient
.– Rodrigo Rigotti
Thank you so much for your help. I use the object instead of the ID, but to simplify the example I put the ID right away. Thanks for the help.
– Filipe Moraes