I have a problem that I couldn't deal with it. I have a News table includes TitleID, TextID, ImageID. And three more tables Titles, Texts, Images. I want to get all of them in one model. But when I try, got result like array in array . But I want it like:
[ News: [ { ID, Title, Text, Image } ] ]
Eloquent ORM responds it like:
[ News: [ { ID, Title: [ID, Title], Text: [ID, Text], Image: [ID, Image] } ] ]
Database Structure
News =>
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| TitleID | int(11) | NO | | NULL | |
| TextID | int(11) | NO | | NULL | |
| ImageID | int(11) | NO | | NULL | |
| CatID | int(11) | NO | | NULL | |
| OlusturmaZamani | datetime | NO | | NULL | |
| YayinZamani | datetime | NO | | NULL | |
| DuzenlemeZamani | datetime | YES | | NULL | |
| Onay | tinyint(11) | NO | | NULL | |
| Hit | int(11) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
Titles =>
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| TitleText | text | NO | | NULL | |
+------------+---------+------+-----+---------+----------------+
the other tables like title table.
Is there a reason you've extracted Title and Text to their own table?
A solution for you would be something like what you see in this post:
Add a custom attribute to a Laravel / Eloquent model on load?
Tweaked for your example:
Assume you changed the relationShip to "titleTable", just to avoid collision
class News extends Eloquent {
protected $appends = array('title');
public function getTitleAttribute()
{
return $this->titleTable->TitleText;
}
}
Try eager loading.
If you have created relationships among News, Titles, Text and Image tables, you can load the relationship models by eager loading.
$news = News::with('title','text','image)->find(1);
Sounds like that ORM "over-normalizes". There is no reasonable reason for news attributes such as title, text, and image to be put in separate tables JOINed by an "id".
"Eager loading" sounds like a kludge to make it feel like the columns are in the News table when they aren't. This leads to unnecessary inefficiencies.
Related
I have the following Psql table structure.
Table "public.kart_user":
Column | Type | Modifiers | Storage | Stats target | Description
------------+------------------------+-----------+----------+--------------+-------------
token | character varying(35) | not null | extended | |
cycle | character varying(200) | | extended | |
userid | character varying(200) | | extended | |
proxyid | character varying(200) | | extended | |
salesrepid | character varying(200) | | extended | |
buyer | character varying(200) | | extended | |
salesrep | character varying(200) | | extended | |
discount | integer[] | | extended | |
custid | character varying(200) | | extended | |
category | character varying(200) | | extended | |
users | character varying(500) | | extended | |
validto | date | | plain | |
ratioonly | boolean | | plain | |
proxy | character varying(500) | | extended | |
validfrom | date | | plain | |
notified | boolean | | plain | |
Indexes:
"kartuser_pkey" PRIMARY KEY, btree (token)
Referenced by:
TABLE "kart_space" CONSTRAINT "kart_space_token_fkey" FOREIGN KEY (token) REFERENCES kart_user(token)
TABLE "kart_order" CONSTRAINT "kartorder_token_fkey" FOREIGN KEY (token) REFERENCES kart_user(token)
I'd like to change the column token from an varchar to a uuid without dropping the foreign key
I have tried the following, but failed.
ALTER TABLE kart_user alter token type uuid using token::uuid;
Note: I don't want to drop the relation ship in any case
How can i fix this issue? Thank you for your response
I'm using grails 1.2.2 and 1.3.7 in my project.
I want to add an index to the domain's table.
class Test {
String name
String surname
static mapping = {
name column: 'name', index: 'test_dx'
surname column: 'surname', index: 'test_dx'
}
}
I tried with the two version of grails and with dbCreate = "create" or dbCreate = "create-drop" or dbCreate = "update" (is the one I want to use) but no "custom index" created.
Your example works for me. If I run my app in update mode with a MySQL database, then I get this from the MySQL console:
mysql> show index from test;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| test | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | |
| test | 1 | test_dx | 1 | name | A | 0 | NULL | NULL | | BTREE | |
| test | 1 | test_dx | 2 | surname | A | 0 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.00 sec)
What database are you using?
Product
+----------------+----------------+--------+-------+----------+----------------+
|Field | Type | Null | Key | Default | Extra |
+----------------+----------------+--------+-------+----------+----------------+
|id | int(11) | No | PRI | Null | auto_increment |
|modelNumber | varchar(150) | No | | | |
|manufacturer_id | int(11) | No | | | |
|name | varchar(100) | No | | | |
|department_id | int(11) | No | | | |
|class_id | int(11) | No | | | |
|subclass_id | int(11) | No | | | |
+----------------+----------------+--------+-------+----------+----------------+
Department_id
+---------------+----------------+--------+-------+----------+-------------+
|Field | Type | Null | Key | Default | Extra |
+---------------+----------------+--------+-------+----------+-------------+
|department_id | int(11) | | PRI | | |
|name | varchar(100) | | | | |
+---------------+----------------+--------+-------+----------+-------------+
Manufacturer
+----------------+----------------+--------+-------+----------+---------------+
|Field | Type | Null | Key | Default | Extra |
+----------------+----------------+--------+-------+----------+---------------+
|manufacturer_id | int(11) | | PRI | | |
|name | varchar(100) | | | | |
+----------------+----------------+--------+-------+----------+---------------+
Class
+-----------+----------------+--------+-------+----------+----------+
|Field | Type | Null | Key | Default | Extra |
+-----------+----------------+--------+-------+----------+----------+
|class_id | int(11) | | PRI | | |
|name | varchar(100) | | | | |
+-----------+----------------+--------+-------+----------+----------+
Subclass
+---------------+----------------+--------+-------+----------+------------+
|Field | Type | Null | Key | Default | Extra |
+---------------+----------------+--------+-------+----------+------------+
|subclass_id | int(11) | | PRI | | |
|name | varchar(100) | | | | |
+---------------+----------------+--------+-------+----------+------------+
I do not have full control over how I obtain the data, as I am getting a large amount of data via XML. I need to create a database that will allow me to expand.
if classid and subclassid are not related,it is in 3nf and not in BCNF because modelNumber is not a dependency on id. but if they are related its not even in 3nf
I am writing a CakePHP application to log the work I do for various clients, but after trying for days I seem unable to get it to do what I want. I have read most of the book CakePHP's website.
and googled for all I'm worth, so I presume I am missing something obvious!
Every 'log item' belongs to a 'sub-project, which in turn belongs to a 'project', which in turn belongs to a 'sub-client' which finally belongs to a client. These are the 5 MySQL tables I am using:
mysql> DESCRIBE log_items;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| date | date | NO | | NULL | |
| time | time | NO | | NULL | |
| time_spent | int(11) | NO | | NULL | |
| sub_projects_id | int(11) | NO | MUL | NULL | |
| title | varchar(100) | NO | | NULL | |
| description | text | YES | | NULL | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
mysql> DESCRIBE sub_projects;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| projects_id | int(11) | NO | MUL | NULL | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
mysql> DESCRIBE projects;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| sub_clients_id | int(11) | NO | MUL | NULL | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
mysql> DESCRIBE sub_clients;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| clients_id | int(11) | NO | MUL | NULL | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
mysql> DESCRIBE clients;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
I have set up the following associations in CakePHP:
LogItem belongsTo SubProjects
SubProject belongsTo Projects
Project belongsTo SubClients
SubClient belongsTo Clients
Client hasMany SubClients
SubClient hasMany Projects
Project hasMany SubProjects
SubProject hasMany LogItems
Using 'cake bake' I have created the models, controllers (index, view add, edit and delete) and views, and things seem to function - as in I am able to perform simple CRUD operations successfully.
The Question
When editing a 'log item' at www.mydomain/log_items/edit I am presented with the view you would all suspect; namely the columns of the log_items table with the appropriate textfields/select boxes etc. I would also like to incorporate select boxes to choose the client, sub-client, project and sub-project in the 'log_items' edit view.
Ideally the 'sub-client' select box should populate itself depending upon the 'client' chosen, the 'project' select box should also populate itself depending on the 'sub-client' selected etc, etc.
I guess the way to go about populating the select boxes with relevant options is Ajax, but I am unsure of how to go about actually accessing a model from the child view of a indirectly related model, for example how to create a 'sub-client' select box in the 'log_items' edit view.
I have have found this example:
http://forum.phpsitesolutions.com/php-frameworks/cakephp/ajax-cakephp-dynamically-populate-html-select-dropdown-box-t29.html
where someone achieves something similar for US states, counties and cities. However, I noticed in the database schema - which is downloadable from the site above link - that the database tables don't have any foreign keys, so now I'm wondering if I'm going about things in the correct manner.
Any pointers and advice would be very much appreciated.
Kind regards,
Chris
Your foreign key names should be singular. So projects_id should be project_id and sub_projects_id should be sub_project_id and so forth. If you are using cake bake or scaffolding, you should not be able to edit associated data in each model edit page. As a side note, make sure all you created model classes are singular (in the /models/folder).
To edit multiple levels of association, it maybe as easy as setting the $recursive class member to 2 in each of the models where you want the edit page to have multiple levels of association. Let me know how this works out for you.
In response to your second issue.
Make sure your models have all the proper association. If you baked them they should include them but given your error it looks like they somehow weren't included. So in log_item.php you should have something like
var $belongsTo = array('SubProject');
Currently in my application I have a single table that is giving me a bit of trouble. The issue at hand is I have a value object that is mapped to this table. When the data is returned to me as an array of value objects, I have to then loop through this array and begin my recursion by matching the ParentID to parent ObjectID's.
The column ParentID is either null (acts a parent) or it holds the value of an ObjectID.
I know there has to be a better way to create this data structure so that I do not have to do recursive loops to match ParentID's with their ObjectID's.
Any help with this is greatly appreciated.
Here is the table in describe form:
+----------------+------------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------------------+-----------------------------+
| ObjectID | int(11) unsigned | NO | PRI | NULL | auto_increment |
| ObjectHeight | decimal(6,2) | NO | | NULL | |
| ObjectWidth | decimal(6,2) | NO | | NULL | |
| ObjectX | decimal(6,2) | NO | | NULL | |
| ObjectY | decimal(6,2) | NO | | NULL | |
| ObjectLabel | varchar(255) | NO | | NULL | |
| TemplateID | int(11) unsigned | NO | MUL | NULL | |
| ObjectTypeID | int(11) unsigned | NO | MUL | NULL | |
| ParentID | int(11) unsigned | YES | MUL | NULL | |
| CreationDate | datetime | YES | | 0000-00-00 00:00:00 | |
| LastModifyDate | timestamp | YES | | NULL | on update CURRENT_TIMESTAMP |
+----------------+------------------+------+-----+---------------------+-----------------------------+e
You could use a nested set model. See the very good explanation here: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/