Model and controller structure in CakePHP [closed] - cakephp

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I Want to separate the database functionality and logic part by having the database operations in the model and the logic part in the controller. Earlier I had all the code in the action part of the controller itself. I have tried something, but it doesn't work.
This is what I had earlier:
/* Controller */
function insertFormName(){
$formname=$_POST['formname'];
$ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
$newid=$ret[0]['forms']['id'];
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
}
And now I changed it a bit, which doesn't work:
/* Controller */
function insertformname()
{
$this->data['Form']['formname']=$this->params['form']['formname'];
$this->Form->save($this->data);
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
}
/* Model */
function save($data)
{
$ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
$newid=$ret[0]['forms']['id'];
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
return $newid;
}
EDIT:
I have tried it another way.. Have the entire functionality in the model and just call that function from the controller.Is this method correct?
/* Model */
function saveFormName($data)
{
$this->formname=$data[$this->name]['formname'];
$ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
$newid=$ret[0]['forms']['id'];
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
}
/* controller */
function insertformname()
{
$this->data['Form']['formname']=$this->params['form']['formname'];
$this->Form->saveFormName($this->data);
}

It looks like you should probably revisit the Cake book (book.cakephp.org) and redo the lessons. If you setup your form correctly, you shouldn't have to manually assign $_POST['formname'] to $this->data. Try setting the field names in your form (in the HTML) to data[ModelName][FieldName].
Next:
$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
Why are you updating data right after you saved it? Where do the $newid and $formname variables come from? You have them defined in the Model::save, but not in the controller.
This seems like you're trying to fight with the Cake automagic stuff too much. Perhaps you should repost your question, but please spell out your high level description rather than just a "why doesn't this work?" It looks to me like this could be simplified a ton, but, again, I'm not quite sure what your objectives are.
Respectfully,
Travis

Related

How to continue a quiz with a collection of data entered by the user in laravel5.1

I'm gonna go straight forward for this. This question is connected with my passed question, I will offer you the bounty within 3 days from now if you solved this problem.
What I want
After the user answered a quiz, the user can save it without submitting, so they can edit/continue later. Now, after they saved, how can I loop through the user's answer in the current loop of questions in a quiz? So that the user can edit or continue the quiz.
What I have
database:
quiz_result_of_user has 9 fields, ['id', 'quiz_id', 'user_id', 'question_id', 'answer', 'question_type', 'attempt_number', 'marks', 'grades']
This table saves all the user's answer so this serves as the history table of the user's quiz.
quizattempt_user has 3 fields, ['id', 'quiz_id', 'user_id']
This table saves all the user's attempt, so I can reference all the answer of the user where the id = attempt_number in the quiz_result_of_user table.
Controller - Update
$quiz = Quiz::with('multiple_choices.answers', 'true_false', 'short_answer', 'descriptions')->findOrFail($id);
$questions = collect($quiz->multiple_choices);
$questions = $questions->merge(collect($quiz->true_false));
$questions = $questions->merge(collect($quiz->short_answer));
$questions = $questions->merge(collect($quiz->descriptions));
$questions = $questions->sortBy('question_number');
Problem
I can now loop through the questions, and the answers of the user, but I can't figure out how can I put the user's answer because it is also a collection of data. Note: every quiz can have different types of question, multiple choice, true or false, and short answer/fill in the blank.
Based on what I understand of the table structure, we can create a dictionary of the user's answers keyed by the the question IDs, so we can easily look up the answer for each question. Here's a minimal implementation that we can expand on:
$questions = ... // see question description
$answers = QuizResults::where('quiz_id', $quiz->id)
->where('user_id', $userId)
->where('attempt_number', $attemptNumber)
->get()
->keyBy('question_id');
foreach ($questions as $question)
{
echo $question->question_number + ". " + $question->description;
if ($question is MultipleChoice) {
foreach ($question->answers as $choice) { echo $choice; ... }
}
echo 'Your answer: ' + $answers->get($question->id)->first()->answer;
}
I'm not sure if the page uses Blade to generate the results. For clarity, this is written in plain PHP, but it should be easy to rewrite this for a Blade template.

CakePHP checkbox for values 0 and 1 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I want to generate a CakePHP checkbox for a boolean field of my database, but it is resisting. I have tried in many different ways, but it's not working.
The checkbox is always 1, no matter if selected or not.
Any idea what might be the problem?
echo $this->FormTB->input(
'puntuacion',
array(
'checked' => true,
'value' => 1,
'label' => 'Puntuar esta empresa',
'class' => 'checkbox',
'data-id' => 'puntuar'
)
);
I have tried removing the "value" => 1 part, nothing changed.
As already pointed out multiple times in the comment, that is exactly how cakephp operates internally: a "not checked" is internally transferred to a boolean FALSE - which again is equivalent to integer 0 - in the controller/model.
So this suffices:
echo $this->FormTB->input(
'puntuacion', array(
'label' => 'Puntuar esta empresa',
'class' => 'checkbox',
'data-id' => 'puntuar'));
remove the 'checked' => true, 'value' => 1, part

In yii how to create retrive fields input and compare it with databse fields [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In yii i am creating forget password functionality. after clicking on forgetpassword button, server will provide one php paga with blank text field for entering primary email id. Now how to retrive this email id in controller's method and how to check wheather this email id exist in databse. Please help me...
This will help you get started, you would put a method similar to this in your controller and create a view with a password field on it.
public function actionForgotPassword(){
if(isset($_POST['email']{
$record=User::model()->find(array(
'select'=>'email',
'condition'=>'email=:email',
'params'=>array(':email'=>$_POST['email']))
);
if($record===null) {
$error = 'Email invalid';
} else {
$newpassword = 'newrandomgeneratedpassword';
$record->password = md5($newpassword );
$record->save(); //you might have some issues with the user model when the password is protected for security
//Email new password to user
}
}else{
$this->render('forgetPassword'); //show the view with the password field
}
}

How to send a pdf file as attachment in cakephp? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using cake php 2.0 version, I need to send a PDF attached email once clicked on survey button in my application?
Any help greatly appreciated.
Try this:
First of all, you should ensure the class is loaded using App::uses():
<?php
App::uses('CakeEmail', 'Network/Email');
in your function/action:
$email = new CakeEmail();
$email->attachments('/full/file/path/file.pdf')
$email->to = 'em...#email.co.uk';
$email->subject = 'Something';
$email->replyTo = $client['Client']['email'];
$email->from = $client['Client']['email'];
$email->emailFormat = 'html';
if($email->send()){
die('Email Sent!');
}else{
die('Failed to send email');
}

Extjs4 How to change a Model dynamically? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I use Extjs4 and MVC. I would like to change my Model's fields dynamically...
Something like adding a variable number of fields... Any suggests?
You can use the model.setFields(fieldsArray) function, shown here in the API. This method replaces all existing fields on the model with whatever new fields you include in the argument. There is not a static getFields method to capture the existing fields so as not to overwrite them but it is easy enough to get them using model.prototype.fields.
I did this recently to attach dynamic permission setting fields to a "User" model before I loaded the user. Here's an example:
Ext.define('myApp.controller.Main', {
extend: 'Ext.app.Controller',
models: [
'User',
],
stores: [
'CurrentUser', // <-- this is not autoLoad: true
'PermissionRef', // <-- this is autoLoad: true
],
views: ['MainPanel'],
init: function() {
var me = this;
// when the PermissionRef store loads
// use the data to update the user model
me.getPermissionRefStore().on('load', function(store, records) {
var userModel = me.getUserModel(),
fields = userModel.prototype.fields.getRange();
// ^^^ this prototype function gets the original fields
// defined in myApp.model.User
// add the new permission fields to the fields array
Ext.each(records, function(permission) {
fields.push({
name: permission.get('name'),
type: 'bool'
});
});
// update the user model with ALL the fields
userModel.setFields(fields);
// NOW load the current user with the permission data
// (defined in a Java session attribute for me)
me.getCurrentUserStore().load();
});
}
});

Resources