How to call view in another view but with triggered controller - cakephp

Hi how could I render one view inside another but with its data...
for example I have view home.ctp, than controller for news, and news.ctp
I want to render in home.ctp news.ctp but first all news has to be selected from database...
I tried this
echo $this->elements(../News/news);
but there is no selected data to be displayed.. it returns error undefined variable.
Thanks

CakePHP cell is the right thing to solve such type of problems. In this document there is a easy tutorial. you can check it

Related

Where to put a function to create a pdf

I have an application which deals with projects evolving according to a process defined by a series of status transitions. I have a button to set a project to the next status. The button calls a function in the ProjectsController. This function calls another function in the Project model, where I search for the correct transition, depending on the current status, the user_group and some other parameter, and set the new status. After everything successfully accomplished, I return to the original page with 'return $this->redirect($this->referer());' from the controller.
Some of the transitions have side effects. One is to create a PDF, save it on the server and add a new entry to the 'documents' table referenced to the current project.
Problem: where should I put the function to create the PDF? I would like to put it to the Model. But I need some View Files to first render a html page and then convert it to the PDF. I could put it to the Controller. But then I have a controller function which should not be called directly an doesn't render a view.
The functions all work, but where to put them? Is there another possibility? Would it be possible to use a component?
Implement it as PdfView that will render any (pdf) view for you as Pdf. There is already a plugin that does this, search for CakePdf on Github.
A component is clearly the wrong place. A Pdf is output, so it's a kind of view. Model gets you the data, controller sets it, view renders it.

AngularJS: ng-model not getting applied on Controller instantiation

I am creating a web app planner using Angular and I am having some difficulties with a <select> box that is not changing value based on the variable denoted with ng-model.
My architecture is as follows:
I am using ui-router which gives me different view states, one for each page of my planner. The root HTML page has a Controller called MainController. This is where I set up my JSON model, $scope.Master = {} that I want to use throughout the planner. All pages of the planner should inherit this model and continue to modify/add to it.
I then have my 4 pages of the planner like:
Start -> Accounts -> Settings -> Review
Each page has its own Controller that gets instantiated every time I visit the page. On the Start page, I have a <select> box that has ng-model="$scope.Master.Start.selectedAccount" that gets populated dynamically using the StartController (therefore it gets populated every time I come to the Start page).
This <select> works great on the first time to the page, but if I go to Accounts and then come back, the select box is back to the default value, "Please select an account", instead of the selected account that is in the $scope.Master.Start.selectedAccount model that is bound to the <select> box
I thought I could just do something like $scope.$apply or something in order to re-apply the binding to the DOM object, but that just gave me an error saying it is already digesting.
How can I apply the binding to the <select> box after the page has been loaded 2 or more times?
This is probably because every time you go back to your original page, a new controller is instantiated since it was removed from the DOM when you left it originally. Thus $scope.Master.Start.selectedAccount. To save this, you can either
Use a service / factory singleton on the main app to save this value
https://docs.angularjs.org/guide/services#!
Save $scope.Master.Start.selectedAccount as a global variable
https://docs.angularjs.org/api/ng/service/$rootScope
Put that controller on the outside
Im real sorry... I realized I was populating the <select> using ng-repeat instead of ng-options no idea how I managed that... That was the problem

Backbone view based on model from collection, how to update view?

I am looking for a bit of direction I am still fairly new to Backbone and am currently creating a test application to learn more.
My problem is this, I am populating a backbone view with a underscore template. I load a collection of models, then I find the model I need and populate these values into the template. There can be many pages based on the template so I have a dynamic route that accepts an id.
My problem is I want to add a next feature, that would change the current page and reload the template with the new model.
I have tried a crude method along the lines of :
Backbone.history.navigate(newLocation)
However this did'nt work, please note newLocation is actually populated with the route and the id I want to navigate to.
I will add some code from my view below, I won't include the full code however if it is needed please ask.
Any help or a push in the right direction would be great.
Thanks in advance
You need to use your router object's navigate method rather than than history's class method, and you need to pass it the option `{trigger: true} in order to invoke the corresponding route function.

How to access a Model that isn't part of a Controller in CakePHP

I am currently working in a controller called Content. In this controller, I have a function that will be called when someone goes on to the homepage, and most of the data will be pulled from the Content model.
However, I want to display data from another model called Phones. When I type this code:
$phones = $this->Phone->find('all');
I get the following error:
Fatal error: Call to a member function find() on a non-object
When I change the Phone part of the PHP code to Content it works fine. So I'm guessing that I can't, at the moment, access the Phone model from inside the Content controller.
Is there a way to achieve a way of accessing a model externally from a controller?
$this->loadModel('Phone'); # Important: singular!!!
$this->set('phones', $this->Phone->find('all'));
try with below
$this->loadModel('Phone');
$this->set('phones', $this->Phone->find('all'));
cheers

How can I get record ID on Edit view layout?

How can I get ID of current record, on Edit view layout?
You can load the Model in question and access it that way from within the view (which, in my opinion, is an awful idea) or you can 'set' it within your controller action:
// In the controller action that renders the view
$this->set('current_id',$this->ModelName->id);
// Access it this way in the view/layout:
<?php echo $current_id; ?>
You could also grab the current record ID using ajax, but that doesn't sound like something that would benefit you here.
Good luck
Use $this->data['ModelName']['id'] in the view
Since 2.3, as long as you have $this->Form->hidden('fieldname'); you can reliably use $this->Form->value('ModelName.fieldname') for the value of a field in the form. When the view is rendered, just as html input tags get a value based on the model used in the controller and referenced in the $this->Form-create('ModelName'), when rendering the $this->Form->value('ModelName.fieldname') has a value.
In the controller, $this->request->data['ModelName'] is set to an array of the Model's data in CakePhp, where ['id'] points to the value of the id field.
$this->Form->create('ModelName');
$this->Form->input('id');
$this->Form->end();
Corresponds to controller logic where $this->request->data['ModelName'] gets an associative array with at least "id" => some id value depending on the data read from the database.
Note that when you use $this->Form->value() to retrieve form data, it will work when the screen is initially rendered by the controller, but may fail when the screen is rendered again because a model validation rule that used a function in the model definition failed.
To make sure that the $this->Form->value() is reliable with more rigorous model validation logic, use $this->Form->hidden('fieldName'); to hold the data in $this->request->data['ModelName'] throughout multiple model validation iterations.

Resources