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

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?

Related

Delete values from second column alongside the values from first column in the same row between two database tables

I am trying to delete the same id between two tables. And deleting a storage file by getting the image file value.
For example procedure:
user_id(table1)->user_id(table2)->avatar->file name-> delete images-> delete all data from same user_id between first table and second table
I have two tables,
*user* table
| user_id | Full_name |
| -------- | -------------- |
| 1 | Steve Jobs |
| 2 | Bill Gates |
| 3 | Elon Musk |
*users_option* table (with both column foreign keys)
| user_id | user_option | user_value |
| ------------ | -------------- | -------------- |
| 1 | email | abc#abc.com |
| 1 | avatar | 'big.jpg','sml.png' |
| 1 | username | stevejobs |
| 2 | email | def#def.com |
| 2 | avatar | 'big.png','sml.jpeg' |
| 2 | username | billgates |
I am hardly writing this code with ORM MySQL
ORM::table($config['db']['pre'].'user')
ORM::table($config['db']['pre'].'user_option')
Simply that, I just want completely delete all the data from the same user_id.
I just want completely delete all the data from the same user_id.

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();

Manage multiple tables relationed in CakePHP

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.

What is the best table Structure for an ORM

I have 4 tables related like so
+-----------+ +------------+ +---------+ +----------+
| Project | | Slide | | Shape | | Points |
+-----------+ +------------+ +---------+ +----------+
| id | | id | | id | | id |
+-----------+ | project_id | |slide_id | | shape_id |
+------------+ +---------+ | x |
| y |
+----------+
From the ORM docs I have been reading for the Active record object built into CodeIgniter is it best to keep the tables structured like so or change them to one of the following ways.
First would be to use a total relational table like this
+-----------+ +------------+ +---------+ +----------+ +-------------------------------+
| Projects | | Slides | | Shapes | | Points | | Projects_Slides_Shapes_Points |
+-----------+ +------------+ +---------+ +----------+ +-------------------------------+
| id | | id | | id | | id | | id |
+-----------+ +------------+ +---------+ | x | | Project_id |
| y | | Slide_id |
+----------+ | Shape_id |
| Point_id )
+-------------------------------+
That way everything is related by one table or should I relate things with seperate tables so instead of the above it the relation tables would look like so.
+-----------------+ +---------------+ +---------------+
| Projects_Slides | | Slides_Shapes | | Shapes_Points |
+-----------------+ +---------------+ +---------------+
| id | | id | | id |
| Project_id | | Slide_id | | Shape_id |
| Slide_id | | Shape_id | | Point_id |
+-----------------+ +---------------+ +---------------+
The first way will have a smaller number of entries but less queries to build the ORM object on the other hand the other has less entries and more queries. I really don't know which is best or which an ORM prefers. Or if an ORM can handle one or the other.
Or for an ORM can I keep them like they are and I am misunderstanding the docs.
Thanks for the advice.
You should first determine what kind of relationships you're going to have between the four different object you have defined.
For example, there is no need to create a "Projects_Slides" table unless you have a many-to-many relationship between "Project" and "Slides". As it is, I assume that you would like each Project to have many slides, and each slide to be associated with only one Project? In this case, you have a one-to-many relationship and the first schema would be best.
If you were to define that a Slide could belong to more than one Project, then that would be a many-to-many relationship and the "Projects_Slides" table would make sense.
Did you intend for the Shapes table to be self referential (it has id and shape_id columns)?
Generally, ORM's are happiest when you properly normalize your database. I'd say your first example is the most properly normalized, if you're trying to do what I think you are.

Categories