This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » General discussion » Foreign key
Foreign key [message #103239] Thu, 29 July 2010 14:45 Go to next message
littledg  is currently offline littledg
Messages: 3
Registered: July 2010
Location: France
Junior Member
Hello

My question is probably stupid, but I do not understand how to properly configure symfony to save foreign keys.

Here's my model (simplified):

Adresse:
  connection: doctrine
  tableName: adresse
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    rue:
      type: string(100)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false

  relations:
    Client:
      local: id
      foreign: id_adresse
      type: many

Client:
  connection: doctrine
  tableName: client
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    nom:
      type: string(80)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    id_adresse:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false

  relations:
    Adresse:
      local: id_adresse
      foreign: id
      type: one


For the form:

class ClientForm extends BaseClientForm
{
  public function configure()
  {
	$this->embedForm('adresse', new AdresseForm());
  }


processForm clientActions class :

protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    
    if ($form->isValid())
    {
	$client = $form->save();
	  
        $this->redirect('client/edit?id='.$client->getId());
    }
  }


"Client" and "Adresse" are saved but the foreign key (id_adresse) is always NULL in Client table.

I'm sure this is not very complicated but I do not understand. Embarassed

Can you help me ?

Thank you in advance.
Re: Foreign key [message #103247 is a reply to message #103239 ] Thu, 29 July 2010 16:33 Go to previous messageGo to next message
littledg  is currently offline littledg
Messages: 3
Registered: July 2010
Location: France
Junior Member
Nobody?

I have to save "Address" manually before "Client"?

The question is to know if Symfony can automatically save hasOne record before, to get the id_adresse in Client record ?
Re: Foreign key [message #103257 is a reply to message #103247 ] Thu, 29 July 2010 19:38 Go to previous messageGo to next message
DBA_Alex  is currently offline DBA_Alex
Messages: 257
Registered: July 2008
Location: North Carolina, USA
Faithful Member
Give this a read - it walks through how to set up forms similar to what you're wanting to do:

http://www.symfony-project.org/more-with-symfony/1_4/en/06-A dvanced-Forms

There's also a plugin that makes embedding relationships like this a snap:

http://www.symfony-project.org/plugins/ahDoctrineEasyEmbedde dRelationsPlugin


Currently Using
---------------------------
Symfony 1.4/Doctrine 1.2
MySQL 5.1
Re: Foreign key [message #103260 is a reply to message #103257 ] Thu, 29 July 2010 21:48 Go to previous message
littledg  is currently offline littledg
Messages: 3
Registered: July 2010
Location: France
Junior Member
Thank you for the plugin.

For the documentation page, the sample form is ok to save hasMany records. I think it's different from my case and I do not know how to adapt it : i have to manually save HasOne record (with Adresse->save() etc.). There is probably something I'm missing.

Thank you again for your help!
Previous Topic:How to have permission between groups and dynamics item
Next Topic:Showing only Intro text using pagebreak
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software