Reputation:
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?
Upvotes: 6
Views: 1384
Reputation:
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
Upvotes: 5
Reputation: 23255
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.
Upvotes: 0