Setting the Doctrine_Collection key mapping attribute in schema.yml

In Doctrine 1.2, you can configure Key Mapping for a table in which Doctrine_Collection objects created by this table fill keys from a specific column in each record in the collection.

An example from the above documentation:

You can display a column of names:

 // test.php // ... $userTable = Doctrine_Core::getTable('User'); $userTable->setAttribute(Doctrine_Core::ATTR_COLL_KEY, 'username'); 

Now user collections will use name column values ​​as element indices:

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

Is there any way to set this in the schema.yml file?

+6
source share
2 answers

When studying a similar problem, I came across in this example :

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

Applying the same principle with the coll_key attribute gives the following:

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

After assembly, we can verify that the attribute has been accepted:

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

However, there is one caveat. You must explicitly create the column that you want to use, otherwise Doctrine throws 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 need to explicitly specify the slug column, although the Sluggable template usually creates it for you automatically:

 User: actAs: Sluggable: ~ columns: ... slug: type: string(50) unique: true attributes: coll_key: slug 
+5
source

If possible, this is not well documented. You can specify table parameters in a 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 could not determine exactly where the parameters are processed in the code, but you can do this with xdebug step-by-step debugging.

Good luck, and tell me to use it if one of these attempts works.

0
source

Source: https://habr.com/ru/post/895493/


All Articles