[code]
Menu:
columns:
id:
type: integer
primary: true
autoincrement: true
name:
type: string(40)
relations:
Stock:
refClass: Recipe
local: menu_id
foreign: stock_id
Stock:
columns:
id:
type: integer
primary: true
autoincrement: true
name:
type: string(40)
relations:
Menu:
refClass: Recipe
local: stock_id
foreign: menu_id
Recipe:
columns:
stock_id:
type: integer
menu_id:
type: integer
quantifier:
type: integer
relations:
Menu:
onDelete: CASCADE
Stock:
onDelete: CASCADE
[/code]
This is a pretty standard n:m relationship between Menu and Stock,
using Recipe as a through class.
Now, coming from a basic example, the data could be something like
this:
[code]
Menu:
Coffee:
name: Coffee
Sandwich:
name: Sandwich
Stock:
Bread:
name: Slices of Bread
Cheese:
name: Slices of Cheese
Espresso:
name: Espresso Tabs
Milk:
name: Milk (50ml)
Recipe:
RCoffee1:
Stock: Espresso
Menu: Coffee
Quantifier: 1
RCoffee2:
Stock: Milk
Menu: Coffee
Quantifier: 2
etc.....
[/code]
Now, using the new forms and the sfWidgetFormDoctrineSelectMany, I can
create a nice form for my Menu (MenuForm), that has a selection list
of all Stock items. This gets saved in the recipe table and I can
manipulate it accordingly. BUT, seeing that I have a modified n:m
relationship, I need a list of all Stock items with an input field
next to it where I can insert the quantity.
After talking to John Wage, I have started implementing a new
sfWidgetForm and a new sfValidator called
sfWidgetFormDoctrineQuantityInput and sfValidatorQuantityInput
respectively.
The widget looks like this:
[code]
<?php
class sfWidgetFormDoctrineQuantityInput extends sfWidgetForm
{
protected function configure($options = array(), $attributes =
array())
{
$this->addRequiredOption('model');
$this->addOption('method', '__toString');
$this->addOption('order_by', null);
$this->addOption('alias', 'a');
$this->addOption('query', null);
$this->addOption('connection', null);
$this->addOption('enclosure', 'ul');
parent::configure($options, $attributes);
}
public function render($name, $value = null, $attributes = array(),
$errors = array())
{
$html = '';
foreach ($this->getChoices() as $key=>$choice)
{
$val = 0;
if (is_array($value) && array_key_exists($key, $value))
$val = $value[$key];
$html .= '<li>' . $this->renderTag('input',
array_merge(array('type'=>'text', 'name' => $name . '[]', 'value' =>
$val), $attributes)) . '<label>' . $choice . '</label></li>';
}
return $html;
}
public function getChoices()
{
$choices = array();
$a = $this->getOption('alias');
$q = is_null($this->getOption('query')) ? Doctrine_Query::create()-
>from($this->getOption('model')." $a") : $this->getOption('query');
if ($order = $this->getOption('order_by'))
{
$q->orderBy("$a." . $order[0] . ' ' . $order[1]);
}
$objects = $q->execute();
$method = $this->getOption('method');
foreach ($objects as $object)
{
$choices[is_array($value = $object->getPrimaryKey()) ?
current($value) : $value] = $object->$method();
}
return $choices;
}
}
[/code]
and produces an output similar to this:
[code]
<li><input type="text" name="menu[recipe][]" value="0"
id="party_recipe" /><label>Bread</label></li>
<li><input type="text" name="menu[recipe][]" value="0"
id="party_recipe" /><label>Cheese</label></li>
<li><input type="text" name="menu[recipe][]" value="0"
id="party_recipe" /><label>Espresso</label></li>
<li><input type="text" name="menu[recipe][]" value="0"
id="party_recipe" /><label>Milk</label></li>
[/code]
Now, here come my questions:
I am stuck as to where and how I insert this data into the database.
This is the logic behind the data insertion/manipulation:
1. iterate over all fields
2. if the value is 0, delete the Recipe entry, if applicable
3. if the value is not 0, create a Recipe entry or - if it exists -
edit the quantity field with the value given.
4. Repopulate the fields when editing the entry
Any help would be appreciated.
juro
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony developers" 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-devs?hl=en
-~----------~----~----~----~------~----~------~--~---