CakePHP naming conventions/file structure - php

I'm quite new to CakePHP so I'm wondering if anyone can help me with how to order my pages.
I have a table of products (with a Product model and products_controller).
I also have a table of categories (with a Category model and categories_controller).
The categories hasMany products.
Firstly, is the name categories incorrect to call it. According to CakePHP convention, what is the correct name to call it?
Secondly I would like the user to click on the products link and then be presented with a list of categories and finally, once he/she chooses a category be presented with the products in that category. How would this be laid out?

You're asking some pretty basic CakePHP stuff, I suggest you read the book, which outlines naming conventions, file structure and data retrieval to name a few things.
That being said, the name categories is correct, unless you want products to have more than one category, the relationship will be Product 'BelongsTo' Category.
To get category info inside the product controller you can just access it's find methods with $this->Product->Category->find();, but again I recommend you read through the CakePHP book as you go to build up our knowledge and learn more about the framework you're using.

You mean that categories is not a plural of category? I think so. Your table has to be named as 'categories'.
Secondly, I think that you need a Categories hasAndBelongsToMany Products (HABTM) in your model, so every Category has many Products, and also a Category belongs to many products.
Use the 'cake bake' command and you will see easily if it is what you want.
Hope it helped, althought I'm quite new in cakePHP as well...
Alf.

If you have categories tables in db, its controller would be categories_controller.php and the Products belongsTo Category will work if products belong to only one category. No need to HABTM relationship. See in cakephp the model files are in singular form and controller file are in plural form with controller attached with them. The tables are named in plural in db.
Regarding ur 2nd question, I think im not getting it exactly.

Related

Eloquent ORM | Trying to understand relations in Laravel 5.6

Stackoverflow.
I'm newbie to PHP and Laravel. I'm learning it by creating ecommerce shop system.
So the question is about Eloquent ORM and Relations in system.
I have Brands Catalog with Products that belongs to Brand. Than I want to create Catalog with Products, Types, Variants and Photos.
Project Scheme
On scheme below you can see the relations and DB migrations done now.
The Problem
What I have done:
Models relations;
Brand controller (simple CRUD with slug);
What I need to do:
Create adding Product logic (Only 1 ProductVariant).
When adding new Product, by default, it should create 1 ProductVariant with
datas (e.g. ASUS (Brand), ROG (Product), Notebook (Type), Zephyrus (Variant)).
Create adding Product logic (More than 1 ProductVariant).
When adding new Product, but Product has >1 ProductVariant(e.g. Intel (Brand), Core i7 (Product), CPU (Type) and Variants 7700, 7700K, 7600).
So, when I'm creating product (e.g. product/add.blade.php view).
With example form:
<form action="{{ route('name of add route) }} method="post">
here goes brand select with options (foreach brands as brand)
than goes type select with options (foreach types as type)
than goes inputs for Product (name, country, etc.)
than goes inputs for ProductVariant (name, description, price, etc.)
than goes inputs for another ProductVariant.
</form>
The Question
Am I done all relations correctly?
How should I store this data in DB in Controller?
Also, I have Controllers for Product, ProductVariant, ProductType.
I will be very grateful if someone can help me
It's too long to write whole code here or describe how to save relational data in laravel.
Please have a look official documentation and this small tutorial
Connected with correct relationships
Depending on your example I think that you need to have additional description fields for tables Product and ProductVariant, to store short info about product or concrete variant of product.
Also I think that will be a lot of product photos, which will be independent of product variant (will be the same for all variants of that product). To do not have duplicates of photos for each variant you can change ProductPhotos table structure:
id
product_id - belongs to Product
product_variant_id - belongs to ProductVariant (NULL if its for all variants of that product)
photo_url
So to get all photos for concrete variant of product you can get
all photos for that product which are not connected with variant
(product_variant_id IS NULL)
plus all photos for that variant

What table structure is better for categories and custom user categories

I have a categories and users table. A user can have many categories and a category can have many users (many to many). However, I also need a feature were users can insert/create their own categories and which is only accessible to the user (category creator) + the defaults categories.
I created a pivot table to handle the many to many relationship, however, I was having difficulty deciding if I need to create another table to handle the custom user categories or just add a user_id on the categories table.
What would be the correct structure I should take/create to handle this.
Thanks.
Given the information you have described, there are two solutions which would be valid: one would be to have a separate table for custom categories, and my preferred solution, would be to have a boolean value on the categories table which indicates whether a category is custom or not. This gives you the following advantages:
Logic applied to the two similar kinds of category remains the same
Other fields which are shared can be kept, in kind
If you wish to convert a custom category to a real category, this then becomes trivial (change the boolean)
You could include a creator id field to identify the person to whom the category applies, alternatively, you might simply designate in-code that custom categories may only have one member.

WordPress Taxonomy Structure For Laravel

I am developing a taxonomy system (tags, categories, and attributes) with Laravel. Currently in my database I have the following tables, "terms", "term_relationships" and "products". The "term_relationships" table being the pivot table. However, with multiple taxonomies (e.g. categories, tags and attributes), how can I store the taxonomy names without getting them confused between categories, tags or attributes. I see that WordPress uses a 4th table named "term_taxonomy", however I am unsure of how the 4 tables relate to each other.
Would someone be so kind as to enlighten me on this subject providing the best solution to implement such a taxonomy system.
Thank you.
If this can help, I created this package : https://github.com/codiiv/laravel-taxonomies and it's based on the 3 table structure in Wrordpress, but I combined them into one table rather!

article categories - many to many with one MAIN

My client needs to select many categories for his article, but one category should act as main category.
Would it be better to model relationship as many to many and add column flag "is_main" to article_category table or use one to many for main category?
I am also trying to find some jquery widget to easilly implement function like this.
I thought about using jquery ui dragable to let him move categories between 3 divs: article main category, article regular categories and ALL CATEGORIES available in cms.
ALternatively I could use select for main category and checkboxes for the rest?
Maybe there is some ready solution?

CakePHP how to use static values (categories)

I would like to have categories, and rankings for my content and users respectively, and I am not sure how to go about implementing this using CakePHP conventions?
Do I need to create a model?
That depends entirely on what these categories are supposed to do and not do. You could simply define a number of constants that you use for categorizing stuff. But, are categories...
subject to change? Do you want to add more eventually?
editable? May you want to change their names?
nested?
supposed to have more attributes than just their id? Names, descriptions?
If you answered Yes to any of the above, you'll want to store them as data in the database. This is independent of Cake, it's just sane data modeling. For Cake that means you'll need to create a model. The same goes for ratings.
So you'll have these tables:
users
hasMany ratings
categories
hasMany contents
contents
belongsTo categories
hasMany ratings
ratings
belongsTo users (polymorphic)
belongsTo contents (polymorphic)
You may want to separate user ratings and content ratings into two tables instead of using a combined polymorphic table (which just means that you have an extra column keeping track of whether a rating is for a user or for content).
i guess you are looking for something like this IF you dont want to use a model:
http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
one possible approach to use "enums" for things that maybe only have 1-5 states.
if you have more than 10 or you want to be able to dynamically modify them (label, active/inactive) you will need a separate table and model relation.

Categories