Hey I have two models User and City, where a user belongs to a city and a city has many users.
On the city table I have city_id and city_name.
I've used used cake bake and created the user model and when I add a new user the combos box under the city field is showing the id of the city instead of the name.
I've tried several methods to show the name of city instead of the id but I've failed.
You don't mention what you've tried, but usually you'd use the model's displayName property to set the field that should be used a record's "title". In your case, I'd think:
$this->displayName = 'city_name';
Note that in 1.3, it looks like the attribute name is displayField rather than displayName.
Cake requires a specific table layout for the automation to work. Rob explained how to get around it. But if you want to avoid writing the extra lines of code, you should consider organizing your tables accordingly.
CREATE TABLE `cities` (
`id` int(11) AUTO_INCREMENT NOT NULL,
`name` varchar(25) NOT NULL,
PRIMARY KEY (`id`)
)
When you bake this model, Cake will automatically use name as the display name. This will apply to every table you create. In addition, if you use id for the table, it will automatically know how to reference it as a foreign key from other tables as city_id.
Related
I have a table "products" for my website. There is the product_id, image and bunch of information like manufacturer, health, age etc. When I started to work on my projekts I chose to save everything as ENUMs like this:
manufacturer ENUM('manufacturer1', 'manufacturer2', 'manufacturer3', 'manufacturer4'),
However dealing with more stuff I have realized that this is not the best approach for me. So I decided to create reference tables. For manufacturer it looks like this:
CREATE TABLE manufacturer(
manufacturer_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (manufacturer_id ),
UNIQUE INDEX name (name)
);
but in products table I now have:
manufacturer_id INT(10),
The question is on what is the best way to assign value from reference table. I am using CI framework and it was very easy to display product in view. However now when I run query for product I get "3" instead of "manufacturer3".
I could use JOIN but I would have 7 JOINs. I created library with function that ataches the value. But I have to save all the possible values in that function or to query every reference table every time I use library.
I mean there are a lot of articles about why not to use ENUMs and I get that - I run in problems with ENUM in my project myself. I dont get how to convert those reference ids to real value (efectivelly, I dont think that 7 JOINs is a good way to that especially if I have to do that very often)
I am using Eloquent on my project and I am currently facing a problem with regards to polymorphic relations. What I would like to do is to specify another column instead of the primary key in morphing relationship such that:
Products
id
product_number
description
User
id
username
Photos
id
path
imageable_id
imageable_type
In this scenario, I would like to create a relation with both the products and the users table, but in the product relationship, instead of putting the ID as the relationship identifier, I would like to put the product_number because of some reasons. Is it possible as of version 5.1?
Thanks,
Jan
It is possible by specifying the local key like so:
return $this->morphMany('App\Photos', 'imageable', null, null, 'product_number' );
I have a categories table like this for my forum:
CREATE TABLE categories (
category_id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(60) NOT NULL,
PRIMARY KEY (category_id),
UNIQUE (name)
) ENGINE = MYISAM;
As you can see, I set up the name column as UNIQUE because I intended to allow users to freely create new categories as they wish when they create new posts.
Now, I am creating the edit.php page to enable users to edit their own posts, in which I allow them to change the category of the posts if they wish.
Assume that everything goes smoothly with my UPDATE query for the edit.php page.
However, The problem arised here is that when the user accesses the edit.php page to update the his own post's category, he immediately changes his mind that he does not want to change it anymore. The he clicks the submit button. The error occurs saying Duplicate entry 'x' for key y. I guessed that the root cause was at the unique(name) in the table.
So, should I remove the UNIQUE type for the name column and sacrify the feature of allowing users to create new categories freely?
Or, If I can keep it, your help for a piece of code will be appreciated!
Thanks
This is not a database problem which you can solve with SQL. This is an application problem. If no category must be changed or added, the application should not try to change or add categories. Removing the UNIQUE attribute would probably allow the application to do an unwanted change, which doesn't seem to me a great solution.
Anyway, I suggest you use InnoDB. MyISAM does not provide any benefit for this kind of tables.
Add duplicate validation before update the category this is mandatory
For this case append another condition name != '$_POST["name"]' in the update query.
UPDATE categories SET name = '$_POST["name"]'
WHERE category_id = '$_POST["category_id"]'
AND name != '$_POST["name"]'
what is the way of designing an optimized schema's for a shopping website that can provide different filter options for different categories.
In TV Category the filters available are Type, Display Features, Display Size. Whereas in computer accessories like keyboard the filters available are Type and Interface. and the filters change for various products.
Filtering the result by cost and brand can be done easily as they have separate column in the table.
but how to design a table of items which have different types of multiple filters which varies by category. My guess would be having a column named filter for every item. that holds these data's. But is it the best way of using filters as the column filter have multiple filters.
Having one column surely is not the best way if you have a lot of data.
As I suppose, you can have some table tbl_product_filters with M:1 relation to your tbl_products that contains filter names and values for each product id.
Something like:
CREATE TABLE `tbl_product_filters`
(
`id` int(10) unsigned auto_increment PRIMARY KEY,
`product_id` ...,
`name` varchar(25) NOT NULL,
`value` varchar(255)
)..;
You can also normalize it further and isolate filter names as filter types in separate table to substitute string name to integer type_id. It would be faster I suppose.
I was just wondering if I might get some advice on the following:
I'm building a site in CodeIgniter in which exists a content type "portfolio_item".
The same portfolio item may be displayed in 3 places via checkbox controls:
Homepage, Member Page and Client Page.
I'm just wondering what the best way to implement this relationship in the database is. I could have 3 relationship tables for each of the above scenarios, but to me this seems overkill (the site is quite small).
I was thinking of using one relationship table with the following fields:
type (homepage, client or member)
show_on_id
portfolio_id
The intent of the type field is to determine which table to retrieve the "show_on_id" from (either clients or member), if the type is homepage then show_on_id is not required.
Is there any obvious disadvantage of doing it this way?
Yes, there is a bit of a disadvantage. You could end up with multiple rows for the same setting, each might contradict each other. So, how you insert rows into this table, is very important.
If you will not add any more sections, might as well have the portfolio table:
CREATE TABLE `portfolio`
(`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`content` TEXT NULL,
`showHome` BOOLEAN NULL,
`showClient` BOOLEAN NULL,
`showMember` BOOLEAN NULL)
And then the table which links the users to their portfolios,
CREATE TABLE `portfolio_user`
(`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`portfolio` INT NOT NULL,
`user` INT NOT NULL)
If you are going to add more places where the portfolio can be displayed later, or if these places are dynamic, your method will work. I would just change 'type' to 'place' as that is easier to understand, then either use ENUM or another table to define the places that the portfolio can be shown.
Can there be one or many portfolio items for each location?
If many, use link tables, if only one use direct foreign key field to link to the portfolio.
I would advice against using one link table for all three based on personal experience with exactly such designs and the problems that can surface later.