1

Topic: ZC40 – Doctrine Relations and Data Fixtures

This episode of zendcasts will cover how we can write Data Fixtures and extend our data model to include a simple one-to-many using Doctrine's schema.yml file. I also noticed in the code of my last example that Model loading wasn't working properly. I cover the fix in the video, however I'd like to highlight it here too. In your _initDoctrine() method, make sure you load the models:
        Doctrine::loadModels($doctrineConfig['models_path]);

also, I'd like to extend a welcome to the folks at ServerGrove Networks for sponsoring this episode of Zendcasts. ServerGrove specializes in Zend Framework hosting and they've offered a 10% rebate on hosting with coupon code "zc". If you're looking for a host, be sure to check them out.

2

Re: ZC40 – Doctrine Relations and Data Fixtures

at 18:31 in the video it looks liek you have a typo "DOctrine::"

3

Re: ZC40 – Doctrine Relations and Data Fixtures

Thanks for another great Episode.
I saw you write static functions in your model. I honestly do not know, but will that not be a problem later with a unit tests ?
Is static method behaving the same as object->method call in PhpUnit ?
Just want to make sure, it would be very interesting to hear your answer wink

4

Re: ZC40 – Doctrine Relations and Data Fixtures

static methods are fine when you're trying to encapsulate a concept inside a method. They cause problems when people use them to assignment of objects, however I'm using them for one-time invocation. When I write the public static methods in the model, I'm treating the model as a flat interface. If queries are becoming increasingly complex, it might be worth looking at "Aggregate Roots" and the Repository pattern, however these patterns add additional layers which might be unnecessary.

5

Re: ZC40 – Doctrine Relations and Data Fixtures

Thanks, that confirms my understanding that static functions are not always the right solution then things go complex.

Jon Lebensold wrote:

static methods are fine when you're trying to encapsulate a concept inside a method. They cause problems when people use them to assignment of objects, however I'm using them for one-time invocation. When I write the public static methods in the model, I'm treating the model as a flat interface. If queries are becoming increasingly complex, it might be worth looking at "Aggregate Roots" and the Repository pattern, however these patterns add additional layers which might be unnecessary.

6

Re: ZC40 – Doctrine Relations and Data Fixtures

How do you use functions (like NOW()) in the fixture?
And do you know why my timestamp field ends up being a datetime field in the database?

7

Re: ZC40 – Doctrine Relations and Data Fixtures

Data fixtures are meant to be static. The whole point is that you have your application data at a "fixed" state and can thus properly test to ensure that any logic which interacts with the data is working properly. So instead of using NOW() in your fixture, you would use what NOW() might output at any given point in time (which is in datetime format for mysql).

Ex: 2010-01-21 10:01:49

Also, timestamp is a data type internal to doctrine. The actual column type that is generated when doctrine creates your tables depends on the database driver being used. For most (including mysql), this is a column type of "datetime", which is a combination of the date and time (based on the IS0-8601 standard for formatting). See http://www.doctrine-project.org/documen … :timestamp

8

Re: ZC40 – Doctrine Relations and Data Fixtures

For building the db you are using this schema:

options:
  type: INNODB
  collate: utf8_general_ci
  charset: utf8
  
User:
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    name: string(300)
    email: string(300)
    phone: string(9)
    car_id: integer
  relations:
    Car:
      local: car_id
      foreign: id

Car:
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    brand: string(300)
  relations:
    Users:
      class: User
      foreign: car_id
      local: id
      type: many

An alternative is using the "detect_relations" option. The schema.yml file looks like this:

options:
  type: INNODB
  collate: utf8_general_ci
  charset: utf8
detect_relations: true  
  
User:
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    name: string(300)
    email: string(300)
    phone: string(9)
    car_id: integer

Car:
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    brand: string(300)

9

Re: ZC40 – Doctrine Relations and Data Fixtures

Hi, i've been following Zendcasts for a while now, but only just registered,  and i don't think i've seen this anywhere yet,

I know this might be an old topic, but this should be worth mentioning,

I don't think i am the only one who designs his/her databases with MySQL Workbench( http://wb.mysql.com/ ), and now i've recently picked up a very usefull workbench- plugin to use with doctrine.

This plugin allows you to export your database models to a yaml file for use with doctrine.

It should be noted that i havn't tested this yet with a live application, but the exported yaml files look promising.

(EDIT: it seems to be working, although i had a few issue's with accidently calling a field in a table 'order' which screwed up the generated sql)

the plugin can be found at: http://code.google.com/p/mysql-workbenc … ne-plugin/

Last edited by iiTyr (2010-03-27 13:30:06)

10

Re: ZC40 – Doctrine Relations and Data Fixtures

Hi iiTyr ,
Really thanks for your suggestion , its really nice idea and will ease the YAML Generation .
btw in the doctrine cli interface there are many helpful commands for reverse engineering like :

doctrine generate-models-db
doctrine generate-models-yaml
doctrine generate-sql
doctrine generate-yaml-db
doctrine generate-yaml-models


and Thanks for sharing us with this plugin smile
I  look forward to hear form you and others as well

11

Re: ZC40 – Doctrine Relations and Data Fixtures

iiTyr wrote:

I don't think i am the only one who designs his/her databases with MySQL Workbench( http://wb.mysql.com/ ), and now i've recently picked up a very usefull workbench- plugin to use with doctrine.

Wow! All these years and I had never taught to look for something other then phpmyadmin and custom mysql scripts. Thanks for making me discover this tool!!

12

Re: ZC40 – Doctrine Relations and Data Fixtures

Fordnox wrote:

Thanks for another great Episode.
I saw you write static functions in your model. I honestly do not know, but will that not be a problem later with a unit tests ?
Is static method behaving the same as object->method call in PhpUnit ?
Just want to make sure, it would be very interesting to hear your answer wink

I totally agree with you. These tutorials here are very helpful and informative. It helps me with my class in IT and in my research. I hope you will post more tutorials here. Thanks and keep it up.

I liked the kid who wrote me that he had to do a term paper on a modern poet and he was doing me because, though they say you have to read poems twice, he found he could handle mine in one try.
Howard Nemerov