I am new to YII. How to call an action from another action of the same controller.
Suppose I am in action A. I need to call action B with two parameters. My controller name is Master Controller. How will I perform this. Please say me a solution. Thanks in advance.
My controller:
class NimsoftController extends Controller
{
public function actionStore_action($action,$data_id)
{
$model= new NimsoftUserLoginAction;
$model->user_id=Yii::app()->user->id;
$model->action=$action;//"Deleted";
$model->affected_data_id=$data_id;//"22";
$model->save();
}
public function actionSearch($id)
{
$cust_id = $id;
$criteria = new CDbCriteria();
$criteria->condition = "host_customer_id = '$cust_id'";
$details = NimsoftHost::model()->find($criteria);
$criteria2 = new CDbCriteria();
$criteria2->condition = "cust_id= '$details->host_customer_id'";
$name = MasterCustomers::model()->find($criteria2);
$dataProvider = new CActiveDataProvider('NimsoftHost', array(
'criteria' => $criteria,
'pagination'=>array('pageSize'=>40)
));
$model = new NimsoftHost();
$this->actionStore_action(array('action'=>'search', 'data_id'=>$cust_id));
$this->render('index',array(
'dataProvider' => $dataProvider,
'details' => $details,
'name' => $name->cust_name,
'model' => $model
));
}
}
You can simply call the function directly, or,
$this->actionMyCustomAction(); exit;
you can perform a redirect to the action:
$this->redirect('anotherControllerName/myCustomAction');
or this if you are in the same controller:
$this->redirect('myCustomAction');
UPDATE:
You are passing the variables wrongly:
$this->actionStore_action(array('action'=>'search', 'data_id'=>$cust_id));
It should be two separate variables:
$this->actionStore_action('search', $cust_id);
This has NOTHING to do with Yii. It's only basic function calling.
$this->forward('site/contact');
In case of forward your URL will not be change and you cannot send extra parameter in this.
If you want to send extra parameter then use redirect.
$this->redirect(Yii::app()->createUrl('site/contact', array('id' => '22')));
Hope this will help you..
You can do it this way also:-
Now you want to call action Name1 inside action Name2.
public function actionName1($param1,$param2) {
//your code...
}
public function actionName2() {
$this->redirect(array('name1','param1'=>'param1','param2'=>'param2'));
}
In case of forward your URL will not be change and you cannot send extra parameter in this.
$this->redirect('contact');
Related
I need to use the function in my view/template page.
My code:
public function getAppNumber($id = null){
$aPPLICATIONINFO = $this->AppEstLto->APPLICATIONINFO->find('all', [
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
foreach($aPPLICATIONINFO as $aPPLICATIONINFO){
$aPPLICATIONINFOx = $aPPLICATIONINFO;
}
return $aPPLICATIONINFOx;
}
You can use set() to use the function variables in your view as given in cookbook:https://book.cakephp.org/3/en/views.html#setting-view-variables
public function get_app_number($id = null){
$applicationInfo = $this->AppEstLto->APPLICATIONINFO->find('all',
[
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
//Create an array
$applicationArray = new array();
//Store all results in array
foreach($applicationInfo as $application){
$applicationArray = $application;
}
// Pass the array to view
$this->set(compact('applicationArray'));
}
Now, you can use it in your view:
get_app_number.ctp:
<?php
foreach($applicationArray as $application)
{
echo $application['application_number'];
}
?>
You should be doing something like this in your routes.php file inside your Config folder:
Router::connect('/get-app-number', array('controller' => 'yourController', 'action' => 'get_app_number'));
That way you will be able to connect the url that will be used for your view.
Your action corresponds and sends data to your view.
The action is the function that is inside your controller in which you developed for setting the data and variables.
The example of the slug which would be generated:
http://localhost/get-app-number
I'm trying to create simple form with Zend, I need to use this form in most part, so I create the default form then in controller i modify it for the occurrence with private function. But I have two problems:
the form getValues() doesn't take the value of text element.
I put render at the end of the form action, but it doesn't render to the right page.
The form consists of a text field and the sumbit button
Here is the code of my controller:
That is for customize the form
private function getSearchForm($action = '', $name, $type, $placeholder)
{
$urlHelper = $this->_helper->getHelper('url');
$this->_searchForm = new Application_Form_Admin_Search_Search();
$this->_searchForm->setName($name);
$text = $this->_searchForm->getElement('ricerca');
$text->setLabel('Ricerca '.$type);
$text->setName($type);
$text->setAttrib('placeholder', $placeholder);
$this->_searchForm->setAction($urlHelper->url(array(
'controller' => 'admin',
'action' => $action),
'default'
));
return $this->_searchForm;
}
there are the actions:
public function pneumaticoAction()
{
$this->_searchForm = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
$this->view->searchForm = $this->_searchForm;
}
public function pneumaticosearchAction()
{
if (!$this->getRequest()->isPost()) {
$this->_helper->redirector('index', 'public');
}
$form=$this->_searchForm;
if (!$form->isValid($this->getRequest()->getPost())) {
$this->render('pneumatico');
}
$values = $form->getValues();
$this->view->assign(array(
"pneumatici" => $this->_modelAdmin->searchPneumatici($values['pneumatico'])
));
$this->render('pneumatico');
}
First question, whenever you get routed to pneumaticosearch action, you do not set $this->_searchForm but you have it as:
$form=$this->_searchForm;
Should be something like this:
$form = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
And the second question. When you run render, it is similar to pass $this->view parameters to .phtml. I don't see your view files, but I guess you need to set view first:
$this->view->searchForm = $form
I have one temporary model as viewModel. In my CRUD actions (for example actionCreate) I want to get this viewModel data and assign that to a ActiveRecord model. I used below code but my model object atrribute always show NULL value for attributes:
$model = new _Users();
if ($model->load(Yii::$app->request->post())) {
Yii::info($model->attributes,'test'); // NULL
$attributesValue =[
'title' => $_POST['_Users']['title'],
'type' => $_POST['_Users']['type'],
];
$model->attributes = $attributesValue;
Yii::info($model->attributes,'test'); // NULL
$dbModel = new Users();
$dbModel->title = $model->title;
$dbModel->type = $model->type . ' CYC'; // CYC is static type code
Yii::info($dbModel->attributes,'test'); // NULL
if ($dbModel->save()) {
return $this->redirect(['view', 'id' => $dbModel->id]); // Page redirect to blank page
}
}
else {
return $this->render('create', [
'model' => $model,
]);
}
I think $model->load(Yii::$app->request->post()) not working and object attribute being NULL. Is it Yii2 bug or my code is incorrect??
If there is no rule for your attribute the $model->load() will ignore those not in the rules of the model.
Add your attributes to the rules function
public function rules()
{
return [
...
[['attribute_name'], 'type'],
...
];
}
To fetch data for an individually attributes(db-fields) in yii2.0 then you should just do as:
echo $yourModel->getAttribute('email');
ActiveRecord $attributes is a private property
Use $model->getAttribute(string)
You can use following codes:
$model = new _Users();
$model->attributes=Yii::$app->request->post('_Users');
$model->title= $model->title
$model->type = $model->type . ' CYC'; // CYC is static type code
#$model->sampleAttribute='Hello World';
Declare attribute as private then
echo $yourModel->attribute
work as expected
You must remove all public properties (title, type, etc.) in your _User model and $model->attributes = $post will work correctly.
I have also encountered the same problem, i Add my attributes to the rules function,but also error. And i found the reason for this problem. It is beause that the submit form's name in corresponding view file is not the same as the model's name which you use in controller
[controller file]:
$model=new SearchForm();
[view file]:
<input name="SearchForm[attribus]" ...
or
[view file]:
<?= $form->field($model,'atrribus')->textInput()?>
I want to add a download button for each row in moduleadmincontroller helper.
I tried to add it by using the following code on RenderList function. But it is not working.
$this->addRowAction('download');
Kindly let me know if I can add custom action for each row and how to process it.
as you know the actions is the default array that have default value array('view', 'edit', 'delete', 'duplicate'); and you can use this but if you want add new action you should use some function.for example you can go to your_prestashop/controllers/admin/AdminRequestSqlController.php
this class add new action with 'export' name
$this->addRowAction('export');
then for create link for this action it is using the displayExportLink() function as you can see in bellow code
public function displayExportLink($token, $id)
{
$tpl = $this->createTemplate('list_action_export.tpl');
$tpl->assign(array(
'href' => self::$currentIndex.'&token='.$this->token.'&
'.$this->identifier.'='.$id.'&export'.$this->table.'=1',
'action' => $this->l('Export')
));
return $tpl->fetch();
}
and then you can get your new action with the initProcess() function or initcontent() function and do something lik download
public function initProcess()
{
parent::initProcess();
if (Tools::getValue('export'.$this->table))
{
$this->display = 'export';
$this->action = 'export';
}
}
I'm using a custom datasource to consume webservice.
Create, Read and Update work well but Delete doesn't works.
Here is my code calling the delete method in my controller.
public function delete($id){
$this->autoRender = false;
debug($this->Article->delete($id));
}
And here the code in my datasource
public function delete(Model $Model, $id = null) {
echo "Display a message if this method is called";
$json = $this->Http->post(CakeSession::read('Site.url') . '/webservice/delete/', array(
'id' => $id,
'apiKey' => $this->config['apiKey'],
'model' => $Model->name
));
$res = json_decode($json, true);
if (is_null($res)) {
$error = json_last_error();
throw new CakeException($error);
}
return true;
}
But when I want to delete an item, the debug(); display false.
I have no other displays.
I don't understand why my delete method isn't called correctly.
Is there something wrong in my code ?
Thanks
Let's check: you're only passing a parameter to your method:
$this->Article->delete($id)
According to the method that you created, the first parameter, which is required, is the Model. The second is $id:
public function delete(Model $Model, $id = null)
During the method you want to use both parameters. Here:
'id' => $id
And here:
'model' => $Model->name
Based on this, you need to review how this method will be called. BTW, if you want override delete() method, according the book, you need something like this: delete(int $id = null, boolean $cascade = true).