views:

63

answers:

1

So I have 2 models. Users and Groups. Each group has a user as the creator and a group has many users. The FK of these tables are set up correctly, but I was wondering if there was an easier way to get all related FK objects from other objects. For example, with a group object, is there a built in method to get the user object of the creator? Or for a user, is there a built in method to get all group object that he belongs to? I couldn't find out how to do this with the documentation on the symfony page. From what I see I feel like I need to create methods and use doctrine to access the appropriate tables using the current objects id and so on.

Thanks!

Some sample schema:

Group:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(500), notnull: true }
    image: { type: string(255) }
    type: { type: string(255), notnull: true }
    created_by_id: { type: integer }
  relations: 
    User: { onDelete: SET NULL, class: User, local: created_by_id, foreign: id, foreignAlias: groups_created }
+3  A: 

You need to show us your code for a decent answer, but it will be something like below.

YAML:

Group:
    columns:
        ..........
        creator_id:     { type: integer(4), notnull: true }
    relations:
        Creator:        { class: User, local: creator_id, foreign: id }

PHP:

$user = $group->getCreator();
Coronatus
Thanks! so is there any part of the documentation that discusses how to use these "magic" methods? Also what if I wanted to change the way to call it. Instead of getCreatedBy(). Would I make some sort of alias or just another getter method that wraps it? Thanks again!
Daniel Hertz
See http://www.symfony-project.org/jobeet/1_4/Doctrine/en/03 and http://www.symfony-project.org/jobeet/1_4/Doctrine/en/06
Coronatus
Sorry to bother you again. I am trying to access the creator by doing $this->getCreatedBy() and it doesn't work. Any ideas?
Daniel Hertz
The method will be `getCreator()`, not `getCreatedBy()`
Coronatus