Kohana ORM relationships and filtering - kohana-orm

I have a Provider model which has 4 has_one relationships with Category, Country, State and City.
I use find_all to ge a list of all the providers, but need to be able to filter them by category, country, state and/or city by their name.
How would I, for example, get a list of the providers in US? The providers table has a foreign key to the countries table and that table has the name of the countries.
Thanks.

filtering by country
$country = ORM::factory('Country',$country_id);
$providers_by_country = $country->providers->find_all()->as_array();
filtering by category
$category = ORM::factory('Category',category_id);
$providers_by_category = $category->providers->find_all()->as_array();
Then, you can merge both arrays to get a full list of providers filtered by country and category

Related

SQL query which returns COUNT value of matched columns of two different tables

I want to output the number of properties registered in db of same cities under that city. Here is my tables:
What I want is to select number of properties with same city, Mysql command with PHP output all the 3 properties if I run for a specific city.
SELECT COUNT(property_id)
FROM properties
JOIN cities
WHERE cities.city_id = properties.city_id
city_id is a foreign key in properties table, How can I show total properties under same cities?

Laravel query nested relations

I have the following model schema
Products (can have many stores on table product_stores; can have many chains on table product_chains)
Stores (have one chain on column chain_id; can have many products on table product_stores; have a spatial location column)
Chains (can have many stores; can have many products on table product_chains)
I need to retrieve the products that match a certain expression (I am using MATCH (name) AGAINST), and I need to order them by the distance to the user's location. The distance to the user's location should be the minimum distance to any of the product stores, that are associated to the product through on either of the tables (product_stores, product_chains).
I achieved this for the stores that are associated to the product through the stores table, but I'm still not able to do it for the stores associated through Chains (nested).
For the direct stores I'm doing it like this:
$productList = $productList->withCount(['stores as distance' => function($query) use ($centerPoint) {
$query->select(DB::raw("coalesce(MIN( GLength( LineStringFromWKB( LineString( location, GeomFromText('" . $centerPoint . "') ) ) ) ) )"));
}])->having('distance', '<', $distance)->orderBy('distance', 'asc');
For the chains' stores I haven't reached a solution. Any ideas? I'm also wondering about ways to improve the performance on this query. Thank you in advance!

companies er model for different countries

I want to build a ER-model for companies.
Some fields for companies are the same, like the name, legal_address and others, but some fields are defined based on the country, where the company is registered.
For example if we take Russia, the company should have fields like inn, ogrn, etc.. (don't matter the names).
If we will take another country - the company will have another set of fields.
I need an advice of how to plan (architect) this.
Any ideas would be great.
At the moment I have this done:
[locations]
id
country
city
lat
lng
zip_code
[companies]
id
location_id
name
director
legal_address
actual_address
[company_russia]
id
company_id
inn
kpp
ogrn
stat_codes
reg_number_fss
reg_number_pfr
But I think there will be problems joining the tables and that each other country requires a separate table - is not a good approach.

Database design for categories, subcategories and keywords filtered data

I am creating an application that lists local businesses and groups them by category, subcategory and keyword. Here are the rules for how businesses are sorted:
A business can belong to multiple categories and subcategories
A business can have multiple keywords
Not every category has subcategories, but those that do are only two levels deep (category -> subcategory)
A subcategory can belong to multiple categories
Also worth mentioning is that the client has given me the list of businesses in an excel document which is laid out as such:
Each category is a separate tab, or page
Each page contains the list of businesses, their addresses, keywords and subcategories
The business' subcategories are designated by an 'X' in a column with the subcategory's name
I have been attempting to solve this through pivot tables, but this has lead me to quite a few pivots (category_subcategory, business_keyword, business_category) and I’m afraid that this will have a substantial impact on performance do to the volume of queries that would be made to determine which businesses to show based on a filter.
I think I am on the right track, but I feel that there is a more optimal way to approach this.
If I read all that correctly, these are the tables I think you should make:
businesses
id, name, etc.
keywords
id, word, etc.
categories
id, name, etc.
subcategories
id, name, etc.
categories_subcategories
id, category_id, subcategory_id
businesses_categories
id, business_id, category_id
businesses_subcategories
id, business_id, subcategory_id
businesses_keywords
id, business_id, keyword_id
Proper way would be to have something like this:
1.Business(id (*PK), name,...)
2.Keyword(id (*PK), keyword,...)
3.Category(id(*PK), name, parent_category (*FK),...)
4.Bis_cat(bis_id (*FK), cat_id (*FK),...)
5.Bis_key(bis_id (*FK), keyword_id(*FK),...)
Where (*PK) are primary keys, and (*FK) a foreign keys.
In this model you would have unlimited subcategories, using recursion. You can also add referential integrities if you want to be 100% sure that you want have any errors but it's a very tight restriction on a db, so if you know how to do it put it in.

get a list of cities from location model and how many locations exist for each city

I have a location model which has a few columns: title, address, city, state, etc...
I am trying to show a list of all the cities based on the locations in my database. Then, I am trying to figure out per city how many locations there are.
I want to be able to output something like
Tampa(10)
Denver(20)
Atlanta(15)
... and so on
Currently to get a list of cities in the database I am using the following code:
cities = Location.uniq.pluck(:city)
Technically I could loop through all the cities and do a COUNT(*) WHERE city = "cityname"... but that seems like a very inefficient way to do this.
Is there a better way to accomplish this?
Location.group(:city).count
This will give you a hash with city as the key and value as the count
you should use a GROUP BY insted
SELECT city, COUNT(*) FROM locations GROUP BY city;