Re: Multiple N to Distinct N Model
Thanks for all of the feedback. I wanted to post the resolution. I hate when people ask questions, get resolution, but fail to post what they did to help answer the issue for other people who might be having the same problem. So, when I baked the view, I discovered something interesting. All of the 'letter_content' sections where being published with the variable $openings. This is why when I stated above all of the content was openings only. So when I changed the variables to the appropriate content type (i.e. $middles, $endings) in the view, the drop downs worked! Zonium, I think your solution is the best of all the comments posted. I appreciate everyone who added comments to the post and the help offered. But the reason I like Zonium's solution is because it keeps the number of models to a minimum and the code easier to read. I am able to accomplish the goal of providing various types of content from one table (i.e. Openings, Middles, Ends) and from 1 model by the letter_section flag. This is great, I just wish cake would support it in scaffolding. Oh well. Here is the final solution: ** LETTER CONTENT (model) ** array('className' => 'Letter', 'conditions' => 'Opening.opening_id = id', 'order' => '', 'foreignKey' => 'id' ), 'Middle' => array('className' => 'Letter', 'conditions' => 'Middle.middle_id = id', 'order' => '', 'foreignKey' => 'id' ), 'Ending' => array('className' => 'Letter', 'conditions' => 'Ending.ending_id = id', 'order' => '', 'foreignKey' => 'id' ), ); } ?> ** LETTERS (model) ** array('className' => 'LetterContent', 'conditions' => "`Opening`.`letter_section` = 'Opening'", 'order' => '', 'foreignKey' => 'opening_id' ), 'Middle' => array('className' => 'LetterContent', 'conditions' => "`Middle`.`letter_section` = 'Middle'", 'order' => '', 'foreignKey' => 'middle_id' ), 'Ending' => array('className' => 'LetterContent', 'conditions' => "`Ending`.`letter_section` = 'Ending'", 'order' => '', 'foreignKey' => 'ending_id' ), ); } ?> ** LETTERS (controller) ** Letter->recursive = 0; $this->set('letters', $this->Letter->findAll()); } function view($id = null) { if (!$id) { $this->Session->setFlash('Invalid id for Letter.'); $this->redirect('/letters/index'); } $this->set('letter', $this->Letter->read(null, $id)); } function add() { if (empty($this->data)) { $this->set('openings', $this->Letter->Opening- >generateList(array('letter_section'=>'Opening'))); $this->set('middles', $this->Letter->Middle- >generateList(array('letter_section'=>'Middle'))); $this->set('endings', $this->Letter->Ending- >generateList(array('letter_section'=>'Ending'))); $this->render(); } else { $this->cleanUpFields(); if ($this->Letter->save($this->data)) { $this->Session->setFlash('The Letter has been saved'); $this->redirect('/letters/index'); } else { $this->Session->setFlash('Please correct errors below.'); $this->set('openings', $this->Letter->Opening- >generateList(array('letter_section'=>'Opening'))); $this->set('middles', $this->Letter->Middle- >generateList(array('letter_section'=>'Middle'))); $this->set('endings', $this->Letter->Ending- >generateList(array('letter_section'=>'Ending'))); } } } function edit($id = null) { if (empty($this->data)) { if (!$id) { $this->Session->setFlash('Invalid id for Letter'); $this->redirect('/letters
Re: Multiple N to Distinct N Model
Santa, First I am using 1.2 so there might be some differences some where with the way generateList / scaffold work. However, I guess the key functionality and the end result should be the same. > So why would I need a completely new model for each section? By defining the Letter model as you / I proposed, virtually (to cake) we have set up 3 models with different names (Opening, Middle and Ending), yet using the same class (LetterContent). This is confirmed by looking at the SQL statements created by generateList (when debug is on). See below. > While reviewing the Scaffold, I can see that it is purely vanilla. If > you reference a model, it will pull that model without conditions or > variation. So I can see why the conditions get bypassed when using > scaffolding, the scaffolding ignores them. :( But I still do not > understand why the model can't handle that situation Like you, I had thought the conditions we set for the model should be taken into account. I had expected generateList follow findAll pattern of being 'controlled' by the condition set for the model. However, it is how it functions, so... just accept it for now and make it work by manually giving correct conditions for generateList. If it works for openings it should work for other sections... Try turning on debug and make sure the SQL statements for other sections are 'the same' as for openings (mine are, and they look as follows). SELECT `LetterContent`.`id` FROM `letter_contents` AS `LetterContent` WHERE `letter_section` = 'Opening' SELECT `LetterContent`.`id` FROM `letter_contents` AS `LetterContent` WHERE `letter_section` = 'Middle' SELECT `LetterContent`.`id` FROM `letter_contents` AS `LetterContent` WHERE `letter_section` = 'Ending' If you like I can forward to you the complete code I used for testing out this case. Good luck, Zonium On Dec 12, 9:59 pm, Santa <[EMAIL PROTECTED]> wrote: > AD7six said: > > > OR consider scaffolding what it is (a development tool), bake/write > > some code and add the conditions to the generateList call as you would > > need to do for any polymorphic model definition. if you use 3 models > > opperating on the same table, just edit the model's beforeFind to add > > a condition to only return rows with 'type' = middle (for example). > > I'm not sure how this would be any different than what I am trying to > accomplish with the Letter and LetterContent model? I am setting up > the filter using the 'condition' in the relationship $belongsTo. (see > below) So why would I need a completely new model for each section? > Shouldn't the condition do the filtering for me and only provide the > data specified by the filter 'condition' back to the controller? or am > I missing something? > > Unless I am misreading the code, the findAll function in the AppModel > (model_php5.php) passes the condition to the beforeFind function by > default so I shouldn't have to write 3 models defining the filter in a > beforeFind option for each one. The condition should already cover > that. > > While reviewing the Scaffold, I can see that it is purely vanilla. If > you reference a model, it will pull that model without conditions or > variation. So I can see why the conditions get bypassed when using > scaffolding, the scaffolding ignores them. :( But I still do not > understand why the model can't handle that situation even in a > scaffold environment. The request from the controller should always > filter through the model and return what the model is intending, not a > vanilla flavor the scaffold selects. If I am off on this, someone > please explain it to me. Even the lifecycle shows the scaffold is but > a small method used to render basic controller functions / views to > use with the model. (http://www.cakecollab.org/lifecycle.png) It still > appears the model is responsible for the data that is returned to the > controller. > > LETTER MODEL > class Letter extends AppModel { > var $name = 'Letter'; > var $belongsTo = array( > 'Opening' => > array('className' => 'LetterContent', > 'conditions' => > "letter_contents.letter_section = 'Opening'", > 'order' => '', > 'foreignKey' => 'opening_id' > ), > 'Middle' => > array('className' => 'LetterContent', > 'conditions' => > "letter_contents.letter_section = 'Middle'", > 'order' => '', > 'foreignKey' => 'middle_id' > ), > 'Ending' => > array('className' => 'LetterContent', > 'conditions' => > "letter_contents.letter_section = 'Ending'", > 'order' => '', > 'foreignKey' => 'en
Re: Multiple N to Distinct N Model
AD7six said: > OR consider scaffolding what it is (a development tool), bake/write > some code and add the conditions to the generateList call as you would > need to do for any polymorphic model definition. if you use 3 models > opperating on the same table, just edit the model's beforeFind to add > a condition to only return rows with 'type' = middle (for example). I'm not sure how this would be any different than what I am trying to accomplish with the Letter and LetterContent model? I am setting up the filter using the 'condition' in the relationship $belongsTo. (see below) So why would I need a completely new model for each section? Shouldn't the condition do the filtering for me and only provide the data specified by the filter 'condition' back to the controller? or am I missing something? Unless I am misreading the code, the findAll function in the AppModel (model_php5.php) passes the condition to the beforeFind function by default so I shouldn't have to write 3 models defining the filter in a beforeFind option for each one. The condition should already cover that. While reviewing the Scaffold, I can see that it is purely vanilla. If you reference a model, it will pull that model without conditions or variation. So I can see why the conditions get bypassed when using scaffolding, the scaffolding ignores them. :( But I still do not understand why the model can't handle that situation even in a scaffold environment. The request from the controller should always filter through the model and return what the model is intending, not a vanilla flavor the scaffold selects. If I am off on this, someone please explain it to me. Even the lifecycle shows the scaffold is but a small method used to render basic controller functions / views to use with the model. (http://www.cakecollab.org/lifecycle.png) It still appears the model is responsible for the data that is returned to the controller. LETTER MODEL array('className' => 'LetterContent', 'conditions' => "letter_contents.letter_section = 'Opening'", 'order' => '', 'foreignKey' => 'opening_id' ), 'Middle' => array('className' => 'LetterContent', 'conditions' => "letter_contents.letter_section = 'Middle'", 'order' => '', 'foreignKey' => 'middle_id' ), 'Ending' => array('className' => 'LetterContent', 'conditions' => "letter_contents.letter_section = 'Ending'", 'order' => '', 'foreignKey' => 'ending_id' ), ); } ?> --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Multiple N to Distinct N Model
I really appreciate all of the feedback. I must admit, there are advantages and disadvantages to both methods. I want to keep it positive so I won't discuss the disadvantages from my perspective, but the things I look for. Zonium, thanks for the information on the foreign key caveat... _id... that will come in handy to know. I will try to submit a documentation bug (unless you have already) to the cakephp documentation team. AD7six. I am not certain what you mean as to your statement of a "general misundertanding of what an object is or how they are implemented in cake." I mean I understand the statement, but the example you are providing isn't clear as to the point you are trying to make. Not to me anyway. Not in the context of what we have been working on. If your example was: $this->Car->id = 'mine'; $keys = $this->Car->Owner->Key->findAll("where `car_id` = 'mine'"); Then I could see the context. But from my perspective, I am not trying to say here is my letter_section now give me all the sections available and expecting cake to understand what I am asking for. abba your solution is interesting to me. I must confess, I am not excited about adding the extra controller unless cake absolutely needs this in order to get this to work correctly. I have a feeling I will be testing your implementation before long however. Since I am jack of all trades (DB & Code) and master of neither, I like to keep things simple. I like the idea of having different models for each of the letter_content sections, but my thought is it would be easier to follow the approach zonium has presented. It keeps all of the code in fewer locations and specific to the model/controller combinations. As it stands, the data pulled from the letter_content model by the relationship in the letter model requires the letter controller to ALWAYS be filtered as dictated by the application specifications. (i.e Openings will always be "where `letter_section` = 'Opening'", etc.) In other words, I will never want endings to be available as openings. So there is no concern about needing to have access to the letter_content through the letter model by some other method. Having said that, I tried to replicate zoniums work, and I get some of the same inconsistencies I was seeing before. However, the openings are showing up just fine now... but the middle and ending both have middle content in the drop down now. So filtering is making it through, but just not as expected. When I set the debug to 3 in the core.php, I am able to use the queries directly in MySQL and they pull the filtered data as expected, so I am not sure what the disconnect is. I am using version 1.1.18.5850 in the case you are using different versions. It is still odd to me when setting a filter in the model the scaffolding totally disregards it. It would seem the data model should be reflected in the controller as the model specifies. Or am I confused as to how MVC functions? If so, I would love for someone to explain it to me. So Abba, I will try your methodology next. I am not giving up on zoniums solution, I just want to experiment with the various solutions to see which of them feel right to me and make the most sense. Thanks for all of the feed back. Keep it coming! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Multiple N to Distinct N Model
To correct myself.. I would create a model - ie LetterPart In the Letter relationships set ( for hasMany - your needs might be different but the keys are the same ) var $hasMany = array( 'TopSection' => array( 'className' => 'LetterPart', 'conditions' = 'TopSection.section_type = "top''' ... ), 'MiddleSection' => array( ... - similiar condition key - ... ) ); This way the section needs only 1 model and the condition is in the relationship abba bryant wrote: > > The right way to do this would be to have 3 models each of which uses the > same table ( letter_contents ) > with the correct conditions defined ( see finderQuery ) or place the > conditions within the Letter model's relationships. > > > [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? >> >> >> >> >> > > -- View this message in context: http://www.nabble.com/Multiple-N-to-Distinct-N-Model-tp14235103p14303232.html Sent from the CakePHP mailing list archive at Nabble.com. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Multiple N to Distinct N Model
On Dec 12, 9:47 am, zonium <[EMAIL PROTECTED]> wrote: > > - 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) I sense a general misunderstanding of what an object is, or how they are implemented in cake. e.g. $this->Car->id = 'mine'; $keys = $this->Car->Owner->Key->findAll(); will not find all the keys that will open my car. It's the same as: $Key = new Key(); $keys = $Key->findAll(); It will find all keys, belonging to (or not) an owner which may or not be associated with A car etc. There is no ignorance in the generateList method, it does what you tell it to do. If you pass no constriant, or don't automatically modify results in a beforeFind method based on some other logic, you get everything (from the table) returned. hth, AD --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Multiple N to Distinct N Model
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: create('Letter');?> input('opening_id'); echo $form->input('middle_id'); echo $form->input('ending_id'); ?> end('Submit');?> ... ?> 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 tho
Re: Multiple N to Distinct N Model
ded 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? > > -- > View this message in > context:http://www.nabble.com/Multiple-N-to-Distinct-N-Model-tp14235103p14289... > Sent from the CakePHP mailing list archive at Nabble.com. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Multiple N to Distinct N Model
On Dec 12, 7:31 am, abba bryant <[EMAIL PROTECTED]> wrote: > The right way to do this would be to have 3 models each of which uses the > same table ( letter_contents ) > with the correct conditions defined ( see finderQuery ) or place the > conditions within the Letter model's OR consider scaffolding what it is (a development tool), bake/write some code and add the conditions to the generateList call as you would need to do for any polymorphic model definition. if you use 3 models opperating on the same table, just edit the model's beforeFind to add a condition to only return rows with 'type' = middle (for example). hth, AD --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Multiple N to Distinct N Model
The right way to do this would be to have 3 models each of which uses the same table ( letter_contents ) with the correct conditions defined ( see finderQuery ) or place the conditions within the Letter model's relationships. [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? > > > > > -- View this message in context: http://www.nabble.com/Multiple-N-to-Distinct-N-Model-tp14235103p14289751.html Sent from the CakePHP mailing list archive at Nabble.com. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Multiple N to Distinct N Model
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 cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: Multiple N to Distinct N Model
The 'problem' seems to be with generateList() which is used by scaffolding to provide data for the 'select' construct. It ignores the condition you set for Letter model. You may consider 2 options: 1) if you want to take advantage of scaffolding then you might need to break the table 'letter_contents' into 3. 2) If you prefer the existing table design, you might need to run bake again to re-create the controller (select option that creates 'add', edit, view, delete functions). Then in the 'edit' functions, provide condition parameter for generateList(). Zonium On Dec 8, 6:46 pm, Santa <[EMAIL PROTECTED]> wrote: > I have a model situation that I cannot find in the group anywhere. I > have spent nearly a day searching. I am hoping there is a model guru > that can help. > > First, to help you understand, I am putting together a pre-canned form > letter tool. I will have three sections to the letter using all pre- > written content; the opening, the middle, and the end. Each letter > will have 1 of each prewritten section, everytime, all the time, no > acceptions. So when I create a letter, I choose from the different > sections of pre-written letter content to create my letter. Here is > how I modeled it. > > CREATE TABLE `letter_contents` ( > `id` int(11) NOT NULL auto_increment, > `letter_section` enum('Opening','Middle','Ending') NOT NULL, > `letter_content` text NOT NULL, > PRIMARY KEY (`id`) > ); > > CREATE TABLE `letters` ( > `id` int(11) NOT NULL auto_increment, > `openingid` int(11) NOT NULL default '0', > `middleid` int(11) NOT NULL default '0', > `endingid` int(11) NOT NULL default '0', > PRIMARY KEY (`id`) > ); > > Now when I set the controllers to use scaffolding and write the models > so that only openings show for the openingid and only midddle shows > for the middleid, etc., it doesn't return the expected result. In the > drop down for openings, I show ALL the letter_content. Here are my > models. > > LETTER MODEL > class Letter extends AppModel { > var $name = 'Letter'; > var $belongsTo = array( > 'Openings' => > array('className' => 'LetterContent', > 'conditions' => > "letter_contents.letter_section = 'Opening'", > 'order' => '', > 'foreignKey' => 'openingid' > ), > 'Middle' => > array('className' => 'LetterContent', > 'conditions' => > "letter_contents.letter_section = 'Middle'", > 'order' => '', > 'foreignKey' => 'middleid' > ), > 'Ending' => > array('className' => 'LetterContent', > 'conditions' => > "letter_contents.letter_section = 'Ending'", > 'order' => '', > 'foreignKey' => 'endingid' > ), > ); > > } > > I have also tried to put a $hasMany in the LetterContent model as > follows: > > var $hasMany = array( > 'LetterOpening' => > array('className' => 'Letter', > 'conditions' => 'Letter.openingid = id', > 'order' => '', > 'foreignKey' => 'id' > ), > 'LetterDeeds' => > array('className' => 'Letter', > 'conditions' => 'Letter.deedsid = id', > 'order' => '', > 'foreignKey' => 'id' > ), > ); > > What I do not want to do if it can be avoided, is strip the content > out into three separate tables. From my perspective, there is no need > to do that. This is 3NF from my perspective. If I were writting the > content each time from scratch, I would store the letter data right > inside the table. So this is no different from that. > > Anyone have any idea how to get this model to work so that all my "pre- > written" content can reside in one table and use the letter_section to > know what content to show in the various sections in the Letter model? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---
Multiple N to Distinct N Model
I have a model situation that I cannot find in the group anywhere. I have spent nearly a day searching. I am hoping there is a model guru that can help. First, to help you understand, I am putting together a pre-canned form letter tool. I will have three sections to the letter using all pre- written content; the opening, the middle, and the end. Each letter will have 1 of each prewritten section, everytime, all the time, no acceptions. So when I create a letter, I choose from the different sections of pre-written letter content to create my letter. Here is how I modeled it. CREATE TABLE `letter_contents` ( `id` int(11) NOT NULL auto_increment, `letter_section` enum('Opening','Middle','Ending') NOT NULL, `letter_content` text NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `letters` ( `id` int(11) NOT NULL auto_increment, `openingid` int(11) NOT NULL default '0', `middleid` int(11) NOT NULL default '0', `endingid` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ); Now when I set the controllers to use scaffolding and write the models so that only openings show for the openingid and only midddle shows for the middleid, etc., it doesn't return the expected result. In the drop down for openings, I show ALL the letter_content. Here are my models. LETTER MODEL class Letter extends AppModel { var $name = 'Letter'; var $belongsTo = array( 'Openings' => array('className' => 'LetterContent', 'conditions' => "letter_contents.letter_section = 'Opening'", 'order' => '', 'foreignKey' => 'openingid' ), 'Middle' => array('className' => 'LetterContent', 'conditions' => "letter_contents.letter_section = 'Middle'", 'order' => '', 'foreignKey' => 'middleid' ), 'Ending' => array('className' => 'LetterContent', 'conditions' => "letter_contents.letter_section = 'Ending'", 'order' => '', 'foreignKey' => 'endingid' ), ); } I have also tried to put a $hasMany in the LetterContent model as follows: var $hasMany = array( 'LetterOpening' => array('className' => 'Letter', 'conditions' => 'Letter.openingid = id', 'order' => '', 'foreignKey' => 'id' ), 'LetterDeeds' => array('className' => 'Letter', 'conditions' => 'Letter.deedsid = id', 'order' => '', 'foreignKey' => 'id' ), ); What I do not want to do if it can be avoided, is strip the content out into three separate tables. From my perspective, there is no need to do that. This is 3NF from my perspective. If I were writting the content each time from scratch, I would store the letter data right inside the table. So this is no different from that. Anyone have any idea how to get this model to work so that all my "pre- written" content can reside in one table and use the letter_section to know what content to show in the various sections in the Letter model? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com 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 -~--~~~~--~~--~--~---