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.
Related
I am doing a project in PHP, MySQL. I need to create a database in which there is a category to which there can be many subcategories ( like for a course book I can have physics, computer science, mathematics etc.).
Also there can be some categories which do not have subcategories (say Mythological Books).
The administrator has to add new category or subcategory via a form in PHP page which then updates the database.
How should I design the structure of my table where if admin for following two cases to be incorporated:
Admin only wants a subcategory to be added then upon selecting its parent category, it should be done.
Admin wants to add a new parent category and there exists no subcategory for it.
Please suggest how should I create my table(s).
Create a table called 'categories' with columns 'id', 'name', 'parent'.
Then use it like this:
1, Hardware, null
2, Storage, 1
3, Video, 1
4, Harddisks, 2
...
Here is a simple example with books:
Categories table:
+----+--------------------+--------+
| id | name | parent |
+----+--------------------+--------+
| 1 | Comics | NULL |
| 2 | Programming | NULL |
| 3 | SQL/PHP | 2 |
| 4 | Java | 2 |
| 5 | Marvel | 1 |
| 6 | Mythological Books | NULL |
+----+--------------------+--------+
Books table:
+----+---------------------------------------+----------+
| id | name | category |
+----+---------------------------------------+----------+
| 1 | PHP/MYSQL for dummies | 3 |
| 2 | Iron Man and the prisoners of azkaban | 5 |
+----+---------------------------------------+----------+
If you want to show all the books in the Programming>SQL/PHP category you just simply get them with a select query.
mysql> select * from books where category = 3;
+----+-----------------------+----------+
| id | name | category |
+----+-----------------------+----------+
| 1 | PHP/MYSQL for dummies | 3 |
+----+-----------------------+----------+
1 row in set (0,00 sec)
I'm thinking over to design an API to filter objects using fos_rest bundle in Symfony2 and Doctrine with MySQL.
Let's say I have a Master Entity, which has relations with different entities which have some properties.
Now in frontend I would like to create a filter, where one can filter the Master Entity by the properties of the related Entities. How would that be doable?
say we have
+---------------+
| Master Entity |
+----+----------+
| id | name |
+----+----------+
| 1 | Apple |
+----+----------+
| 3 | Berry |
+----+----------+
+-------------------+
| Property Entity |
+----+------+-------+
| id | id_m | value |
+----+------+-------+
| 1 | 1 | green |
+----+------+-------+
| 2 | 1 | yello |
+----+------+-------+
| 3 | 1 | red |
+----+------+-------+
| 4 | 3 | pink |
+----+------+-------+
And I want to have a filter, where I filter by the values in Property Entity
I would like to do something like
$em->getRepository('AcmeBundle\MasterEntity')->findBy(array("PropertyEntity:value" => "red","PropertyEntity:value" => "yello"))
so it would return the object collection of Master Entity with ID=1 (apple) - because both parameters would match Apple
You can create a query builder instead of use findBy
$qb->select('m')
->from('AcmeBundle\MasterEntity', 'm')
->join('m.PropertyEntity', 'p')
->where('p.value IN (:values)')
->setParameter('values',['red','yellow']);
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();
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.
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.