Select some columns instead of select * with SubSonic 3 - subsonic3

I'm using SubSonic 3 as my OR mapper in the project. My problem is that the query SubSonic generates for select and other operations is like:
var repo = GetRepo();
var results = repo.GetAll();
And this would make select * from the entity, but I have to select only Id and Title from the table. I don't have permission to select * on the table

There isn't much to go off of and you probably solved this problem already but just in case someone else has the same question, here's my 2 cents.
If you just wanted to select Id and Title you could do one of the following.
I'm guessing you're using ActiveRecord.
//results in a list of anonymous objects with Id and Title
var results = (from r in YourTable.All() select new { Id = r.Id, Title = r.Title }).ToList();
If you're using AdvancedTemplate, then you could do
//create an instance of the db schema
var repo = new SubSonic.AdvancedTemplate.YourDatabase();
//results in a list of anonymous objects with Id and Title
var results = (from r in repo.YourTable select new { Id = r.Id, Title = r.Title }).ToList();

Related

How to programmatically add a record to an APEX Interactive Grid?

I need to add a new record to one of the interactive grids on my page via javascript code.
I know how to get an record from the grid:
var model = ig$.interactiveGrid("getViews","grid").model;
var record = model.getRecord(rowID);
But how do I add a record? Is there a function for that?
What I am trying to do is get selected rows from one grid and insert it into the underlying table in another grid. I got my javascript code to grab the records selected in grid1 and loop through them. Now I am getting the model of the grid2 and want to insert the records from grid1 into grid2
Try to insert a new record and immediately update the values.
var widget = apex.region('staticIdOfIG').widget();
var grid = widget.interactiveGrid('getViews','grid');
var model = grid.model;
//insert new record on a model
var myNewRecordId = model.insertNewRecord();
//get the new record
var myNewRecord = model.getRecord(myNewRecordId);
//update record values
model.setValue(myNewRecord, 'NAME_OF_COLUMN1', 'value1');
model.setValue(myNewRecord, 'NAME_OF_COLUMN2', 'value2');
Example: https://apex.oracle.com/pls/apex/f?p=145797:8
*Click on button "INSERT ROW WITH VALUES"
Workspace: stackquestions
login: test
pwd: test
page: 8

Symfony One-To-Many, unidirectional with Join Table query

I have some One-To-Many, unidirectional with Join Table relationships in a Symfony App which I need to query and I can't figure out how to do that in DQL or Query Builder.
The Like entity doesn't have a comments property itself because it can be owned by a lot of different types of entities.
Basically I would need to translate something like this:
SELECT likes
FROM AppBundle:Standard\Like likes
INNER JOIN comment_like ON comment_like.like_id = likes.id
INNER JOIN comments ON comment_like.comment_id = comments.id
WHERE likes.created_by = :user_id
AND likes.active = 1
AND comments.id = :comment_id
I've already tried this but the join output is incorrect, it selects any active Like regardless of its association with the given comment
$this->createQueryBuilder('l')
->select('l')
->innerJoin('AppBundle:Standard\Comment', 'c')
->where('l.owner = :user')
->andWhere('c = :comment')
->andWhere('l.active = 1')
->setParameter('user', $user)
->setParameter('comment', $comment)
I see 2 options to resolve this:
Make relation bi-directional
Use SQL (native query) + ResultSetMapping.
For the last option, here is example of repository method (just checked that it works):
public function getLikes(Comment $comment, $user)
{
$sql = '
SELECT l.id, l.active, l.owner
FROM `like` l
INNER JOIN comment_like ON l.id = comment_like.like_id
WHERE comment_like.comment_id = :comment_id
AND l.active = 1
AND l.owner = :user_id
';
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->_em);
$rsm->addRootEntityFromClassMetadata(Like::class, 'l');
return $this->_em->createNativeQuery($sql, $rsm)
->setParameter('comment_id', $comment->getId())
->setParameter('user_id', $user)
->getResult();
}
PS: In case of Mysql, 'like' is reserved word. So, if one wants to have table with name 'like' - just surround name with backticks on definition:
* #ORM\Table(name="`like`")
I find the Symfony documentation very poor about unidirectional queries.
Anyway I got it working by using DQL and sub-select on the owning entity, which is certainly not as fast. Any suggestion on how to improve that is more than welcomed!
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT l
FROM AppBundle:Standard\Like l
WHERE l.id IN (
SELECT l2.id
FROM AppBundle:Standard\Comment c
JOIN c.likes l2
WHERE c = :comment
AND l2.owner = :user
AND l2.active = 1
)'
)
->setParameter('user', $user)
->setParameter('comment', $comment)
;

Table view OR temporary table in Doctrine + symfony 2.3

I was trying to implement bayesian average logic in doctrine and symfony.
I have basic native Mysql query like this:
Create View `ratings` AS
SELECT
restaurant_id,
(SELECT count(restaurant_id) FROM ratings) / (SELECT count(DISTINCT restaurant_id) FROM ratings) AS avg_num_votes,
(SELECT avg(rating) FROM ratings) AS avg_rating,
count(restaurant_id) as this_num_votes,
avg(rating) as this_rating
FROM
ratings
GROUP BY
restaurant_id
SELECT
restaurant_id,
((avg_num_votes * avg_rating) + (this_num_votes * this_rating)) / (avg_num_votes + this_num_votes) as real_rating
FROM `ratings`
This query creates table view from where we are retrieving records.
From some documents I came to know that we can't create view in Doctrine. So another option is to create temporary table. How we can create temp table with different structure.
Referring : http://shout.setfive.com/2013/11/21/doctrine2-using-resultsetmapping-and-mysql-temporary-tables/
// Add the field info for each column/field
foreach( $classMetadata->fieldMappings as $id => $obj ){
$rsm->addFieldResult('u', $obj["columnName"], $obj["fieldName"]);
// I want to crate new fields to store avg rating etc.
}
How we can implement this in Doctrine and Symfony?

Can I use the DynamoDB .NET Object Persistence Model when I have a Global Secondary Index?

I have a table in Dynamo with a hash & range index plus a secondary global index with just a hash. If I try to Query or Save an object I get the following error:
Number of hash keys on table TableName does not match number of hash keys on type ObjectModelType
(Replacing TableName and ObjectModelType with the actual table and model type)
I have the hash properties (both the primary and secondary) decorated with DynamoDBHashKey
Googling the error turns up exactly zero results
Update: Ok, so not exactly zero, obviously it now returns this question!
Update the second: I've tried using the helper API & it works just fine, so I am assuming at this point that the Object Persistence Model doesn't support Global Secondary Indexes
I encountered the same problem and found FromQuery worked, although QueryFilter is actually from the DocumentModel namespace:
var queryFilter = new QueryFilter(SecondaryIndexHashKeyColumn, QueryOperator.Equal, "xxxx");
queryFilter.AddCondition(SecondaryIndexRangeKeyColumn, QueryOperator.LessThan, DateTime.Today);
var items = context.FromQuery<MyItem>(new QueryOperationConfig { IndexName = SecondaryIndexName, Filter = queryFilter }).ToList();
Thank you Brendon for that tip! I was able to adapt it to get deserialization of a multiple result set.
var queryFilter = new QueryFilter(indexedColumnName, QueryOperator.Equal, targetValue);
Table table = Table.LoadTable(client, Configuration.Instance.DynamoTable);
var search = table.Query(new QueryOperationConfig { IndexName = indexName, Filter = queryFilter });
List<MyObject> objects = new List<MyObject>();
List<Document> documentSet = new List<Document>();
do
{
documentSet = search.GetNextSetAsync().Result;
foreach (var document in documentSet)
{
var record = JsonConvert.DeserializeObject<MyObject>(document.ToJson());
objects .Add(record);
}
} while (!search.IsDone);
Thanks x1000!! :)

Sitecore: Follow relational links in Sitecore Query

I have an item structure that doens't correspond to "documents" in the standard Sitecore sense. It corresponds to data items.
One of the relationships that my items have is the following:
Person -> State -> Country
Where a Person has a link field to a State and state's have link fields to a Country.
My question is: How would one write a query to retrieve all of the People who have an eventual link to a Country of a certain abbreviation?
I'm stumped as to how to do it in Sitecore Query. My current solution is using LINQ and several queries.
Assuming that the links between these items where created using a Reference field, such as a Droplink, Treelist, Droptree, etc., you can traverse the references/referrers using the Sitecore Links database.
Here is an example (untested) that starts with a country item and fetches any referring states. The same query is performed on the states to fetch any referring people. The results are added to a generic List<Item> collection.
List<Item> peopleList = new List<Item>();
var countryReferences = Globals.LinkDatabase.GetReferences(countryItem);
var referringStates = countryReferences.Select(c => c.GetTargetItem()).Where(c => c.Template.Key == "state");
foreach (var state in referringStates)
{
var stateReferences = Globals.LinkDatabase.GetReferences(state);
var referringPeople = stateReferences.Select(s => s.GetTargetItem()).Where(s => s.Template.Key == "person");
foreach (var person in referringPeople)
{
peopleList.Add(person);
}
}