No I haven't used Bake as yet, but the structure is really simple. I
ll skip the answers part because that is not relevant here
Survey Model
---ID (primary key)
---Name
Question Model
---ID (primary key)
---Text
---Survey ID (foreign key)
so what happens is I am looking at all the surveys I have, and I
decide to add a question to a particular survey, so I call the add
function from questions controller passing it the parameter,
(survey_id) to use for its foreign key field. The add action is a form
where i take the question text and recalls the same add action with
$this->data and the passed parameter.
function add($surveyid=null)
debug($this->Question->id);
if (!empty($this->data)) {
$this->data['Question']['survey_id']= $surveyid;
if($this->MasterQuestion->save($this->data, array('validate' =>
true))){
//redirect me to another page
}
}
}
Now when we submit the form and enter the function, $this->Question-
>id is magically made equal to the $surveyid. So when save() is called
it thinks that it needs to update the db which has the given id. As
you can see the the debug function is outside the if statement so it
runs even before i press submit. If i pass parameter 1($surveyid = 1),
$this->Question->id is set to 1. If i pass parameter =2, $this-
>Question->id is set to 2 and so on. Thanks.
On Nov 14, 8:44 am, Pablo Terradillos <[email protected]> wrote:
> Also, have you tried to generate code with bake? it could be helpful or you
> could see what are you doing wrong at least
>
> http://book.cakephp.org/view/1522/Code-Generation-with-Bake
> Pablo E. Terradillos
>
> On Sun, Nov 14, 2010 at 12:43 AM, Pablo Terradillos <[email protected]>wrote:
>
> > I'm not sure if i understand correctly your first issue...
> > Have your associated model the corresponding foreign key and, also, a
> > primary key?
> > For example,
>
> > Questions
> > id
> > text
>
> > Answers
> > id
> > question_id
> > text
>
> > Regarding the second issue,
>
> > remember to enclose modelName.$i.field with double quotes so $i is treated
> > as a variable or you could use 'modelName.'.$i.'field'
>
> > Pablo E. Terradillos
>
> > On Sat, Nov 13, 2010 at 11:39 PM, Rameez <[email protected]> wrote:
>
> >> Thank you for your response, I in fact did do what you said about the
> >> first problem and resolved that error. I was passing the parameter to
> >> a page that has a form. The create function in the view, would recall
> >> the same action using the $options as 'action'. I added the 'url tag
> >> to the' $options array. Without the tag my parameters kept
> >> disappearing
>
> >> 1) However now I have come across a new problem. The parameter I pass
> >> is to set the foreign key field of the Question (Survey ID). But when
> >> I save $this->data, it takes the id to be the same argument
> >> automatically. So it goes into the database and over writes the entry
> >> with that ID instead of making a new entry.
> >> Here is my view code:
>
> >> echo $form->create('ModelName', array('url' => array('action' =>
> >> 'create',$this->passedArgs[0])));
> >> echo $form->input('text',array('label'=> 'Enter the Question Text'));
> >> echo '<div><input type="submit" value="Next" />
> >> echo $form->end();
>
> >> Inside the controller i just try to debug the id
> >> Code:
> >> if (!empty($this->data)) {
> >> debug($this->ModelName->id);
> >> $this->ModelName->save($this->data, array('validate' => true));
> >> redirect(somewhere);
> >> }
>
> >> 2) As for the second question, saveAll() tries to save all the answers
> >> options with the same primary id which is zero for this case. If I use
> >> a loop I change the syntax like this ModelName.0.FieldName becomes
> >> ModelName.$i.Fieldname where $i is the loop variable. This doesnt save
> >> anything.
>
> >> On Nov 14, 3:45 am, Pablo Terradillos <[email protected]> wrote:
> >> > Have your model the corresponding associations?
> >> > Questions have many answers?
>
> >> > if so, you could check what your form is POSTing using
> >> debug($this->data);
> >> > in order to check if you are making all in a the correct way
>
> >> > You may not have any problems putting the form generator in a loop, as
> >> long
> >> > as your associations are correct (is saveAll working whenever you try to
> >> > save the answers without the loop?)
>
> >> > Pablo E. Terradillos
>
> >> > On Fri, Nov 12, 2010 at 10:53 PM, Rameez <[email protected]>
> >> wrote:
> >> > > I am new to cake php so please bear with me. I am trying to create a
> >> > > survey for which i can create multiple choice questions. i am running
> >> > > into two issues
>
> >> > > 1) for multiple choice questions i have a form that takes all the
> >> > > answer choices and saves them as separate entries inside the answers
> >> > > database table. Can someone tell me how to take same input field for
> >> > > multiple entries in a loop? This because i have a varying number of
> >> > > answer choices for every question. The cook book only shows how to it
> >> > > indivudially using 'Modelname.0.fieldname' and Modelname.1.fieldname
> >> > > and so on. I tried replacing the numbers with a loop variable but then
> >> > > the saveall() didnt work?
>
> >> > > 2) i cant pass parameters from an action inside the surveys controller
> >> > > to an action inside the questions controller. Can someone help me with
> >> > > this? The url indicates that the parameter is being passed, but the
> >> > > receiving function always gets a null.
>
> >> > > Could someone help me with this. Thank you in advance.
>
> >> > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp
> >> others
> >> > > with their CakePHP related questions.
>
> >> > > You received this message because you are subscribed to the Google
> >> Groups
> >> > > "CakePHP" group.
> >> > > To post to this group, send email to [email protected]
> >> > > To unsubscribe from this group, send email to
> >> > > [email protected]<cake-php%[email protected]>
> >> <cake-php%[email protected]<cake-php%[email protected]>>For
> >> more options, visit this group at
> >> > >http://groups.google.com/group/cake-php?hl=en
>
> >> Check out the new CakePHP Questions sitehttp://cakeqs.organd help
> >> others with their CakePHP related questions.
>
> >> You received this message because you are subscribed to the Google Groups
> >> "CakePHP" group.
> >> To post to this group, send email to [email protected]
> >> To unsubscribe from this group, send email to
> >> [email protected]<cake-php%[email protected]>For
> >> more options, visit this group at
> >>http://groups.google.com/group/cake-php?hl=en
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" 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