How to pass data from one controller to another in CakePHP 3? - cakephp-3.0

I wanna pass a variable from userscontroller to salescontroller in my project in CakePHP 3 and use it in salescontroller .But I don`t wanna to redirect from an action to another one (not passing variables in url) how can I do it?

Edit : to pass data between controller, you can use Session,
$session = $this->request->session();
how to read and write read link bnelow
http://book.cakephp.org/3.0/en/development/sessions.html
https://stackoverflow.com/questions/30568653/how-to-read-and-write-session-in-cakephp-3-0

Related

Can't save the data using wbragancia widget yii2 for more than one dynamic form in single view

I am new in yii2 and i am using wbraganca/yii2-dynamicform widget in my view. My question is that I have to use more than two dynamic form in a single view. For single dynamic widget it works fine while saving from controller but others are saving just the last data of dynamic widget, while inspecting the post data in network array index for others except first dynamic form are not increasing (there index is only zero). Would you please suggest how should I have to do. How can I use multiple dynamic-form widget in single view. Thank you in advance.
I believe the problem must be in your view. You should ensure that each control has a unique name and then loop through the post variables in the controller.
<?= $form->field($modelx, "[{$i}]MyVariableName", ['labelOptions' => ['label' => false]])->textInput(['name' =>'myClassNameHere'."{$block}[$i][MyVariableName]"]) ?>
See the 'name' option in the code above. It gives a unique name to each field. Here the $block variable is a unique code I append to each widgetItem class in the DynamicFormWidget.
If I now look at my POST data, I see something like the following:
myClassNameHere0[0][MyVariableName]:1
myClassNameHere1[0][MyVariableName]:11
If this does not help - post a simply example that you cannot get working and I will have a look at it.

Yii2: How to send multiple parameters in redirect

I am redirecting from login page to dashboard page. I want to send teams variable that contain my existing data about the teams. I have tried following but that is not working...
return $this->redirect(array('site/dashboard', ['model' => $model1, 'teams' => $teams]));
model1 is getting sent but not teams...
As arogachev already pointed, the second param is the code, BUT the first param is an array. In order to put some parameters, you need to do something like this:
Yii::$app->response->redirect(['site/dashboard','id' => 1, 'var1' => 'test']);
So for every param you put extra item in the array where the key is the name and the value is the value of the get param.
Cheers!
Change your line to following. Since you have already used array no need for square brackets.
return $this->redirect(array('site/dashboard', 'model' => $model1, 'teams' => $teams));
See official documentation about this method.
The second parameter is status code, so what you are doing is completely wrong.
Passing the variables here does not make any sense because immediately another action starts to load.
You should pass variables to view in action to which you redirect and not where actual redirect happens.

Make grails pagination

My controller returns results in a Array like this:
render (template: 'show' , model: [changes: changes])
Show is a default action and changes is an Array created with a query(createCriteria).
How can I make pagination in my index.gsp page which is displaying results in a HTMl div?
My problem:
Controller:
def list() {
[terminHistorie: changes, terminHistorieCount: changes.size()]
}
gsp:
<g:paginate controller="terminHistorie" action="list" total="${terminHistorieCount}"/>
Error: Tag [paginate] is missing required attribute [total] at C:/Users/matejb/Documents/NetBeansProjects/gutmann/grails-app/views/terminHistorie/index.gsp:309
The way pagination works is you first need to create a view using paginate tag, then on your show action you need to slice your changes. Meaning that the view will send the offset and max parameter and your createCriteria will query the database only for those data. Take a look at this page and then here to see how you can pass max and offset into your createCriteria method.
To see an example, see this page or create a new application with one simple domain and generate scaffolding for it. Grails will generate a paginated result for you.
Btw, if you are using ajax you might want to use remote paginate plugin.

How to insert json encoded inputs into foreign related tables

I am creating functionalities in yii+extjs. My client side is desighned in extjs and server side is in yii framework.
I am having two tables as
Poll Option
-pollId -optionId
-PollQuestion -option
-pollId
Now through poll creation form which will be in extjs,Question and its related option gets send to server in json format. So in yii framework,in actionCreate function will get input as-
$json='{"pollId":1,"PollQuestion":"Who is the best
cricketer","option":"ABC","option":"DEF","option":"XYZ"}'
$obj=json_decode($json);
During creation of poll,user can enter any number of options.So number of options can be anything.
i am creating above functionality in Pollcontroller.
So this newly created question gets inserted into Poll table as=
$model=new Poll();
$model->pollId=$obj->pollId;
$model->PollQuestion=$obj->PollQuestion;
Now i want to put all these new options in option table with same pollId. So how to add all these options in option table? Please help me
I would start by modifying the JSON so the options are a separate JSON within the pollquestion JSON.
Something like this...
$json='{"pollId": 1,"PollQuestion": "Who is the best cricketer",
"options":{[{"value":"ABC"},{"name": "DEF"},{"name": "XYZ"}]}';
That way when you decode it with json_decode you'll get an options array which you can go through and add each of the elements in that array in the options array.
for($i=0; $i<sizeof($obj['options']);$i++){
//Add to table logic here
}

Last inserted id in cakephp

I use this code but its not working in cakephp and the code is:
$inserted = $this->get_live->query("INSERT INTO myaccounts (fname) values('test');
After this im using:
$lead_id = $this->get_live->query("SELECT LAST_INSERT_ID()");
It's working, but only one time.
Try this. Lots less typing. In your controller, saving data to your database is as simple as:
public function add() {
$data = "test";
$this->Myaccount->save($data);
// $this->set sends controller variables to the view
$this->set("last", $this->Myaccount->getLastInsertId());
}
You could loop through an array of data to save with foreach, returning the insertId after each, or you could use Cake's saveAll() method.
Myaccount is the Model object associated with your controller. Cake's naming convention requires a table called "myaccounts" to have a model class called "Myaccount" and a controller called "Myaccounts_Controller". The view files will live in /app/views/myaccounts/... and will be named after your controller methods. So, if you have a function add()... method in your controller, your view would be /app/Views/Myaccounts/add.ctp.
The save() method generates the INSERT statement. If the data you want to save is located in $this->data, you can skip passing an argument in; it will save $this->data by default. save() even automagically detects whether to generate an UPDATE or an INSERT statement based on the presence of an id in your data.
As a rule of thumb, if you're using raw sql queries at any point in Cake, you're probably doing it wrong. I've yet to run into a query so monstrously complex that Cake's ORM couldn't model it.
http://book.cakephp.org/2.0/en/models/saving-your-data.html
http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html?highlight=getlastinsertid
HTH :)
You can get last inserted record id by (works for cakePHP 1.3.x and cakePHP 2.x)
echo $this->ModelName->getLastInsertID();
Alternately, you can use:
echo $this->ModelName->getInsertID();
CakePHP 1.3.x found in cake/libs/model/model.php on line 2775
CakePHP 2.x found in lib/Cake/Model/Model.php on line 3167
Note: This function doesn't work if you run the insert query manually
pr($this->Model->save($data));
id => '1'
id is a last inserted value