Setting up Doctrine_Collection key mapping attribute in schema.yml
Asked Answered
L

2

6

In Doctrine 1.2, it is possible to set up Key Mapping for a table where Doctrine_Collection objects created by that table will populate keys from a particular column in each record in the collection.

An example from the documentation linked above:

You may want to map the name column:

// test.php

// ...
$userTable = Doctrine_Core::getTable('User');

$userTable->setAttribute(Doctrine_Core::ATTR_COLL_KEY, 'username');

Now user collections will use the values of name column as element indexes:

// test.php

// ...
$users = $userTable->findAll();

foreach($users as $username => $user) {
    echo $username . ' - ' . $user->created_at . "\n";
}

Is there a way to set this up in a schema.yml file?

Lenrow answered 20/8, 2011 at 15:57 Comment(0)
L
5

While exploring a similar issue, I came across this example:

---
User:
  columns:
    ...
  attributes:
    export: all
    validate: true

Applying the same principle with the coll_key attribute yields this:

User:
  columns:
    ...
  attributes:
    coll_key: username

We can verify after doing a build that the attribute was accepted:

$this->setAttribute(Doctrine_Core::ATTR_COLL_KEY, 'username');

There is one caveat, though. You have to explicitly create the column that you want to use, or else Doctrine will throw an error during the build process:

User:
  actAs:
    Sluggable:  ~
  columns:
    ...
  attributes:
    coll_key:  slug
$ symfony doctrine:build --all --no-confirmation
>> doctrine  Dropping "doctrine" database
>> doctrine  Creating "dev" environment "doctrine" database
>> doctrine  generating model classes
>> file+     /tmp/doctrine_schema_60681.yml
   ...
>> doctrine  generating form classes

  Couldn't set collection key attribute. No such field 'slug'.

To get the above to work, you would need to explicitly specify the slug column, even though the Sluggable template normally creates it for you automatically:

User:
  actAs:
    Sluggable:  ~
  columns:
    ...
    slug:
      type:    string(50)
      unique:  true
  attributes:
    coll_key:  slug
Lenrow answered 21/8, 2011 at 18:11 Comment(0)
U
0

If it is possible, it is not well documented. You can specify options for a table in the table definition, like this Knowing that

const ATTR_COLL_KEY             = 108;

I would try :

User:
  options:
    attr_coll_key: username

then

User:
  options:
    attrCollKey: username

or even

User:
  options:
    108: username

I couldn't find precisely where in the code the options are handled, but you could do this with xdebug step-by-step debugging.

Good luck, and tell use if one of these tries works.

Untidy answered 21/8, 2011 at 10:26 Comment(2)
Thanks for the suggestions; they were an excellent starting point! I was able to get it working by specifying it as an attribute rather than an option (see my answer for more info).Lenrow
@Phoenix: didn't event know you could set attributes in the schema, great job!Untidy

© 2022 - 2024 — McMap. All rights reserved.