Not a problem. Here goes - sorry for the long mail then ;-)
Controller code:
<?php
public function executeEdit(sfWebRequest $request)
{
if($request->hasParameter('drinkdb_id'))
{
$im = new JustdrinkImporter();
$this->drink = $im->loadDrink($request->getParameter
('drinkdb_id'));
} else {
$this->drink = Doctrine::getTable('Drink')->findOneById($request-
>getParameter('id'));
if(!$this->drink)
{
$this->drink = new Drink();
}
}
$ingredient_options = array();
foreach(Doctrine::getTable('Ingredient')->findAllOrderByName() as
$ingredient)
{
$ingredient_options[$ingredient->getId()] = $ingredient->getName
();
}
$this->ingredient_options = $ingredient_options;
$measurements = array();
foreach(Doctrine::getTable('Measurement')->findAll() as $m)
{
$measurements[$m->getId()] = $m->getName();
}
$this->measurements = $measurements;
if($request->isMethod('post'))
{
$i = 0;
$measurements = $request->getParameter('measurements');
$amounts = $request->getParameter('amounts');
#unset($this->drink->DrinkIngredient);
// $this->drink->DrinkIngredient = new Doctrine_Collection;
if(!$this->drink->isNew())
{
Doctrine_Query::create()->from('DrinkIngredient')->where
('drink_id = ?', $this->drink->getId())->delete()->execute();
}
if(is_array($request->getParameter('ingredients')))
{
foreach($request->getParameter('ingredients') as $ing)
{
$di = new DrinkIngredient();
$di->ingredient_id = $ing;
$di->measurement_id = $measurements[$i];
$di->amount = $amounts[$i];
$this->drink->DrinkIngredient[] = $di;
$i++;
}
}
}
$this->form = new BackendDrinkForm($this->drink);
if ($request->isMethod('post'))
{
$this->form->bind($request->getParameter('drink'));
if ($this->form->isValid())
{
$this->form->save();
$this->getUser()->setFlash('success', 'Drinken blev
gemt.');
$this->redirect('drink/edit?id=' .
$this->form->getObject()->getId
());
}
}
}
?>
The editsuccess is just a simple implementation of a form - no
biggies. The form is generated, but I have made some smaller changes:
<?php
class BackendDrinkForm extends DrinkForm
{
public function configure()
{
parent::configure();
unset($this['slug'],$this['created_at'],$this['deleted_at']);
$this->widgetSchema->setLabel('lang', 'Sprog');
sfProjectConfiguration::getActive()->loadHelpers('I18N');
$langs = array();
foreach(JustdrinkTools::getAvailableLanguages() as $lang)
{
$langs[$lang] = format_language($lang);
}
$this->widgetSchema['lang'] = new sfWidgetFormSelect(array
('choices' => $langs));
$this->validatorSchema['name'] = new sfValidatorString(array
('required' => true));
$this->validatorSchema['description'] = new sfValidatorString(array
('required' => true));
}
}
?>
The relevant model parts are these:
DrinkCategory:
columns:
id:
type: integer
primary: true
autoincrement: true
name: string(255)
description: string(255)
lang: { type: char(2), default: da, notnull: true }
actAs:
Sluggable: { fields: [name], uniqueBy: [lang,name] }
DrinkIngredient:
columns:
drink_id:
type: integer
primary: true
ingredient_id:
type: integer
primary: true
measurement_id:
type: integer
amount:
type: string(50)
relations:
Drink:
type: one
local: drink_id
foreign: id
Ingredient:
type: one
local: ingredient_id
foreign: id
Measurement:
type: one
local: measurement_id
foreign: id
DrinkIngredient:
type: many
local: drink_id
foreign: drink_id
Ingredient:
columns:
id:
type: integer
primary: true
autoincrement: true
name: string(255)
description: string(255)
lang: { type: char(2), default: da, notnull: true }
indexes:
slug:
fields: [slug]
actAs:
Sluggable:
unique: true
fields: [name]
canUpdate: true
SoftDelete:
Timestampable:
relations:
DrinkIngredient:
type: many
local: id
foreign: ingredient_id
Drink:
columns:
id:
type: integer
primary: true
autoincrement: true
name:
type: string(255)
notnull: true
description: string
user_id: integer
category_id:
type: integer
notnull: true
rating:
notnull: true
type: float
default: 0
votes:
type: integer
default: 0
views:
type: integer
default: 0
image_id: integer
glass_id: integer
lang: { type: char(2), default: da, notnull: true }
actAs:
Sluggable:
unique: true
fields: [name]
canUpdate: true
SoftDelete:
Timestampable:
updated:
disabled: true
indexes:
slug:
fields: [slug]
relations:
User:
local: user_id
foreign: id
type: one
DrinkCategory:
local: category_id
foreign: id
type: one
DrinkIngredient:
type: many
local: id
foreign: drink_id
onDelete: CASCADE
DrinkComment:
foreignAlias: Comments
local: id
foreign: drink_id
onDelete: CASCADE
type: many
Image:
local: image_id
foreign: id
type: one
DrinkGlass:
foreignAlias: glass
local: glass_id
foreign: id
type: one
foreignType: many
Don't worry about the mess, it will be fixed later - first it has to
work :)
On Oct 23, 8:39 am, Alexandre SALOME <[email protected]>
wrote:
> Hello,
>
> Is it the generated form or did you add some code ? (copy/paste it if yes)
>
> Can you give us the POST variables too ?
>
> Can you give us the form names, the schema ?
>
> Thanks,
>
> Alexandre
>
> 2009/10/22 phil0 <[email protected]>
>
>
>
>
>
> > I am importing some data from an external data source. For that I
> > decided to load the data into a model object, and then pass that to a
> > form (generated by Doctrine).
>
> > The object that I am editing is has a many-to-many relation, so it
> > seems that Doctrine saves the data temporarily and then deletes it
> > again. This is not a problem as performance is not an issue for this
> > backend functionality.
>
> > The problem arises when the form is submitted. The "id" of the object
> > is - because it was temporarily saved - set to an integer, but this no
> > longer exists because Doctrine deleted it. This makes the form
> > validation fail because the "id" is not empty and it doesn't exist in
> > the database. This results in an "Id: Invalid." error on the id field.
>
> > I would prefer not to save the object before editing it using the
> > form, as the user might regret the import and therefore just go back.
> > This would result in unedited data in the database.
>
> > I hope that I clarified my problem enough - if you need sample code
> > just tell me.
> > I use symfony 1.3 and the Doctrine version that is in the 1.3 svn.
>
> --
> Alexandre Salomé -- [email protected]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---