Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top