You have a lot to learn on how Doctrine associations work. I suggest taking some time and really reading through the Doctrine mapping documentation.
Your User entity shouldn't be using integers like that. You probably want a ManyToOne
relationship for those instead (meaning: Each user can have exactly one country and one province). You should start thinking of things as objects and try not to pass around ID's
For example:
/**
* @ORM\ManyToOne(targetEntity="Country")
* @ORM\JoinColumn(name="countryId", referencedColumnName="id")
*/
private $country;
/**
* @ORM\ManyToOne(targetEntity="Province")
* @ORM\JoinColumn(name="provinceId", referencedColumnName="id")
*/
private $province;
The targetEntity
in each of those should be changed to whatever your entities are called.
If you attempt these changes make sure you run the app/console doctrine:schema:update
command to have your schema updated automatically.
Then you can do this in your controller:
$country = new Country('US');
$entityManager->persist($country);
$user->setCountry($country);
Note: I didn't assign the country ID to the setCountry()
method. I pass in the actual Country
object. This is just an example, and in your actual controller the $country
variable here would have been passed in via a form or fetched from the database, etc.