Santa,
I have tested the option 2 and it works as expected.
- The structure of tables and $belongsTo variable that you proposed
were kept unchanged.
- Small modification: under score is used for the foreign keys (e.g.
opening_id). I reviewed the cake core and found out that generatList()
detects only foreign keys having '_id' in name.
generateList() ignores your foreignKey statements (e.g. 'foreignKey'
=> 'openingid' etc.)
- You can try abba bryant's approach. However the main point here is:
the naming for foreign keys and the generateList's ignorance of the
foreginKey statements and conditions specified for $belongsTo (I dont
see these 'secrets' documented any where)
Here is the code I tested:
Models:
class Letter extends AppModel {
var $name = 'Letter';
var $belongsTo = array(
'Opening' => array('className' =>
'LetterContent',
'foreignKey' => 'opening_id',
),
'Middle' => array('className' =>
'LetterContent',
'foreignKey' => 'middle_id',
),
'Ending' => array('className' =>
'LetterContent',
'foreignKey' => 'ending_id',
),
);
}
class LetterContent extends AppModel {
var $name = 'LetterContent';
}
Controller:
class LettersController extends AppController {
var $name = 'Letters';
var $uses = array('Letter', 'LetterContent');
var $helpers = array('Html', 'Form' );
function add() {
if (!empty($this->data)) {
$this->cleanUpFields();
$this->Letter->create();
if ($this->Letter->save($this->data)) {
$this->flash('Letter saved.',
array('action'=>'index'));
exit();
} else {
}
}
//the statements below are the same for 'edit' method
$openings = $this->Letter->Opening-
>generateList(array('letter_section'=>'Opening'));
$middles = $this->Letter->Middle-
>generateList(array('letter_section'=>'Middle'));
$endings = $this->Letter->Ending-
>generateList(array('letter_section'=>'Ending'));
$this->set(compact('openings', 'middles', 'endings'));
}
...
}
View:
<?php echo $form->create('Letter');?>
<fieldset>
<legend><?php __('Add');?> <?php __('Letter');?></
legend>
<?php
echo $form->input('opening_id');
echo $form->input('middle_id');
echo $form->input('ending_id');
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
...
?>
Good luck
Zonium
P/S: I like your table structure and the way you defined $belongsTo.
On Dec 10, 8:03 pm, Santa <[EMAIL PROTECTED]> wrote:
> I considered option 2 as I believe this is a fairly straight forward
> model and it should be handled quite easily by cake. I have completed
> the "baking" as per your "recipe" and here is what I have come up
> with.
>
> When I bake the controller and the views, it gives me an error in the
> controller. Let me explain. Because the model describes the
> letter_content for all three sections (opening, middle, closing) by
> the array key, but references the same model for each section, the
> controller bakes the following with the generateList() function:
>
> $this->set('openings', $this->Letter->Opening->generateList());
> $this->set('middles', $this->Letter->Middle->generateList());
> $this->set('endings', $this->Letter->Ending->generateList());
>
> So now when I access the edit page, I get the following:
>
> Notice: Undefined property: Letter::$Opening
> Fatal error: Call to a member function generateList() on a non-object
>
> This is apparently due to the idea that $this->Letter->Opening is not
> really an object. Or is it?
>
> So, with some creative license and knowing that I needed LetterContent
> to fill in these areas, I update the controller to use the
> LetterContent model:
> var $uses = array('Letter', 'LetterContent');
>
> And then I update the generateList as follows: (notice the change in
> the reference to the LetterContent model and the addition of the
> conditions in the generateList function)
> $this->set('openings', $this->LetterContent->generateList("`letter_section` =
> 'Opening'"));
>
> $this->set('middles', $this->LetterContent->generateList("`letter_section` =
> 'Middle'"));
>
> $this->set('endings', $this->LetterContent-
>
> >generateList("`letter_section` = 'Ending'"));
>
> Now I can access the edit view and things are looking good except for
> one issue. Now all three drop downs (Opening, Middle, Ending) are
> populated with only openings. This is close to how things started, but
> instead of showing all letter_content in every drop down, I get all
> openings in every letter_content drop down.
>
> It seems like cake should handle this, and I know it is due to my
> inability to understand how it should be done. I don't want to have to
> strip out each table if it can be avoided. I am still sticking to my
> original thoughts that this is a simple model, very clear and concise,
> and is the best wat to maintain the tables to keep it easily
> understandable. It conforms to 3NF and is a valid schema.
>
> What am I missing? Any other ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---