CakePHP access indirectly related model - beginner's question - cakephp

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

Related

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.

How to change primary key type from varchar to uuid without dropping foreign key

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

How to get call info from Kamailio

I have setup a Kamailio server and am able to establish calls. I need a way to get call related information like from, to, duration,etc. I have enabled the dialog module in the config but no avail. I am not well versed with config files and I am not sure if I am doing something wrong in the config file.
You need to Modify the config file to log the call related information in kamailio database tables.Here's the link
You have to uncomment the lines in the config file those add columns to database tables.
In addition to this,a web interface siremis for monitoring server can also be installed
It's impossible to blindly know if config is good or bad. However, as general advise, be sure you use dlg_manage() before relaying the INVITE and the other SIP requests related to calls.
For troubleshooting, you can list active dialogs with 'kamctl mi dlg_list' to see if they are correctly tracked or not.
since the link you mentioned is no longer working, here is my suggestion. To get Call information you can best use CDRs , which can be done in 2 ways
set acc module CDR or
get dialog variable and send to external CDR processor.
If you want to obtain further more details about dialogs , its routes , scokets , tiemouts etc then use
dialog DB storage , which looks like
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| hash_entry | int(10) unsigned | NO | MUL | NULL | |
| hash_id | int(10) unsigned | NO | | NULL | |
| callid | varchar(255) | NO | | NULL | |
| from_uri | varchar(128) | NO | | NULL | |
| from_tag | varchar(64) | NO | | NULL | |
| to_uri | varchar(128) | NO | | NULL | |
| to_tag | varchar(64) | NO | | NULL | |
| caller_cseq | varchar(20) | NO | | NULL | |
| callee_cseq | varchar(20) | NO | | NULL | |
| caller_route_set | varchar(512) | YES | | NULL | |
| callee_route_set | varchar(512) | YES | | NULL | |
| caller_contact | varchar(128) | NO | | NULL | |
| callee_contact | varchar(128) | NO | | NULL | |
| caller_sock | varchar(64) | NO | | NULL | |
| callee_sock | varchar(64) | NO | | NULL | |
| state | int(10) unsigned | NO | | NULL | |
| start_time | int(10) unsigned | NO | | NULL | |
| timeout | int(10) unsigned | NO | | 0 | |
| sflags | int(10) unsigned | NO | | 0 | |
| iflags | int(10) unsigned | NO | | 0 | |
| toroute_name | varchar(32) | YES | | NULL | |
| req_uri | varchar(128) | NO | | NULL | |
| xdata | varchar(512) | YES | | NULL | |
+------------------+------------------+------+-----+---------+----------------+

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.

Better data-structure design

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/

Resources