article categories - many to many with one MAIN - php

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?

Related

How to fetch only active items from a Categories->Sources->News structure in Laravel?

This is a rather simple requirement, but I'm not sure how to do it the Laravel way. I have a simple structure of categories, item sources and items and I need to fetch only the active items.
Categories, eg. Tech, Science, Entertainment
Sources (each source belongs to one category), eg. Engadget, Popular Science, Billboard
News items from each source.
All of the above items can be set to active or inactive. If 'Tech' is set to inactive, I will only get news items from the other category sources, eg. Popular Science and Billboard.
A custom query will do this, but I want to know if there is a more eloquent [sic] way of doing it.
Category model can have function that it has many Sources, let call it sources().
Sources model should function that it has many News: function news().
Then you can do something like that:
$news=App\Category::find($categoryID)->sources()->get()->news()->where('active',1)->get();
And if you want to get news only from active sources:
$news=App\Category::find($categoryID)->sources()->where('active',1)->get()->news()->where('active',1)->get();
Also you can make custom function in Category model like:
public function activeSources(){
return App\Source::where('category_id',$this->id)->where('active',1)->get();
}
And then get news like that:
$news=App\Category::find($categoryID)->activeSources()->news()->get();

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.

Dependent Drop down in Grocery CRUD (CodeIgniter)

I stucked in a Problem When Playing with Drop Downs.I tried to use this
http://www.grocerycrud.com/forums/topic/1087-updated-24112012-dependent-dropdown-library/
But actually my requirement is quite different. I have a table fwld_products in which i am adding all other table's categories, from
fwld_cat_main (main Category's ID),
fwld_cat_sub1 (sub1 Category's id)
fwld_cat_sub2 (sub2 Category's id)
fwld_cat_sub3 (sub3 Category's id)
I want to Display Dropdown in such a way, when user Selects main
Category, the Drop Down Appear (sub1) Having Data related to main
category and when sub1 selected drop down appear (sub2) showing data
related to sub1, and sub2 selected and drop down appear(sub3) to show
data related to Drop down (sub2).
When submitted Finnally data inserted to [fwld_products].
Here I am attaching ERD, and result as well.
Please help
Hold on, it seems that DB structure of your categories table needs to be improved. What I suggest is that you follow footprints from some of the popular CMS like Opencart. It will give you a great sense to accomplish your task. You can easily optimize your DB by using just one "category" table (instead of main, sub1, sub2, and sub3 category tables) like this:
For category names, description, and meta keywords etc you can create this table "category_description":
Finally to assign categories to the products you can simply create another table "product_to_category":
In this way you can easily manage your data in DB and you can now easily tackle your situation using Codeigniter and Grocerycrud.
Try, Chained Selects Plugin for jQuery and Zepto (Github Project | Project Home)
If you use jquery or zepto in your project, this plugin will help you to solve your problem, Specially the remote version. You can create related select boxes easily.
Hope this helps :)

show category and subcategories in a tree menu using codeigniter

I want to show categories and subcategories like a tree menu:
Category
Subcategory
Subcategory 2
Category 2
Subcategory
Subcategory 2
my database contains 3 colomns:
categoryId, categoryName, categoryParentId
I really don't have an idea how to do this?! Can somebody help me?! I found some codes on the net, but I couldn't make them work
The best way to make such a structure would be to implement either an Adjacency List model or Nested Set model. You can read more about them in this excellent article by Mike Hillyer.
Of course, you can always come up with a home-made solution, but in my experience, they often don't come up as good, effective and flexible as the two mentioned above.

CakePHP naming conventions/file structure

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.

Categories