Using Grid with join on fields - atk4

I have two tables
table1: id, name, description
table2: id, name, description, idtable1 (FK of table1)
How can I make a Grid without using models but changing the dsql ? I mean maybe a join ?
I have tested using $g->dq->join('table1','table1.id=table2.idtable1')->field('name') but no success;

Try this:
$grid=$page->add('Grid');
$grid->addColumn(..);
$grid->addColumn('text','table2_name');
$grid->setSource('table1');
$grid->dq->join('table2','table2.idtable1=table1.id')
->field('table2.name table2_name');

Related

SQL Server tables extend to many tables

I'm handling this problem here in company: We have different customers, that need different fields in the same table, but we do not want to have a table with 300 columns, which is inneficient, hard to use and so on. Example:
table_products have this fields: product_id, product_name, product_cost.
then, the first client 'X' needs the field product_registerid.
the client 'Y' needs the field product_zipareaid.
That happens by different causes. Example: they are from different states, that have different rules.
At this moment we came up with this solution, which i don't like:
product_id, product_name, product_cost, product_personal. In this product_personal we have saved values like '{product_registerid:001;product_zipareaid:001-000131}'.
I came up with a theoretic solution: extend the table, and the sql will know when i do a query in the extended table, and shou me the column with the main table's column. Something like:
table_products with columns product_id, product_name, product_cost.
table_products_x with column product_registerid.
table_products_y with column product_zipareaid.
And the querys would return:
1.
select * from table_products where product_registerid = 001:
product_id, product_name, product_cost, product_registerid
1, iphone, 599, 001.
2.
select * from table_products where product_zipareaid = 000-000110:
product_id, product_name, product_cost, product_zipareaid
1, iphone, 599, 000-000110.
So, im accepting different suggestions for solving our problem.
Thank you in advance!
One approach would be to add a single Extended Properties table, that would look something like this:
Product_id (FK)
Client_id
PropertyName
PropertyValue
And so it would be populated with values like:
Product_id Client_id PropertyName PropertyValue
1 x product_registerid 001
1 y product_zipareaid 000-000110
Then you just join table_products to Extended_properties on Product_Id and put the Client_id(s) you want in the WHERE clause.
Note that you'll probably end up wanting to use a PIVOT query to get multiple extended properties for each client.

What's better in designing object attachment in sql server

I need to represent adding attachments to order or order item using sql server.
I think of two ways but wonder which one is better.
Solution 1:
table1: attachment (id, name, description, uri, ...)
table2: order (id, client_id, ...)
table3: orderItem (id, order_id, product_id, ...)
table2: objectAttachment (id, objectType, object_id, attachment_id, ...)
In this case I retrieved order attachments like this:
create proc GetOrderAttachments(#order_id int)
AS
select attachment_name, attachment_uri, attachment_desription, order.(...)
from objectAttachment orderAttachment
inner join order on order.id = orderAttachment.object_id
inner join attachment on attachment.id = orderAttachment.attachment_id
where orderAttachment.objectType = 'order' and orderAttachment.object_id = #order_id
Solution 2:
table1: attachment (id, name, description, uri, ...)
table2: orderAttachment (id, order_id, orderItem_id, ...)
table3: order (id, client_id, ...)
table4: orderItem (id, order_id, product_id, ...)
In this case I retrieved order attachments like this (knowing that orderItem_id is neglected):
create proc GetOrderAttachments(#order_id int)
AS
select attachment_name, attachment_uri, attachment_desription, order.(...)
from orderAttachment orderAttachment
inner join order on order.id = orderAttachment.order_id
inner join attachment on attachment.id = orderAttachment.attachment_id and orderAttachment.order_id = #order_id
Lets consider various situations and compare each approach.
Question. How many attachment are there of any type?
1 approach will select from one table(good).
2 approach will need to union many tables.
What if you add one more entity with attachments. You will need to remember all places where you are answering questions like this and add one more case. First approach is safe from this point of view.
You are asked to add additional attributes for order attachments and some other attributes for some other entity's attachments.
With first approach you will need to add attributes of both in one table which will be nullable and filled for just one type of entity type.
Second approach is safe. You add attributes to appropriate entities. There are no many many nullable columns just to maintain one type of attachment.
You are asked to delete all attachments of orders.
With first approach if you have huge amount of data delete operation will take long period of time.
With second approach you can remove relationships and truncate table which will take millisecond.
You can continue this list. Answering various questions and considering cons and pros of different approaches will help you to decide which approach fits better to your needs. There wouldn't be just one correct answer.
There is no better solution. All depends on your needs. The problem here is which table inheritance model fits for you.
You can start reading here:
Single Table Inheritance
Class Table Inheritance
Concrete Table Inheritance
Single Table Inheritance
Class Table Inheritance
*ttp://martinfowler.com/eaaCatalog/concreteTableInheritance.html

soql select individual CaseComment with all its FeedComments

I am trying to select all comments ,feeds and feedcomments for an individual case.
The hierarchy is like
Case
|
CaseComment
|
FeedComments(commnets or feeds under a CaseComment)
I could not find any relation between CaseComments and FeedComments nor CaseComments and CaseFeeds.
How can I select all together in a soql or individual soqls which relates Case, CaseComment,CaseFeed,FeedComment?
EDIT
The query you've included in the comment looks good. I'd write it as something like that:
SELECT Id, Body, ParentId, Parent.CaseNumber, CreatedDate,
(SELECT Id, CommentBody, CommentType FROM FeedComments)
FROM CaseFeed
ORDER BY Parent.CaseNumber, CreatedDate
(this is sample output rendered in Real Force Explorer, a pretty neat tool)
If I'll click into the "2 records" bit I can drill down to the fields selected from FeedComment for "this" CaseFeed:
If your query renders differently for you (some stuff is blank) - maybe try this different editor or even go to https://workbench.developerforce.com
If only some comments contain text - they might be uploaded images for example - filter them by CommentType = 'TextComment'?
ORIGINAL
FeedComments(commnets or feeds under a CaseComment)
No, not really. FeedComment is a Chatter table that can link to many objects but CaseComment is not one of them.
Maybe study the Chatter Entity Relationship Diagram?
Anyway - relationship to feed* objects doesn't have a nice name exposed so we can't query it all in one go:
I think you'll need something like this:
SELECT Id, CaseNumber,
(SELECT Id, CommentBody FROM CaseComments),
(SELECT Id, Body FROM Feeds)
FROM Case
SELECT Id, FeedItemId, ParentId, CommentBody
FROM FeedComment
WHERE ParentId = :caseIdHere

Sql Server - How compare hash of two rows in merge

I have several working tables that I am merging together into one final table that will be used for display. If the display table does not contain the primary key compiled from the working tables (hereafter called src)then I insert the row into display. This works fine, the next part is confusing to me.
If the primary key is already in display I only want to update the display row if the src row has the same primary key but at least one column is different from the display row. I'd like to implement this using the HASHBYTES() method using the MD5 algorithm.
From msdn, the syntax should look like this: HASHBYTES('MD5', {#variable | 'string'})
I want to be able to do something like this in my merge statement:
WHEN MATCHED AND HASHBYTES('MD5', display) != HASHBYTES('MD5', src) THEN ...(stuff)
How do I complete the HASHBYTES function?
Here is my current merge statement
MERGE dbo.DisplayCases AS display
USING (SELECT CaseId, Title, projects.ProjectName, categories.CategoryTitle, Root, milestones.MilestoneName,
milestones.MilestoneDate, Priority, statuses.StatusTitle, EstimatedHours, ElapsedHours, personAssigned.Name as AssignedTo,
personResolved.Name as ResolvedBy, cases.IsResolved, IsOpen, Opened, Resolved, Uri, ResolveUri,
OutlineUri, SpecUri, ParentId, Backlog
FROM fogbugz.Cases cases
JOIN fogbugz.Projects projects ON cases.ProjectId = projects.ProjectId
JOIN fogbugz.Categories categories ON cases.CategoryId = categories.CategoryId
JOIN fogbugz.Milestones milestones ON cases.MilestoneId = milestones.MilestoneId
JOIN fogbugz.Statuses statuses ON cases.Status = statuses.StatusId
JOIN fogbugz.People personAssigned ON cases.AssignedTo = personAssigned.Id
LEFT JOIN fogbugz.People personResolved ON cases.ResolvedBy = personResolved.Id
) as src
ON display.CaseId = src.CaseId
WHEN NOT MATCHED THEN
INSERT(CaseId, CaseTitle, ProjectName, CategoryTitle, RootId, MilestoneName, MilestoneDate, Priority,
StatusTitle, EstHrs, ElapsedHrs, AssignedTo, ResolvedBy, IsOpen, IsResolved, Opened, Resolved, Uri,
ResolveUri, OutlineUri, Spec, ParentId, Backlog)
VALUES(src.CaseId, src.Title, src.ProjectName, src.CategoryTitle, src.Root, src.MilestoneName,
src.MilestoneDate, src.Priority, src.StatusTitle, src.EstimatedHours, src.ElapsedHours,
src.AssignedTo, src.ResolvedBy, src.IsResolved, src.IsOpen, src.Opened, src.Resolved,
src.Uri, src.ResolveUri, src.OutlineUri, src.SpecUri, src.ParentId, src.Backlog);
From Martin Smith's comment...
You could do WHEN MATCHED AND EXISTS(SELECT Source.* EXCEPT SELECT Target.*) THEN UPDATE ...

How to specify the columns in a MultiMap query instead of using * (asterisk)

Sometimes I don't want to fill my entities entirely (some properties are not always necessary), but, how can I specify the table columns in a MultiMap query?
Just an example:
Table Costumer
ID (PK)
Name
Address
CityID (FK to Table City, column ID)
Email
Table City
ID (PK)
Name
State
I need to use a query like this:
SELECT Costumer.ID, Costumer.Name, Costumer.Email, City.ID AS CityID, City.Name AS CityName
FROM Costumer INNER JOIN City ON Costumer.CityID = City.ID
My question is: how can I specify the join columns without having name conflict and having dapper to identify the mapping between the columns and the entity's properties?
With the clarifications in the comments, it should work something like:
var custs = conn.Query<Customer,City,Customer>(sql,
(cust,city) => { cust.City = city; return cust; },
splitOn: "ID,CityID").ToList();

Resources