Doctrine2 not saved in some fields

A few minutes ago I discovered an error in Doctrine2.

I added some fields to my object and then updated the mysql table and continue to persist. I noticed that new data is not stored in the table.

I tried var_dump value "-> getXXX ()", and the result was correct.

Here is the code:

$user->setName($name); $user->setSurname($surname); if($country) $user->setCountry($country->getId()); //New code if($province > 0) $user->setProvince($province); if($town > 0) $user->setTown($town); var_dump($user->getProvince(), $user->getTown()); //Works OK $em->persist($user); // Only persists values before Province. $em->flush(); 

Thanks and welcome!


Edited

Information about my property:

 class Users { /** * @var integer $id * * @ORM\Column(name="id", type="bigint") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $name * * @ORM\Column(name="name", type="string", length=50) */ private $name; /** * @var string $surname * * @ORM\Column(name="surname", type="string", length=95) */ private $surname; /** * @var string $mail * * @ORM\Column(name="mail", type="string", length=80, unique=true) */ private $mail; /** * @var string $password * * @ORM\Column(name="password", type="string", length=255) */ private $password; /** * @var $country * * @ORM\Column(name="country", type="integer") */ private $country; /** * @var $province * * @ORM\Column(name="province", type="integer") */ private $province; /** * @var $town * * @ORM\Column(name="town", type="integer") */ private $town; } 

Country, province and city are identifiers from tables with the same name. I tried to change the field type using the ManytoOne association, but it causes an error in Doctrine2 SQL generation. This seems to be a cache error, but I can't solve it.

+4
source share
2 answers

You have a lot to learn how Doctrine associations work. I suggest spending some time and really reading through the Doctrine Editing Documentation .

A custom object must not use integers. You will probably need the ManyToOne relationship for them (this means: each user can have exactly one country and one province). You should start thinking of things as objects and not try to skip identifiers

For instance:

 /** * @ORM\ManyToOne(targetEntity="Country") * @ORM\JoinColumn(name="countryId", referencedColumnName="id") */ private $country; /** * @ORM\ManyToOne(targetEntity="Province") * @ORM\JoinColumn(name="provinceId", referencedColumnName="id") */ private $province; 

targetEntity in each of them should be changed to everything that you cause.

If you try to make these changes, make sure you run the app/console doctrine:schema:update command so that your schema updates automatically.

Then you can do this in your controller:

 $country = new Country('US'); $entityManager->persist($country); $user->setCountry($country); 

Note. I did not assign a country identifier to the setCountry() method. I am passing the actual Country object. This is just an example, and in your actual controller, the $country variable here would be passed through a form or retrieved from a database, etc.

0
source

I had the same issue with Doctrine when using doctrine:schema:update . And, like @lifo, I had to restart apache. The difference is that I did not have apc, so I think Doctrine does some internal caching for the model.

 $ apachectl -M | grep apc Syntax OK 
+2
source

All Articles