0

I have a table already created in my database with 2 keys, one a PRI key, the other a MUL key. How do I tell Doctrine to mark the column with the MUL key as a key? If I understand correctly, @ID won't work because it's not a primary key, and @Index won't work because the table is already created.

The code currently looks like this:

/**
 * @ORM\Entity
 * @ORM\Table(name="mytablename")
 **/
class MyTablename implements IMyTablename
{
    /**
     * @ORM\Id
     * @ORM\Column(type="string", name="id", length=40)
     */
    protected $id;

    /**
     * @ORM\Column(type="string", name="ccode", length=5)
     */
    protected $ccode;

    ...

}

And I want to mark ccode as a key.

Miryafa
  • 163
  • 1
  • 2
  • 15
  • If you don't want Doctrine's SchemaTool to manage your table schema, what's the point of indicating an index to Doctrine ? Doctrine just won't use this information. – marc Jul 09 '16 at 19:39
  • @marc What do you mean? I'm unfamiliar with how Doctrine uses the ID tag, but I know it uses Column tags even after the table has been created to parse the input from a table, e.g. date vs datetime. – Miryafa Jul 11 '16 at 13:07
  • Could you elaborate on "@Index won't work because the table is already created" ? – marc Jul 11 '16 at 18:30
  • @marc my understanding is that I could use "@Index" to set multiple keys, but that "@Index" is only used for table generation – Miryafa Jul 11 '16 at 19:54
  • Yes, you are right. At the table creation, your annotation is used by the SchemaTool to add an index on the specified columns. After that, your DBMS will take advantage of it during certain requests but that's all. What more are you awaiting from Doctrine ? – marc Jul 12 '16 at 10:18
  • @marc I'm not sure what you mean by awaiting, but I don't want anything else in particular from Doctrine - I just want my Entities to work the way they should. My understanding was that for that to happen, I need to mark all keys as keys in the entity. – Miryafa Jul 12 '16 at 13:05

1 Answers1

2

Doctrine allows you to define indexes (UNIQUE, INDEX and even FULLTEXT since Doctrine 2.5) through the @Index annotation. As a PRIMARY key is mandatory on each entity, it has a special @Id annotation. That's all.

But since you already mentioned it in your question, I guess there is a misunderstanding about the true role played by a MUL key, ie. a basic INDEX. In particular, this type of index concerns only performance and cannot be required by your entities to work correctly (except for performance issues of course). I encourage you to read more literature about indexes or to clarify your question.

marc
  • 330
  • 1
  • 8
  • Could you point me toward said literature? The Doctrine documentation I read didn't mention any of that. – Miryafa Jul 14 '16 at 14:24
  • You will find [here](http://stackoverflow.com/questions/707874/differences-between-index-primary-unique-fulltext-in-mysql) a well written description of the different indexes available. If you want more details, the MySQL official documentation has a section dedicated to [optimization and indexes](https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html). – marc Jul 14 '16 at 15:35
  • Thanks. Just to be clear, you're saying there is no way to define non-primary indexes for an already-created table, is that right? – Miryafa Jul 14 '16 at 16:42
  • Doctrine can still modify your table after its creation thanks to SchemaTool update command: `php doctrine orm:schema-tool:update`. Note that it is not recommended in production, see [Doctrine migrations](http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/reference/introduction.html) instead. – marc Jul 14 '16 at 17:12