Temat: Symfony 1.4 brak możliwości edycji klucza głównego.

Witam. Przez formularz chciałbym wypełnić danymi dwie tabele. Jedna to Profil, druga sf_guard_user. W tabeli Profil znajduje się klucz obcy do tabeli sf_guard_user. Plik schema poniżej:


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 }


Niestety nie mogę ustawić dla pola profil_id autoincrement, gdyż krzyczy błędem, że jedno pole z taką opcją już istnieje. Ogólnie rzecz biorąc chciałbym aby w tej tabeli powyżej był klucz obcy i klucz główny, które będą takie same, gdyż cała reszta bazy opiera się o klucz główny tej tabeli czyli profil_id. Próbowałem wstawić w akcji kawałek kodu, który by pobierał z jednej kolumny interesujące mnie id i wstawiał do drugiej. Jednak coś idzie nie tak, gdyż baza nie jest aktualizowana. Co gorsza, nie jest aktualizowana tylko dla kolumny profil_id. Zacząłem testować i w akcji obsługującej wstawianie do bazy umieściłem taki 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(8);
$profil->save();
}
else
{
$this->redirect('jakastam/jakas');
}
$this->redirect('glowny/index/');
}
}
}
}


Na sztywno ustawienie profil_id na wartość 8 nie działa. Ale gdy zmienię ten fragment na taki:

if($profil)
{
$profil->setImie('Bogdan');
$profil->save();
}

to imię zostaje zmienione na Bogdan. Czy ktoś może mi pomóc? Tylko nie odsyłajcie mnie do dokumentacji, bo gdy dostosuję plik schema w taki sposób jaki jest przedstawiony na stronie dokumentacji, to mój plik akcji nie działa w ogóle. Czy ktoś mógłby mi pomóc rozwiązać ten problem? Jeszcze wstawię plik formularzy dla pełnej dokumentacji tego, co zrobiłem:

RejestracjaForm.class.php

<?php
class RejestracjaForm extends sfGuardUserForm
{
public function configure()
{
unset(
$this['is_active'],
$this['is_super_admin'],
$this['updated_at'],
$this['sf_guard_user_group_list'],
$this['sf_guard_user_permission_list'],
$this['last_login'],
$this['created_at'],
$this['salt'],
$this['algorithm']
);
$profilForm = new ProfilForm();
$this->embedForm('Profil',$profilForm);

//formularze
$this->widgetSchema['username'] = new sfWidgetFormInputText(array('label' => 'Login'));
$this->widgetSchema['password'] = new sfWidgetFormInputPassword(array('label' => 'Hasło'));
$this->widgetSchema['password_confirmation'] = new sfWidgetFormInputPassword(array('label' => 'Powtórz hasło'));

//walidacja innych pól
$this->validatorSchema['password_confirmation'] = clone $this->validatorSchema['password'];
$this->widgetSchema->moveField('password_confirmation', 'after', 'password');
$this->mergePostValidator(new sfValidatorSchemaCompare('password_confirmation', sfValidatorSchemaCompare::EQUAL, 'password', array(), array('invalid' => 'Oba hasła muszą być takie same.')));
$this->validatorSchema['username'] = new sfValidatorString(array('max_length' => 128, 'required' => true), array('max_length' => 'Maximum 128 znaków', 'required' => 'Pole jest wymagane'));
$this->validatorSchema['password'] = new sfValidatorString(array('max_length' => 45, 'required' => true), array('max_length' => 'Maximum 45 znaków', 'required' => 'Pole jest wymagane'));
$this->validatorSchema['password_confirmation'] = new sfValidatorString(array('max_length' => 45, 'required' => true), array('max_length' => 'Maximum 45 znaków', 'required' => 'Pole jest wymagane'));
$this->validatorSchema->setPostValidator(
new sfValidatorPropelUnique(array('model' => 'sfGuardUser', 'column' => array('username')), array('invalid' => 'Taki login już istnieje')));
}
}
?>


ProfilForm.class.php

<?php
class ProfilForm extends BaseProfilForm
{
public function configure()
{
//Walidatory formularza rejestracji znajdują się w formularzu RegisterForm.class.php
$this->validatorSchema['imie'] = new sfValidatorString(array('max_length' => 45, 'required' => true), array('max_length' => 'Maximum 45 znaków', 'required' => 'Pole jest wymagane'));
$this->validatorSchema['nazwisko'] = new sfValidatorString(array('max_length' => 128, 'required' => true), array('max_length' => 'Maximum 45 znaków', 'required' => 'Pole jest wymagane'));
$this->validatorSchema['email'] = new sfValidatorEmail(array('max_length' => 45, 'required' => true), array('max_length' => 'Maximum 45 znaków', 'required' => 'Pole jest wymagane', 'invalid' => 'Złe dane'));
$this->validatorSchema->setPostValidator(
new sfValidatorPropelUnique(array('model' => 'Profil', 'column' => array('email')), array('invalid' => 'Taki email już istnieje')));

$this->widgetSchema['data_urodzenia'] = new sfWidgetFormDate();
$this->widgetSchema['data_urodzenia']->addOption('empty_values', array('year' => 'rok', 'month' => 'miesiąc', 'day' => 'dzień'));;
//formularz roku
$years = range(date('Y') - 120, date('Y') - 6);
$this->widgetSchema['data_urodzenia']->addOption('years', array_combine($years, $years));

//formularz miesiąca
$this->widgetSchema['data_urodzenia']->addOption('format', '%day%/%month%/%year%');
$miesiaceliczbowo = array(1,2,3,4,5,6,7,8,9,10,11,);
$miesiace = array('styczeń', 'luty', 'marzec','kwiecień','maj','czerwiec','lipiec','sierpień','wrzesień','październik','listopad','grudzień');
$this->widgetSchema['data_urodzenia']->addOption('months', $miesiace);

}
}


================================================================
================================================================
Dla potomnych...:)
Wszystko już działa:) Najważniejsze, to zmiana pliku schema.yml , który teraz prezentuje się tak:

propel:
Profil:
profil_id: { type: integer, primaryKey: 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: { type: integer, foreignTable: sf_guard_user, foreignReference: id, onDelete: cascade, onUpdate: cascade }

I plik akcji:


class rejestracjaActions extends sfActions
{
/**
* Executes index action
*
* @param sfRequest $request A request object
*/
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::PROFIL_ID, $this->form->getObject()->getId());
$profil = ProfilPeer::doSelectOne($c);
if($profil)
{
$profil->setId($profil->getProfilId());
$profil->save();
}
else
{
$this->redirect('glowny/blad');
}
$this->redirect('glowny/index/');
}
}
}
}


Najpierw jest wstawiany klucz główny, a potem sam wstawiam klucz obcy pobrany z tej samej tabeli z kolumny profil_id.Kamil Demurat edytował(a) ten post dnia 14.09.11 o godzinie 19:45