4

A few minutes ago I found an error in Doctrine2.

I added some fields in my entity, then refresh the mysql table and going to persist ago I noticed that the new data is not stored in the table.

Tried a var_dump the value of "->getXXX()" and the result was correct.

Here is the code used:

$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();

Thank's and regards!


Edited

Information about my Entity:

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 town are id's from tables with the same names. I've tried to change the type of the field with a ManytoOne assosiation, but it gets me an error on the Doctrine2 generated SQL. It's seem to be a cache error, but I can't solve it.

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
Mateu
  • 73
  • 1
  • 8
  • 2
    did you try to clear your cache? – Snroki Dec 12 '12 at 16:22
  • Can you show your Doctrine config (annotations, yml, etc) for the entity? Are you sure you setup the proper 'ORM/Column' for the new fields? – lifo Dec 12 '12 at 16:40
  • It is not enough to clear the metadata cache with console. In development you can use the VoidCache to disable caching. This helped me – sgotre Nov 27 '15 at 11:36

2 Answers2

2

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

$ apachectl -M | grep apc
Syntax OK
Steve Tauber
  • 9,551
  • 5
  • 42
  • 46
0

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.

lifo
  • 2,791
  • 1
  • 25
  • 32
  • Thanks a lot! I'm developing over Docrtine2 by 1 year and I know how update schema. I've tried ManyToOne relationship, but Doctrine2 gave me errors when I make a simple query like this: $user = $em->getRepository('HomeBundle:Users')->findOneBy(array('id' => $uid)); Doctrine gave me a SQLSTATE[42S22] error, because the country field was renameb to contruy_id (ManytoOne auto column name). With the other tables had no errors, I don't understand why I get this error with this table. – Mateu Dec 12 '12 at 19:43
  • 1
    If you're using APC or memcached, etc, you may want to try restarting the apache process (if you're able to). I've run into problems in the past after updating a Doctrine mapping I sometimes get SQL errors because the doctrine classes were cached by APC and the only fix was to reload apache (which clears the APC cahce). – lifo Dec 12 '12 at 19:49
  • 1
    Ohh!! Thank's! It was the solution! I knew the problem was cache but did not think to restart apache wiped the APC. Thanks again! – Mateu Dec 12 '12 at 19:52