views:

204

answers:

2

Hi everyone!

Can anyone give me an example of how to implement a class self-relation?

Thanks in advance, Best regards!

A: 

Hi!

I want something like this: - a page that have related subpages (like childrens) in a context of a menu and it´s options.

Example: - travels (menu option) - where (suboption) - ...

I have the following schema.yml defintion but it doesn´t fit in my needs:

 Page:
  actAs:
    I18n:
      fields: [name,html,urlShortDesc]
  columns:
    name: string
    gender: 
      type: enum
      values: [html, photoGallery]
      default: html
    html: string
    urlShortDesc: string
    section_id: 
      type: integer
      notnull: true
  relations:
    Section: 
      foreignAlias: Pages
    SubPages:
      class: Page
      local: parent_id
      foreign: child_id
      refClass: PageReference

PageReference:
  columns:
    parent_id:
      type: integer
      primary: true
    child_id:
      type: integer
      primary: true

Thanks in advance, Best regard!

Rui Gonçalves
A: 

You need something like this:

MenuOption:
  tableName: MenuOption
  columns:
    id:
      type: integer(4)
      autoincrement: true
    child_menu_option_id:
      type: integer(4)
      null: true
    ... (more columns)
  relations:
    ChildMenuOptions:
      class: MenuOption
      foreignAlias: News
      foreign: id
      local: child_menu_option_id

You'd refer to your children like this once you've loaded a MenuOption object:

$menuOption = Doctrine_Query::create()
  ->from("MenuOption")
  ->where("stuff here")
  ->fetchOne();
$children = $menuOption->ChildMenuOptions; // this will be a Doctrine_Collection object
Chris Williams