Having just started with Cake, I'm now very frustrated having already spent several hours reading examples etc What I want to do seems so simple, but I can't make enough sense of the documentation to find out how to do this. It's very simple:
I have 2 tables:
items
categories (eg books, cd)
New items will be created constantly, each has a category.
All I want to do is create a dropdown filter in the items list view. But to do this I need to load the list of categories.
So the basic problem is, how do I access the categories table from the items table?
Finally a solution.
In the items controller:
$this->loadModel('Categories');
$categories = $this->Categories->find()->all();
Or to remove all the cake crud from the returned object:
$categories = $this->Categories->find("list", array("fields"=>array("Categories.title")))->all();
Related
I am using Django to build data models including a model Company. Some of the fields belonging to this model are limited to set choices using the choices='' argument. Some of these fields have a large number of choices, for example a country CharField which lists all countries. Finding the right value among the long list can be tedious so I want to be able to search across the given choice values. This is easy to do for ForeignKey/ManytoMany fields using autocomplete_fields = [] as seen in the attached screenshot (from a different model) but can't seem to find a method for implementing this with a normal CharField with lots of choices. This seems like something that should be built into the Django for the admin panel but I can't find anything within the docs. Please how can I implement a search/filter dropdown for any given (non FK/m2m) fields? Thanks in advance. If there's anymore information I can provide let me know and I will.
country = models.CharField(max_length=128, choices=COUNTRY_CHOICES, null=True)
Model code added. This COUNTRY_CHOICES array is what I would like to be able to select from in a searchable dropdown list.
I have a tableView that has the price and quantity of an item in the cell, and I want to add these together so if there is multiple items - it will then say the total.
This is in my cellForRowAt
detailCell.priceCostLabel.text = sortedPriceDictionary[indexPath.row]
detailCell.quantityLabel.text = String(sortedQuantityDictionary[indexPath.row])
let totalPrice = (Double(sortedPriceDictionary[indexPath.row])! * Double(sortedQuantityDictionary[indexPath.row]))
print("The total price is \(totalPrice)")
This is working as far as it is printing the correct price for each product and how many of each there are, but if there are 2 or 3 cells, it is printing (for example)
The total price is 3.9
The total price is 20.7
Would I create an array of totalPrice(s) and do arrayOfPrices[0] + arrayOfPrices[1]? Then again how would I know how many cells I need to add together? Thanks in advance.
What you are trying to do is a bit unusual. In the world of Model-View-Controller, I would expect that the list of prices that you are trying to add up would be the Model. If you were to do any calculations on those prices then you would do it as part of the model. The model would know how many prices it has stored and would know how to add them up.
The TableView would just be a means of putting the values from the model on the screen. You would create as many tableViewCells as your model demands and all the "View" part of the MVC triad would do is show them, you wouldn't actually perform calculations on the values in those cells, they're just for display.
I recommend that you read more about the MVC pattern and gain a better understanding of the role of a TableView. You can start with Apple's description:
https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
When you click on that link it will tell you that that documentation page is deprecated. That's OK. Read it anyway. A Google search of MVC or Model View Controller will provide a rich collection links to help you understand the pattern, and hopefully your problem and solution.
Below is the screenshot of the web pages, I need to write a script that add the product automatically, suppose 15 times.
I achieved this for one single entry but i need to write it in a loop.
Challenge here is that i need to select different categories based on which there are different sub-categories and based on sub-categories there are different sub-sub-category so can anyone suggest how to achieve this.
If the fields and dropdown itself don't change you can create some kind of collection for each of the category options and sub options. Then with some mapping from one category to other you can achieve this.
Eg. CategoryArray will have [Item1, Item2, Item2] selection of this choice will guide which subcategory array you will access like Item1SubCategoryArray then so on and so forth.
I'm still kind of new to Backbone, and I'm pretty stumped by this...
I have a router/collection/view for Titles, and a router/collection/view for Campaigns.
When a user creates a new campaign, it must be associated with a title. So in the create form, I need to access the list of all titles so I can get their names and (rails) IDs.
I'm not sure how to access this title list from within my Campaigns view though.
A few thoughts on solutions:
Using an association library like ligature -- this seems like a lot of work for a fairly straightforward problem, and I don't really need associations.. I just need the full list of titles.
Declaring my Titles collection in the global scope and passing it to the initializer of the Titles router.
Create a Titles collection in the Campaigns router.. this seems pretty redundant.
Happy to post code, but I don't know what is relavent.
If by global scope you mean window, I'd recommend avoiding that if possible. Do your Titles / Campaign classes have access to a shared scope? E.g.:
( function () {
var App = {};
// ...
// Titles code
var titles = App.titles = new Backbone.Collection;
// ...
// Campaign code
var titles = App.titles;
} )();
You might want to arrange things so the various parts of your app have access to a shared scope like that if you want to have a single Titles collection used throughout the app.
Otherwise if that's impractical or undesirable for some reason, you could just create a separate Titles collection in your Campaigns section and fetch it from the server, or bootstrap it, or however you want to populate it.
It's hard to be more specific without seeing some code, so maybe this will suggest some code that you think would be relevant to post.
I have two models:
Book
Category
I have two views:
books/browse
In this view there is paginated list of books, with sort options, book language select, author/title filters and so on. One thing there isn't - book category tree. In fact this controller takes as a parameter category_id and displays only books from this category.
categories/tree
This view (controller) finds the category tree and it takes one parameter -> category_id -> to determine how to print the tree (to show all parent nodes, and parents list and so on).
Now I need a view (for example browsetree) where there is category tree on the left and a list of books (with all stuff) on the right.
What will be best solution to accomplish this? (The problem is also I want to be able to save the link to book browse view with all it's params (category_id param + sorting + few GET params for filtering) and I also don't want to rewrite any view or controller method...
-------------- edit
Okay I have my tree in element
categorytree
and it looks like this:
$params = $this->requestAction('/categories/tree/'.$category_id);
print_category($params['tree'][0],$params['parents'],$category_id);
The $category_id is passed to the element as parameter.
This is inside /books/browse:
<?php echo $this->element('categorytree', array("category_id" => $category_id)); ?>
What I need is that in categorytree element I want that each tree node was a valid link to /books/browse based on current url that books/browse is loaded from.
I mean that the url might be:
books/browse/34/sort:asc/page:4?author=Author&something=something
I could get this link
inside /books/browse/...
view and pass it to the element and then parse it to replace /34/ (it's category id) with other categories... But is there any easier / more elegant way of doing this?
Put your category tree in an element and use CakePHP's requestAction to get it's data from the Category controller.
You can keep your tree view in the Books area (which makes sense, since that's what the user is browsing) - up to you which view you put it in.
Bottom line, the user is searching books, so that's where the view and the code should be. Use the Category tree to just pass a variable for which category you want to use in your query against the books.