Reputation: 97
I have simple problem what I cannot understand why it's not working... Server: latest ZendServer CE, ZF: 1.11.11, Doctrine: 1.2.2
YAML schema:
Uzytkownik: tableName: uzytkownicy columns: id: type: integer fixed: false unsigned: true primary: true autoincrement: true uzytkownik_dane_id: type: integer login: type: string(64) notnull: true haslo: type: string(96) notnull: true email: type: string(192) notnull: true nazwa: type: string(64) relations: UzytkownikDane: local: uzytkownik_dane_id foreign: id foreignAlias: Dane foreignType: one UzytkownikDane: tableName: uzytkownicy_dane columns: id: type: integer fixed: false unsigned: true primary: true autoincrement: true imie: type: string(128) nazwisko: type: string(128) kraj: type: string(32) wojewodztwo: type: string(64) miejscowosc: type: string(128) adres: type: string(128) kod: type: string(16) telefon: type: string(16) relations: Uzytkownik: local: id foreign: uzytkownik_dane_id
Model generated via Doctrine Cli build-all-reload:
hasOne('Application_Model_UzytkownikDane as UzytkownikDane', array( 'local' => 'uzytkownik_dane_id', 'foreign' => 'id')); } }
As you can see doctrine Cli ignored my alias and setup default: Application_Model_UzytkownikDane as UzytkownikDane... Why? Secound thing is that (may it's important i don't know...) Doctrine Cli output me error when i build-all models form yml file:
build-all-reload - Are you sure you wish to drop your databases? (y/n) y build-all-reload - Successfully dropped database for connection named 'doctrine' build-all-reload - Generated models successfully from YAML schema build-all-reload - Successfully created database for connection named 'doctrine' SQLSTATE[HY000]: General error: 1005 Can't create table 'testdb.#sql-188_3a' (e rrno: 150). Failing Query: "ALTER TABLE uzytkownik ADD CONSTRAINT uzytkownik_uzy tkownik_dane_id_uzytkownik_dane_id FOREIGN KEY (uzytkownik_dane_id) REFERENCES u zytkownik_dane(id)". Failing Query: ALTER TABLE uzytkownik ADD CONSTRAINT uzytko wnik_uzytkownik_dane_id_uzytkownik_dane_id FOREIGN KEY (uzytkownik_dane_id) REFE RENCES uzytkownik_dane(id)
why? ofc all working relations working etc.. but alias not working and Cli outputs the error... Regards
Upvotes: 0
Views: 552
Reputation: 19999
Not sure why the foreignAlias
setting is not being respected, I'll check some code when I get home.
But I think the reason why you are getting the failed alter table
response is because of this:
Uzytkownik:
....
uzytkownik_dane_id:
type: integer
# This is missing the unsigned attribute, as set in `UzytkownikDane.columns.id`
# Also it may not hurt to add the fixed attribute as well, although I don't think it will matter
MySQL expects the datatypes of both columns to be the same, so if one is flagged as unsigned and the other is flagged as signed, you will get the SQL error you posted.
-- Update --
According to Doctrine 1.2 docs:
When specifying relationships it is only necessary to specify the relationship on the end where the foreign key exists. When the schema file is parsed, it reflects the relationship and builds the opposite end automatically. If you specify the other end of the relationship manually, the auto generation will have no effect.
I follow their guidelines and only establish the relation on the table which contains the foreign key, so give this a try:
Uzytkownik:
tableName: uzytkownicy
columns:
id:
type: integer
fixed: false
unsigned: true
primary: true
autoincrement: true
uzytkownik_dane_id:
type: integer
login:
type: string(64)
notnull: true
haslo:
type: string(96)
notnull: true
email:
type: string(192)
notnull: true
nazwa:
type: string(64)
relations:
UzytkownikDane:
local: uzytkownik_dane_id
foreign: id
foreignAlias: Dane # Set this if you want the UzytkownikDane to reference this as Dane
alias: Dane # Set this if you want this table to reference UzytkownikDane as Dane (which I think is what you want)
foreignType: one
UzytkownikDane:
tableName: uzytkownicy_dane
columns:
id:
type: integer
fixed: false
unsigned: true
primary: true
autoincrement: true
imie:
type: string(128)
nazwisko:
type: string(128)
kraj:
type: string(32)
wojewodztwo:
type: string(64)
miejscowosc:
type: string(128)
adres:
type: string(128)
kod:
type: string(16)
telefon:
type: string(16)
# No need for references here, they will be auto-generated by Doctrine
I tested the build-model and behaved as the Doctrine docs dictated.
Upvotes: 0