views:

599

answers:

2

I have a problem with the Admin Generator. The Table of Pages have the column sf_guard_user_id. The rest of the table looks as this part of the generator.yml in the line display,

list:
  title: Pages
  display:      [=title, sfGuardUser, views, state, privacy, created_at, updated_at]
  sort:         [created_at, desc]
  fields:
    sfGuardUser: { label: Author }
    created_at:  { label: Published, date_format: dd.MM.y }
    updated_at:  { label: Updated, date_format: dd.MM.y }
  table_method: retrieveUserList

Now the sf_guard_user_id is been replaced and the username ist displayed. Don't get me wrong, that works fine. But how can I get other variables from the sfGuarsUser relation? When I only add salt or another variable to display I get this,

Unknown record property / related component "salt" on "simplePage"

But why?

+2  A: 

The name of the user gets displayed because the _toString() of the sfGuardUser class returns the username. And I guess the related name from simplePage to sfGuardUser is sfGuardUser.

But when you only specify salt, the generator looks up whether simplePages has such an property or not. The admin generator does not know that you mean the salt field of sfGuardUser (how should it?).


You can use partial fields to display that value. Therefore you have to specify the field with an underscore in front (_salt) e.g.:

display:      [=title, sfGuardUser, _salt, views, state, privacy, created_at, updated_at]

The output for _salt is determined by the file modules/<your_module_name_here>/templates/_salt.php.

In this file, you have access to the current object via a variable named by the name of your model class ($simple_page or $simplePagein your case).

So you can put following code into _salt.php:

<?php echo $simple_page->getSfGuardUser()->getSalt(); ?>

I am not sure about the getSfGuardUser() method but I guess you know how to access the associated user from your page model.

Felix Kling
A: 

The admin generator can't delve into related object properties with the default configuration.

The reason that your sfGuardUser field is displaying correctly is because Doctrine defined a getSfGuardUser method in your 'page' class when it was told the two objects were related and the admin generator calls that function to display its column (technically calling the __toString() method on the resulting 'sfGuardUser' object returned by the method).

To get a property of one of your object's related models you can either define a custom getter in your page object:

public function getSfGuardUserSalt() {
  return $this['sfGuardUser']['salt'];
}

and then reference that in your generator.yml:

display: [title, sfGuardUser, sfGuardUserSalt, ...]

or create a partial to handle your custom rendering:

modules/pages/templates/_sfGuardUser.php:

<?php echo $page['sfGuardUser']['name']; ?><br />
<?php echo $page['sfGuardUser']['salt']; ?>

and then include it in your generator.yml with a preceding underscore:

display: [title, _sfGuardUser, views, ...]
Cryo