Grails add domain field as database index - database

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?

Related

Left Join Not Returning Expected Results

I have a table of Vendors (Vendors):
+-----+-------------+--+
| ID | Vendor | |
+-----+-------------+--+
| 1 | ABC Company | |
| 2 | DEF Company | |
| 3 | GHI Company | |
| ... | ... | |
+-----+-------------+--+
and a table of services (AllServices):
+-----+------------+--+
| ID | Service | |
+-----+------------+--+
| 1 | Automotive | |
| 2 | Medical | |
| 3 | Financial | |
| ... | ... | |
+-----+------------+--+
and a table that links the two (VendorServices):
+-----------+-----------+
| Vendor ID | ServiceID |
+-----------+-----------+
| 1 | 1 |
| 1 | 3 |
| 3 | 2 |
| ... | ... |
+-----------+-----------+
Note that one company may provide multiple services while some companies may not provide any of the listed services.
The query results I want would be, for a given Vendor:
+------------+----------+
| Service ID | Provided |
+------------+----------+
| 1 | 0 |
| 2 | 0 |
| 3 | 1 |
| ... | ... |
+------------+----------+
Where ALL of the services are listed and the ones that the given vendor provides would have a 1 in the Provided column, otherwise a zero.
Here's what I've got so far:
SELECT
VendorServices.ServiceID,
<Some Function> AS Provided
FROM
AllServices LEFT JOIN VendorServices ON AllServices.ID = VendorServices.ServiceID
WHERE
VendorServices.VendorID = #VendorID
ORDER BY
Service
I have two unknowns:
The above query does not return every entry in the AllServices table; and
I don't know how to write the function for the Preovided column.
You need a LEFT join of AllServices to VendorServices and a case expression to get the column provided:
select s.id,
case when v.serviceid is null then 0 else 1 end provided
from AllServices s left join VendorServices v
on v.serviceid = s.id and v.vendorid = #VendorID
See the demo.

Eloquent ORM Model Two or More Table

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.

Count rows which has the same ID and display on the table

This is the original table:
| ID | Card_No |
|----+---------|
| 1 | 6453671 |
| 1 | 8795732 |
| 1 | 9948495 |
| 2 | 7483009 |
| 2 | 1029001 |
| 3 | 7463094 |
Is it possible to make it like this? Which will be adding a calculated column the the original table?
| ID | Card_No | Total |
|----+---------|-------|
| 1 | 6453671 | 3 |
| 1 | 8795732 | 3 |
| 1 | 9948495 | 3 |
| 2 | 7483009 | 2 |
| 2 | 1029001 | 2 |
| 3 | 7463094 | 3 |
I'm using Microsoft Access, and I've tried code like this:
SELECT ID, COUNT (*) AS Total FROM Table GROUP BY ID
But I did not get the result I want.
First of all, saving the calculated value back into table is not only unnecessary but bad design.
Options:
build a report that counts records with an expression in textbox
build aggregate query then another query joining aggregate query to the table
DCount() domain aggregate function in query

Where is Drupal Organic Group Role Data stored? How do I access it?

I've created a custom Role for my Group.
I've assigned a user, who was a member of the Group, to this special Role.
Now, I want to access a user's Role in the Group via PHP, but I can't find it anywhere.
I've pored over the Devels of both the User and Group. I can access the fact that the user is a member of the group in the user's group_audience array, but not what their role is in that group.
Any advice?
Edit: Drupal 7
Edit: There's some background here first, then the actual answer is after that.
Background
Looking in the mySQL database on our development server, there seems to be a handful of OG-related tables in our Drupal database. I'm pretty sure the version running on the devserver is og-7.x-1.x-dev.
og
og_membership
og_membership_type
og_menu
og_role
og_role_permission
og_users_roles
field_data_og_membership_request
field_revision_og_membership_request
Their definitions look like this:
mysql> describe og;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| gid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| etid | int(10) unsigned | NO | MUL | 0 | |
| entity_type | varchar(32) | NO | | | |
| label | varchar(255) | NO | | | |
| state | int(11) | NO | | 1 | |
| created | int(11) | NO | | 0 | |
+-------------+------------------+------+-----+---------+----------------+
6 rows in set (0.02 sec)
mysql> describe og_membership;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| type | varchar(255) | NO | | | |
| etid | int(10) unsigned | NO | MUL | 0 | |
| entity_type | varchar(32) | NO | | | |
| gid | int(11) | NO | MUL | NULL | |
| state | varchar(255) | YES | | | |
| created | int(11) | NO | | 0 | |
+-------------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> describe og_membership_type;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | | |
| description | varchar(255) | NO | | | |
| status | tinyint(4) | NO | | 1 | |
| module | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> describe og_menu;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| gid | int(11) | NO | PRI | NULL | |
| menu_name | varchar(128) | NO | PRI | | |
+-----------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> describe og_role;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| rid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| gid | int(11) | NO | | NULL | |
| name | varchar(64) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> describe og_role_permission;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| rid | int(10) unsigned | NO | PRI | NULL | |
| permission | varchar(64) | NO | PRI | | |
| module | varchar(255) | NO | | | |
+------------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> describe og_users_roles;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| uid | int(10) unsigned | NO | PRI | 0 | |
| rid | int(10) unsigned | NO | PRI | 0 | |
| gid | int(11) | NO | PRI | NULL | |
+-------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> describe field_data_og_membership_request;
+------------------------------+------------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+------------------------------+------------------+------+-----+---------+
| entity_type | varchar(128) | NO | PRI | |
| bundle | varchar(128) | NO | MUL | |
| deleted | tinyint(4) | NO | PRI | 0 |
| entity_id | int(10) unsigned | NO | PRI | NULL |
| revision_id | int(10) unsigned | YES | MUL | NULL |
| language | varchar(32) | NO | PRI | |
| delta | int(10) unsigned | NO | PRI | NULL |
| og_membership_request_value | longtext | YES | | NULL |
| og_membership_request_format | varchar(255) | YES | MUL | NULL |
+------------------------------+------------------+------+-----+---------+
9 rows in set (0.00 sec)
mysql> describe field_revision_og_membership_request;
+------------------------------+------------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+------------------------------+------------------+------+-----+---------+
| entity_type | varchar(128) | NO | PRI | |
| bundle | varchar(128) | NO | MUL | |
| deleted | tinyint(4) | NO | PRI | 0 |
| entity_id | int(10) unsigned | NO | PRI | NULL |
| revision_id | int(10) unsigned | NO | PRI | NULL |
| language | varchar(32) | NO | PRI | |
| delta | int(10) unsigned | NO | PRI | NULL |
| og_membership_request_value | longtext | YES | | NULL |
| og_membership_request_format | varchar(255) | YES | MUL | NULL |
+------------------------------+------------------+------+-----+---------+
9 rows in set (0.00 sec)
(I've removed the empty Extras column from the two field_* tables to avoid horizontal scrolling.) Hope that helps?
My workings
Having just had to mess with this myself on my own Drupal site, it turns out that og_membership has a row for each user in each group (I've abbreviated the type, which read og_membership_type_default):
mysql> select * from og_membership where gid = 324 and etid = 182905;
+--------+-----------------+--------+-------------+-----+-------+------------+
| id | type | etid | entity_type | gid | state | created |
+--------+-----------------+--------+-------------+-----+-------+------------+
| 223562 | og_m..._default | 182905 | user | 324 | 1 | 1329388409 |
+--------+-----------------+--------+-------------+-----+-------+------------+
1 row in set (0.01 sec)
In this row, the id is an autoincrementing identifier for the table og_membership, the etid corresponds to the users.uid for the user in question and gid corresponds to to the og.gid for the group in question.
So if I run the query
update og_membership set gid = 38 where gid = 324;
then all the members of group #324 are moved to group #38 (which is what I've just needed to do, due to an error in an import script).
I think the answer to your question is that the og_membership.type corresponds to an og_membership_type.name. Looking at that table:
mysql> select * from og_membership_type;
+----+----------------------------+-------------+--------+--------+
| id | name | description | status | module |
+----+----------------------------+-------------+--------+--------+
| 1 | og_membership_type_default | Default | 2 | og |
+----+----------------------------+-------------+--------+--------+
1 row in set (0.00 sec)
, I think the og_membership_type.status corresponds to the og_role.rid:
mysql> select * from og_role;
+-----+-----+----------------------+
| rid | gid | name |
+-----+-----+----------------------+
| 1 | 0 | non-member |
| 2 | 0 | member |
| 3 | 0 | administrator member |
+-----+-----+----------------------+
3 rows in set (0.00 sec)
Actual answer
So I think the query you want is:
select og_role.name
from og_role
inner join og_membership_type on og_role.rid = og_membership_type.status
inner join og_membership on og_membership_type.name = og_membership.type
where og_membership.gid = $group_id;
where $group_id is the og.gid of the group in question. (The ID that appears in the URL is the og.etid, so you might want to add another join to that query.

CakePHP access indirectly related model - beginner's question

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

Resources