Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony][Symfony 1.4]
Forum PHP.pl > Forum > PHP > Frameworki
Valantir
Witam. Pracuję na pluginie sfGuardAuth i plugin ten wygenerował mi taki plik schema.yml:

Kod
propel:
  _attributes:      { package: plugins.sfGuardPlugin.lib.model }
  
  sf_guard_group:
    _attributes:    { phpName: sfGuardGroup }
    id:             ~
    name:           { type: varchar, size: 255, required: true, index: unique }
    description:    { type: longvarchar }
  
  sf_guard_permission:
    _attributes:    { phpName: sfGuardPermission }
    id:             ~
    name:           { type: varchar, size: 255, required: true, index: unique }
    description:    { type: longvarchar }
  
  sf_guard_group_permission:
    _attributes:    { phpName: sfGuardGroupPermission }
    group_id:       { type: integer, primaryKey: true, required: true, foreignTable: sf_guard_group, foreignReference: id, onDelete: cascade }
    permission_id:  { type: integer, primaryKey: true, required: true, foreignTable: sf_guard_permission, foreignReference: id, onDelete: cascade }
  
  sf_guard_user:
    _attributes:    { phpName: sfGuardUser }
    id:             ~
    username:       { type: varchar, size: 128, required: true, index: unique }
    algorithm:      { type: varchar, size: 128, required: true, default: sha1 }
    salt:           { type: varchar, size: 128, required: true }
    password:       { type: varchar, size: 128, required: true }
    created_at:     ~
    last_login:     { type: timestamp }
    is_active:      { type: boolean, required: true, default: 1 }
    is_super_admin: { type: boolean, required: true, default: 0 }
  
  sf_guard_user_permission:
    _attributes:    { phpName: sfGuardUserPermission }
    user_id:        { type: integer, primaryKey: true, required: true, foreignTable: sf_guard_user, foreignReference: id, onDelete: cascade }
    permission_id:  { type: integer, primaryKey: true, required: true, foreignTable: sf_guard_permission, foreignReference: id, onDelete: cascade }
  
  sf_guard_user_group:
    _attributes:    { phpName: sfGuardUserGroup }
    user_id:        { type: integer, primaryKey: true, required: true, foreignTable: sf_guard_user, foreignReference: id, onDelete: cascade }
    group_id:       { type: integer, primaryKey: true, required: true, foreignTable: sf_guard_group, foreignReference: id, onDelete: cascade }
    
  sf_guard_remember_key:
    _attributes:    { phpName: sfGuardRememberKey }
    user_id:        { type: integer, primaryKey: true, required: true, foreignTable: sf_guard_user, foreignReference: id, onDelete: cascade }
    remember_key:   { type: varchar, size: 32 }
    ip_address:     { type: varchar, size: 50, primaryKey: true }
    created_at:     ~


Ja natomiast chciałbym utworzyć tabelę profil z kluczem głównym profil_id oraz kluczem obcym do tablicy sf_guard_user. Mój plik schema.yml jest następujący:
Kod
propel:
  Profil:
    profil_id:      { type: integer, poreignKey: true, autoincrement: true }
    imie:           { type: varchar(45), required: true }
    nazwisko:       { type: varchar(128), required: true }
    email:          { type: varchar(45), index: unique, required: true }
    miejscowosc:    { type: varchar(60) }
    data_urodzenia: { type: date }
    plec:           { type: varchar(10) }
    telefon:        { type: integer }
    id:             ~
    _foreignKeys:
      -
        foreignTable: sf_guard_user
        onDelete: cascade
        onUpdate: cascade
        references:
          - { local: id, foreign: id }


No i tutaj pojawia się problem. Niestety nie chcę mnie przepuścić. Błąd jest następujący:

Cytat
[propel-sql-exec] Failed to execute:

CREATE TABLE `Profil`
(
`profil_id` INTEGER AUTO_INCREMENT,
`imie` VARCHAR(45) NOT NULL,
`nazwisko` VARCHAR(128) NOT NULL,
`email` VARCHAR(45) NOT NULL,
`miejscowosc` VARCHAR(60),
`data_urodzenia` DATE,
`plec` VARCHAR(10),
`telefon` INTEGER,
`id` INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE KEY `Profil_U_1` (`email`),
CONSTRAINT `Profil_FK_1`
FOREIGN KEY (`id`)
REFERENCES `sf_guard_user` (`id`)
ON UPDATE CASCADE
ON DELETE CASCADE
)Type=InnoDB

Some problems occurred when executing the task:
If the exception message is not clear enough, read the output of the task for
more information
[propel-sql-exec] SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key


Gdy wykasuję kolumnę profil_id wszystko pójdzie dobrze ale nie będę miał klucza głównego... (wiem, że chodzi o autoincrementację). Czy ktoś mógłby pomóc mi rozwiązać ten problem aby istniał i klucz główny i obcy? Zrobiłem jeden schemat, który wydawał mi się słuszny i klucze zostały utworzone ale niestety obcy nie był wpisywany. Natomiast gdy usunę klucz główny z tabeli profil, to klucz obcy normalnie się zapisuje... Czy istnieje sposób na to, żeby nie musieć w akcji pobierać rekordu, który właśnie wstawię do bazy w celu pobrania id z tabeli sf_guard_user i wstania go do tabeli profil na piechotę?
luck
  1. `profil_id` INTEGER AUTO_INCREMENT,
  2. (...)
  3. `id` INTEGER NOT NULL AUTO_INCREMENT,

Musisz się zdecydować tylko na jedno. Najlepiej usunąć autoincrement z profil_id, albo po prostu całe to pole. PK będziesz i tak miał pod id.
Valantir
A jak uzyskam unikatowe id podczas każdego wstawiania rekordu? Tylko na piechotę pobierać i wstawiać? Chodzi o to, że profil_id jest taki sam jak id z tabeli sf_guard_user...
luck
Masz w definicji modelu wpis "id:". Dzięki temu Propel sam wygeneruje autoinkrementowany klucz główny.
Valantir
Cytat(luck @ 13.09.2011, 20:45:23 ) *
Najlepiej usunąć autoincrement z profil_id, albo po prostu całe to pole. PK będziesz i tak miał pod id.


I wtedy powoływać się na to id w innych tabelach jak by to było w przypadku klucza głównego? Ale jest możliwość zaznaczania tego, że ta kolumna jest kluczem obcym do sf_guard_user[`id`] i jednocześnie kluczem głównym tej tabeli? Czy jest to zbędne całkowicie?
luck
Po co chcesz na siłę, żeby id było jednocześnie kluczem głównym i obcym? Przecież tutaj jest wszystko dokładnie opisane, jak powinno się tworzyć profil dla tego pluginu: http://www.symfony-project.org/plugins/sfGuardPlugin

Kod
sf_guard_user_profile:
  _attributes: { phpName: sfGuardUserProfile }
  id:
  user_id:     { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true, onDelete: cascade }
  first_name:  varchar(20)
  last_name:   varchar(20)
  birthday:    date
Valantir
No i co? Nikt nie jest w stanie mi pomóc?
Doszedłem do wniosku, że stworzę sobie pole profil_id, które będzie kluczem głównym tabeli profil oraz pole id, które będzie kluczem obcym do tabeli sf_guard_user. Plik schema jest następujący:
Kod
propel:
  Profil:
    profil_id:      { type: integer, primaryKey: true }
    imie:           { type: varchar(45), required: true }
    nazwisko:       { type: varchar(128), required: true }
    email:          { type: varchar(45), index: unique, required: true }
    miejscowosc:    { type: varchar(60) }
    data_urodzenia: { type: date }
    plec:           { type: varchar(10) }
    telefon:        { type: integer }
    id:             ~
    _foreignKeys:
      -
        foreignTable: sf_guard_user
        onDelete: cascade
        onUpdate: cascade
        references:
          - { local: id, foreign: id }

Chciałem dodać dane do tabeli sf_guard_user i profil, a następnie zedytować wiersz, który wstawiłem i nadać w kolumnie profil_id id widniejące w kolumnie id tabeli profil bądź tabeli sf_guard_user. Wszystko ok tylko, że gdy plik akcji wygląda następująco, id nie jest w ogóle wstawiane-dalej widnieje 0.
Kod
<?php
class rejestracjaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request) {
       $this->form = new RejestracjaForm();
       if ($request->isMethod('post')) {
           $this->form->bind($request->getParameter('sf_guard_user'));
           if ($this->form->isValid()) {
               //$this->form->getObject()->setIsActive(0);
               $this->form->save();
               $c = new Criteria();
               $c->add(ProfilPeer::ID, $this->form->getObject()->getId());
               $profil = ProfilPeer::doSelectOne($c);
               if($profil)
               {
                   $profil->setProfilId(3);
                   $profil->save();
               }
               else
               {
                   $this->redirect('jakastam/jakas');
               }
               $this->redirect('glowny/index/');
           }
       }
   }
}

Gdy np chciałbym zmienić samo imię, to wstawiając taki kawałek kodu do akcji:
Kod
if($profil)
               {
                   $profil->setProfilId('Bogdan');
                   $profil->save();
               }

Wszystko jest w porządku-imię "Bogdan" jest wstawiane do tabeli. Problem jest tylko z profil_id... Czy ktoś mógłby mi pomóc rozwiązać ten problem?
luck
Twój podstawowy problem polega na tym, że zamiast najpierw zapoznać się choćby z podstawową dokumentacją do Propela zaczynasz pisać kod "na żywca" i dziwisz się, że nie działa.
Wiesz co oznacza taki zapis w definicji modelu?
Kod
id:

I co to jest to poniżej?
  1. $profil->setProfilId('Bogdan');

Sorry, ale nikt Ci tego nie wytłumaczy, bo musiałby wyłożyć całą filozofię pracy z tym ORM. Wiem, że nie takiej odpowiedzi oczekujesz, ale naprawdę jedyne wyjście w tym wypadku to: najpierw czytam, potem piszę jakikolwiek kod.
Valantir
Tutaj możesz mieć trochę racji, gdyż z tym kodem
Kod
if($profil)
{
       $profil->setProfilId('Bogdan');
       $profil->save();
}

się po prostu pomyliłem... (ale wstyd:P) Miało tam być setImie a nie setProfilId, wiec rozumiem Twoje rozgoryczenie. Fakt, źle sklepałem plik schema i dlatego nie chodziło... Już sobie z tym poradziłem i wszystko chodzi jak powinno:) Mimo ostrych słów dzięki:P
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.