Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2010-01-01 14:12:10

jiewmeng
Guest

Doctrine YAML Schema File - Relations

i am abit confused with foreignXXX vs 'local' attributes/options inside YAML Schema. specifically in the relations node now.

when i have something like:

ProjectCategory:
  columns:
    id:
      type: integer
      primary: true
    name: string(12)
  relations:
    Project:
      local: id // refers to ProjectCategory.id
      foreign: category_id // refers to Project.category_id
      type: many // refers to? many projects?
      foreignType: one // refers to? many projects ... to one category?
Project:
  columns:
    id:
      type: integer
      primary: true
    category_id: integer
    name: string(60)
  relations:
    Category:
      class: projectCategory
      local: category_id
      foreign: id
      type: one

type (& foreignType) gives me the most confusion.

local refers to the local class's field and foreign refers to the foreign 1. right? for eg. under ProjectCategory's relations node, local refers to ProjectCategory's id field and foreign refers to Project's category_id field.

but for type, it seems different. after some setting up databases/tables in phpMyAdmin then generate-yaml-db, discovered that when i say type:many in ProjectCategory/relations/Project it means many projects. right?

at first assuming i take type just like local where local in ProjectCategory means ProjectCategory, so type:many will mean many ProjectCategory's.

#2 2010-01-01 17:38:53

ryan.horn
Member
From: Buffalo, NY
Registered: 2009-09-08
Posts: 41
Website

Re: Doctrine YAML Schema File - Relations

It is a little confusing at first but makes sense when you think about it. Doctrine only requires that you define the relationship on the model where the foreign key exists. It will automatically detect and build the relationship on the other end. Example:

[code="yaml"]
User:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    username:
      type: string(255)
    client_id:
      type: integer(4)
  relations:
    Client:
      local: client_id
      foreign: id
      type: one
Client:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    name:
      type: string(255)
[/code]

You don't need to specify the relationship between "Client" and "User" in the "Client" definition. Doctrine will do this for you automatically. However, in cases where you need to customize the relationship attributes on the "Client" end, doctrine allows you to define the foreign attributes on the "User" end. This is so you can keep your relationship information in one place, making it easier to manage.

Example: You want to change the relation type on the "Client" end to "many" and you want to change the alias on the "Client" end to "Users"


[code="yaml"]
User:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    username:
      type: string(255)
    client_id:
      type: integer(4)
  relations:
    Client:
      local: client_id
      foreign: id
      type: one
      foreignType: many
      foreignAlias: Users
Client:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    name:
      type: string(255)
[/code]

This is saying a "User" has one "Client" aliased by "Client" in the "User" model class. And "Client" has many "User" aliased by "Users" in the "Client" model class.

I hope that clears it up a little bit.

Last edited by ryan.horn (2010-01-01 17:41:53)

Offline

Board footer

Powered by FluxBB