I want to accomplish a NOT IN clause in CakePHP, but I am not able to achieve it. I've been reading the CakePHP Cookbook and some answers here in StackOverflow but it's not working for me.
I have a Table named 'Hotel' with all its model, controller and views. In the view template I built a CakePHP Cell where I want to show other Hotels, except for the hotel that is currently being viewed. If for example, I am showing the view of a hotel with id #5, I want to show other hotels options, except for that hotel view id number.
I have the following query in the Cell Controller:
$hotels = $this->Hotels->find('all')
->where(['Hotels.id NOT IN' => $current_hotel_id])
->limit(4)
->order('rand()')
->toArray();
I want $current_hotel_id to have the value of current hotel that it's being viewed. That's the approach I am taking.
Any useful information would be appreciated.
If you need some value that is available in your controller in cell, you must pass that value to it.
First, declare an argument in cell action:
class HotelsCell extends Cell{
public function hotels($current_hotel_id){
//your code here
}
}
Then, in your view, pass argument to cell:
$this->cell("Hotels::hotels",[$hotel->id])
More info in docs: Passing Arguments to a Cell
Related
When I Press the Save button it saves the values but when I hit F5 or refresh; the values are gone, they are not visible on my VF page.And for this :
I have created VF page with standard controller and extensions.
created one controller.
And I have embedded VF page into Opportunity object.
Any idea how I can achieve this ??
Thanks.
The description is not very clear to me. What my understanding is you have a VF page with Opportunity standard controller with extension class. This page has a form which works when you save, but when whole page is refreshed the values are gone.
If my understanding is correct than here is a solution that you can try. For standard controllers to get data they need record id you can pass it through url like this:
http://na1.salefroce.com/apex/yourVFPage?id=
Now in your extension you can use following method to get the record:
public Opportunity opp;
public myControllerExtension(ApexPages.StandardController stdController) {
this.opp = (Opportunity)stdController.getRecord();
}
Now you have record values in the opp variable, you can use this to display values as long as id parameter is passed. You can populate this variable by using SOQL as well.
I'm using AngularJS and UI-Router to make a little e-commerce product gallery. It should display a grid of images representing categories of products. When the user selects a category, the grid shall display the child categories of that one, and so on, until the user reaches a level where there are only products. Clicking in a product should display its details. Im using 2 states in UI-Router, one for the gallery and one for the details.
app.config(function($stateProvider){
$stateProvider
// Gallery State
.state('gallery', {
url: '/products/:category',
templateUrl: 'views/gallery.html',
controller: 'galleryCtrl'
})
// Details State
.state('details', {
url: '/details/:id',
templateUrl: 'views/details.html',
controller: 'detailsCtrl'
});
});
In the 'gallery' state, the controller uses the :category parameter to get from the database all subcategories or products of that category to display in the grid. The 'details' state's controller uses an analog strategy to get the product's information with the :id parameter.
Now I want to make a breadcrumb element that shows the 'path' the user went through the gallery. For example, if the user selects the "Computers" category, then the "Notebooks" category and then "Foo Notebook", the breadcrumb should display:
Computers > Notebooks > Foo Notebook
I've found solutions that get the state hierarchy from UI-Router to create the breadcrumb. The problem is, with only the 2 states I created, the breadcrumb wouldn't show all categories, just the last selected one. I don't want to create a state for each category because the number and hierarchy of categories can change. Is there any way to accomplish this?
If you're really interested in documenting the way a user got somewhere: One way to go about this would be to create a service and push each state change to a data store in the service and replicated in a $cookieStore -- you can use the state change events for this.
Generally, though, whenever I've done breadcrumbs / hierarchies in the past - there is a definitive way to get to a location. Your example illustrates that; a specific notebook is under a general list category which is in turn under a parent category. So you can infer all parents when you get to a given item page. Not sure which solution best meets your needs.
I am new to cakephp. I need to send two variables to the view. In codeigniter its easy
$data['var1'] = 'One';
$data['var2'] = 'Two';
$this->load->view('myview',$data);
Now in Cakephp, I have a controller called function names search() in which I am sending an associative array to view.
$gal_providers = $this->GalProvider->getListByCategory(3,$location_id,false,9);
$this->set("gal_providers",$gal_providers);
But I need to send the variable $location_id too to the view. How can I send it ?
I read the article Using set() and compact() together, but I did not get the solution I was looking for.
The blog tutorial describes very well how to set data to the view. I recommend you to do the tutorial first, it gives you all you need to do your first steps.
You can set variables using $this->set() in your controller:
$this->set('first', 'second');
$this->set(array('foo' => 'bar', 'something' => 'else'));
The first will make the variable $first with the value second available in the view. The second will make $foo with value bar and $something with value else available in the view.
Controll::set() is setting the data to the view instances viewVars property. When the view is rendered it turns them into variables available in the view templates.
And do yourself and other people who look at your code a favour and follow the conventions and coding standards.
i am new to cakephp .. i am implementing a Timezone feature in my webapp i am using this timezone helper class in order to show time in my select box
http://bakery.cakephp.org/articles/MarkAlanEvans/2009/12/17/updated-timezone-helper
in my view i am echoing timezone in select box like this
echo $this->Timezone->select('timezone');
what i am doing right now is i am taking the value of whatever the time user has selected and then update the field of timezone in database ... what i want now is when the user wants to again change the timezone how he can see his old timezone in selectbox as a default value .. the first thing i dont know how can i add the default value to my selectbox
my helper class has this function
function select($fieldname, $label="Please Choose a timezone") {
$list = $this->Form->input($fieldname, array("type"=>"select", "label"=>$label, "options"=>$this->timezones, "error"=>"Please choose a timezone"));
return $this->output($list);
}
2nd thing is this as if i want to show the default value, obviously i have to query from the database and then retreive the old timezone of the user.. so the problem is if i have to attach the default value to my helper class for example like this
$list = $this->Form->input($fieldname, array("type"=>"select",'default'=>'$oldtimezone', "label"=>$label, "options"=>$this->timezones, "error"=>"Please choose a timezone"));
return $this->output($list);
so in order to do should i have to loadmodel in helper class and then query in helper class? is that possible ? or is that i am not going against cakephp or mvc rules ? so then my next question becomes how can i load the modal inside helper.
because if i can be able to add default value to here like this
echo $this->Timezone->select('timezone',array('default'=>'oldtimezone'));
then i think there is no need for me to make changes in the helper class as i simple pass variable from controller to this view
The proper way to pass default values to the view is to do that in the else block of the controller:
if (posted) {
//validate and save
} else {
//default values here
$this->request->data['Modelname']['timezone'] = $timeZoneFromDb;
}
This way you leverage the controllers logic and you dont have to do anything in the view/helper.
See http://www.dereuromark.de/2010/06/23/working-with-forms/ for details (default value section).
I have a model called PageMetaData that contains a title and a description. This is to be tied to any other model and to be used as the title tag and meta description for the page.
So I have a model called Brand. Brand has a field called page_meta_data_id and Brand belongsTo PageMetaData
Now on the view for Brand I can run this code:
if(!empty($data['PageMetaData']['title']))
{
$this->set('title_for_layout', $data['PageMetaData']['title']);
}
else if(!empty($data['Brand']['name']))
{
$this->set('title_for_layout', $data['Brand']['name']);
}
if(!empty($data['PageMetaData']['description']))
{
echo $this->Html->meta('description', $data['PageMetaData']['description'],array('inline'=>false));
}
else if(!empty($data['Brand']['description']))
{
echo $this->Html->meta('description', $data['Brand']['description'],array('inline'=>false));
}
And if a PageMetaData has been associated to the current Brand and has a value for title, it will set that as the page title, otherwise if the brand has a field called name it will us that. Same for description.
The problem is I don't want to have to diplicate this code in every view for every model that uses PageMetaData.
I cannot figure out where I can abstract the code to, to avoid duplication.
I cannot put it in a Behavior or a Helper because you cannot set the title from either. I cannot put it in a Component because it cannot access the data found from the model.
Is there somewhere I can put this code for reuse?
You can possibly use elements for this. have a look at the cookbook link:
http://book.cakephp.org/view/1081/Elements
Place the method in your AppModel. I assume the method accepts an id for it to return the appropriate data.
Place another method in your AppController's beforeRender method. Pass the id to this method; which in turn will call the method in AppModel; setting title_for_layout, meta_description and keywords.
You should also not echo out these values, but rather pass them to the view and output them there (or in the layout).
AppController and AppModels are application-wide; so any controller/model may access the methods.
I'm sure there's other methods; and this might not work as I haven't tested it.