Search code examples
phpsymfony1doctrine

Getting a foreign key field other than id in symfony


I have a table 'comment', investigation that has a field referencing another table 'sf_guard_user'. At the moment when I put

<?php echo $investigationComment->getUserId() ?>

I get the id value of the foreign table row. I want to be able to get the name field value.

Somewhere else in my project I was able to omit id from getUserId() and then put->getName() and it brought me that field value, but for this it's not doing it for some reason. How can I get the name value for the foreign key row?


Solution

  • Just get the relation object first and then call for any property of that relation.

    <?php echo $comment->getUser()->getName() ?>
    

    This will work if you defined an alias 'User' in your relation like this:

    //config.yml
    Comment:
      relations:
        sfGuardUser:
          alias: User
          foreignAlias: Comments
          local: user_id
          foreign: id
    

    or

    //config.yml
    Comment:
      relations:
        User:
          class: sfGuardUser
          foreignAlias: Comments
          local: user_id
          foreign: id