Manage multiple tables relationed in CakePHP - php

So, i have three tables
Table: Users
_________________
| id | user |
-------------------
| 1 | Roy |
| 2 | Ben |
|________|________|
Table: Hability_lists // Where i set the list with all habilities available
___________________________________
| id | category | subcategory |
------------------------------------
| 1 | Programmer | PHP |
| 2 | Programmer | ASP |
|________|____________|_____________|
Table: Habilities // Where i set the habilities from all users
________________________________________
| id | user_id | hability_list_id |
-----------------------------------------
| 1 | 2 | 1 |
| 2 | 1 | 2 |
|________|____________|__________________|
By this, we can see that:
Roy are a ASP Programmer and Ben are a PHP Programmer
But, how to set relative models like this using CakePHP?
I know how by using two models but not using three models.
There is some way to do this? Or maybe a better way to do?
Thanks in advance.

When working with an MVC framework it's highly recommended to follow its conventions. So a few changes may be beneficial for you.
What you are looking for its the HABTM (Has And Belongs To Many) association between the "users" table and the "habilities" table *. I'm guessing, by the design of your table, that a user can have multiple habilities, otherwise you should check the hasMany association.
It should be something like this:
Table habilities:
select * from habilities;
+----+------------+----------------------+----------+
| id | category | subcategoy | created | modified |
+----+------------+------------+---------+----------+
| 1 | Programmer | ASP | | |
| 2 | Programmer | PHP | | |
| 3 | Musician | Classical | | |
+----+------------+------------+---------+----------+
Table users:
select * from users;
+----+-------+---------------------+---------------------+
| id | name | created | modified | **
+----+-------+---------------------+---------------------+
| 1 | Roy | 2012-08-15 02:52:18 | 2013-01-17 03:25:28 |
| 2 | Ben | 2012-11-10 03:36:12 | 2012-11-10 03:36:12 |
+----+-------+---------------------+---------------------+
Relational table for HABTM:
select * from habilities_users;
+----+-------------+-------------------+----------+
| id | hability_id | user_id | created | modified |
+----+-------------+---------+---------+----------+
| 1 | 1 | 2 | | |
| 2 | 2 | 1 | | |
+----+-------------+---------+---------+----------+
Look the reference columns in habilities_users, they're singular with a _id suffix to work with CakePHP.
Defining the models classes it's also important, since it's where you define all their associations.
app/Model/User.php
<?php
class User extends AppModel {
public $hasAndBelongsToMany = array('Hability');
}
app/Model/Hability.php
<?php
class Hability extends AppModel {
public $hasAndBelongsToMany = array('User');
}
The table habilities_users doesn't need a model file, its behaviours and properties are implicit in the declaration of its associated models.
* using those names it's also the CakePHP way. [link]
** adding "created" and "modified" in each table will store those events for each record automatically.

You'll want to use HasAndBelongsToMany (HABTM).
This allows you to have two models - User and Hability that are joined by a "tweener" table.

Related

How to model belongsToMany relation where pivot table does not contain id

I'm rewriting an old application and I'm using Eloquent to handle the db layer.
I have these two tables:
+----------------+
| core_users |
| |
| id name |
|----------------|
| 1 John Doe |
| 2 Jane Doe |
+----------------+
+---------------------+
| core_access |
| |
| id user_id access |
|---------------------|
| 1 1 users |
| |
| 2 1 stores |
| |
| 3 2 stores |
+---------------------+
This is a belongsToMany relation since a user can have many access roles and access roles can belong to many users. But the core_access table is not a true pivot table, which have proven cumbersome when I wan't to attach and detach access roles to users.
An alternative, that I hesitate from doing, is to alter the table structure with a true pivot table like this:
+------------------------+
| core_user_core_access |
| |
| id user_id access_id |
|------------------------|
| 1 1 1 |
| |
| 2 1 2 |
| |
| 3 2 2 |
+------------------------+
But rather is there a way to setup the current table structure with Eloquent Models with a belongsToMany approach?

Doctrine orm one to many with reference key also containing strings

I have an entity called books and another one users.
Each book can have many authors(users).
So when I add a book I select the users that are registered on the site.
But if the author of the book doesn't have an account on the site I want to type his name by hand.
+----+---------+-----------+--+
| id | book_id | author_id | |
+----+---------+-----------+--+
| 1 | 1 | 1 | |
| 2 | 1 | Mr.Paul | |
+----+---------+-----------+--+
See how the author_id is a string but also an reference key to an user?
Can I do this with doctrine or I will have to manage the inserts myself?
Edit:I have no idea what the right title should be
Edit2:A possible solution will be to make another table containing only authors that don't have an account on the website.
I would recommend following approach
You have a table books a table accounts and the following:
authors_books
+----+---------+-----------+--+
| id | book_id | author_id | |
+----+---------+-----------+--+
| 1 | 1 | 1 | |
| 2 | 1 | 2 | |
+----+---------+-----------+--+
authors
+----+---------+------------+--+
| id | name | account_id | |
+----+---------+------------+--+
| 1 | Mr. Paul | 1 | |
| 2 | Simon | (empty) | |
+----+---------+-------------+--+
the account_id can be empty

Laravel 5.2 Find a Model Based on a Many-to-Many relationship

I'm having an issue with writing an eloquent query for a search function in my project.
Here are my database tables/models:
artist
| id | name |
---------------------
| 1 | Van Gogh |
| 2 | Michelangelo |
art
| id | title |
---------------------
| 1 | David |
| 2 | Starry Night |
art_artist
| id | art_id | artist_id |
---------------------------
| 1 | 1 | 2 |
| 2 | 2 | 1 |
style
| id | title |
---------------------
| 1 | Sculpture |
| 2 | Painting |
art_style
| id | art_id | style_id |
--------------------------
| 1 | 1 | 1 |
| 2 | 2 | 2 |
One artist hasMany art pieces, one art piece hasMany styles, and also hasMany artists. I have verified that the relationships work correctly. I also have a simple form that returns ids as a GET request with the variables artist and style.
So, I want to do two things in my function: First, check if the variables in the request are set (I can already do this). Then, USE ELOQUENT to query the art model based on the results of the form.
Here's an example: A user searches for a Sculpture by Michelangelo. The function queries the art database for any piece that is a sculpture by michelangelo and returns it as an art model.
The trouble is, I have absolutely no idea how to query the database based on the id of a related model. Any ideas?
I finally figured out how to do the query. First, I have to tell Eloquent I want to retrieve the artist model along with the art, THEN I can use whereHas:
$art = Art::with('artists')->whereHas('artists',function( $query ){
$query->where('artists.id',1);
})->get();

Doctrine mapping many linked tables

I'm working with legacy project in PHP and few of tables in database looks like this:
Table Product:
+----+-------------+----------------+
|id | id_category | id_destination |
+----+-------------+----------------+
|1 | 3 | 1 |
|2 | 2 | 1 |
+----+-------------+----------------+
Table Category:
+-------------+----------------+
| id | category_name |
+-------------+----------------+
| 2 | services |
| 3 | transport |
+-------------+----------------+
Tables "services" and "transport"
+-------------+----------------+ +-------------+----------------+
| id | name | | id | kilometers |
+-------------+----------------+ +-------------+----------------+
| 1 | 'foo' | | 1 | 156 |
| 2 | 'bar' | | 2 | 12 |
+-------------+----------------+ +-------------+----------------+
And now where I want check product with ID 1 it will call:
$info = SELECT * FROM product WHERE ID = 1;
$table = SELECT category_name FROM category WHERE ID = $info['id_category'];
and then:
SELECT * FROM $table WHERE id = $info['id_destination'];
My question is how can I mapped this in Doctrine.
I'm working on Symfony 2.6
You want to look at adding Entity classes in Symfony to handle the data to/from your database; think of these as the database tables themselves.
Then use Doctrine Association Mapping; this handles lookup tables (like MySQL JOIN's) automatically, allowing you to simply get the $product and automatically have access to $product->getCategory() or $product->getServices().
There is an article on this on the Symfony website too which acts as a good introduction:
http://symfony.com/doc/current/book/doctrine.html#relationship-mapping-metadata
Should be enough to get you started.
On a side note, think of upgrading Symfony to v3 or, at least, v2.8, for long term support before you get too deep.

Displaying Data From Multiple MySQL Tables

I have 14 tables (one for every year) with product code, firm name and invoice numbers. Main structure of table is identical (product code, ID), but there can be some variables in names of firms.
Table2011
| ID | productcode | firm1 | firm2 | firm3 | etc |
| 1 | G-00001 | 2;5;40| 32;67 | | 150 |
| 2 | G-00005 | | 50 | | |
|etc | | | | | |
Table2010
| ID | productcode | firm1 | firm2 | firm3 |etc |
| 1 | G-00001 | 1;10 | | 55 | |
| 2 | G-00003 | | 2 | | |
| 3 | G-00005 | | 50 | 40 | |
| etc| | | | | |
Table2009
...
Column Firm1 do not usually equals to same firm as firm 1 in other table
I am using table editor to work with tables (adding columns to table, editing values…).
I would like to know if it is possible to achieve result like below. It is above my PHP skills.
Product G-00001 page
…
<UL>
<LI>Year 2011: 150etc; 67firm2; 40firm1; 32firm2; 5firm1; 2firm1</LI>
<LI>Year 2010: 55firm3; 10firm1; 1firm1</LI>
<LI>Year 2009: ...</LI>
...
</UL>
…
Lemme begin with book recommendation : SQL Antipatterns. You will need it, doesn't matter if you caused this mess or ar just assigned to fix it.
If i was in your place, first thing would do would be to fix the database structure. This is madness. You do not need a new table for each year and new column for each company. Database is not a form of Excel spreadsheet.
Invoices Years Companies
----------------- ------------- ---------------
| product_code PK | | year_id PK | | company_id PK |
| company_id FK | | number | | title |
| amount | ------------- ---------------
| year_id FK |
-----------------
Where PK - primary key and FK - foreign key.
This structure would make the gathering of information much much much MUCH easier.
If you just want to display the data and not worry about the restructuring just yet you can use a JOIN to display the information from all the tables.
Although I would agree with teresko you really need to redesign that database. It is not maintainable the way it is.

Categories