在Doctrine中定义Join Table的结构

I have a ManyToMany relationship defined between user and content like so:

/**
 * @ORM\ManyToMany(targetEntity="Acme\UserBundle\Entity\User")
 * @ORM\JoinTable(name="acme_content_likes")
 */
private $user_likes;

Everything works great, if a User likes a content the relationship is written to the table. But join table only has two columns, user_id and content_id. I would really need the date and time on when was the content liked (for statistical reasons).

No problem here also, I created TIMESTAMPABLE on UPDATE colum on the table (through phpMyAdmin), so date is inserted every time new like is added.

The problem is when I try to run a SCHEMA update in console:

php app/console doctrine:schema:update --force

The operation then drops the newly created TIMESTAMPABLE column in database:

ALTER TABLE acme_content_likes DROP date;

Is there a way could configure my Entity so I would be able to keep the Date inside Join table even on SCHEMA update?

Thank you for your help!

I don't think a simple join table is your solution, even if you do want to record the date at which the tables were joined.

Possible solutions could be:

  • have a logging system when users change certain points it logs when and what the change was.
  • create the join table yourself with two many to one relationships if you need to record more data in the join table.

I would opt for the second. (I have actually done this)


Doctrine is correct in not letting you mess with the join table.


Edit to make more useful.

I have used in the past a symfony bundle which logs all doctrine actions for you, it is loggable extension