Doctrine2 Custom Repository result with multiple objects - doctrine-orm

i have the folllowing entities:
User 1:n StatementResponse n:1 Statement n:1 Survey 1:n User
As you can see, the relations between the entites are closed in a circle. I need to query all statements of a survey with the responses from a given user. Not every statement must be answered. The result should be an associative array which looks like that:
[
1 => [
'statement' => Statement Entity with id 1
'response' => 'Response Entity from the given user of Statement with id 1
],
2 => [
'statement' => Statement Entity with id 2
'response' => null
],
3 => [
'statement' => Statement Entity with id 3
'response' => Response Entity from the given user of Statement with id 3
]
]
Is it possible to get a result like this in a custom repository?

Related

Select sum and dynamic where statement query in Rails 4

I have this below statement in my controller and I want to know how to rewrite it in Rails 4
Entry.find(:all, :conditions => [#conditions.keys.join(" AND "), *#conditions.values],:group => "user_id", :select => "SUM(time_spent) AS total_spent")
The condition has the user_id information in it like "user_id=?".
The statement is trying to find the entry for a particular user id and sum the time spent for the user id and group by user id.
For example you can use it
realations = Entry.where(user_id: [1, 2, 3]).select("SUM(time_spent) as total_spent").group(:user_id)
After that you have relation with which you can work
realations.each do |i|
puts i.total_spent
end

Doctrine 2 specific fields selected via querybuilder is not accessible using getter method

When I use doctrine 2 query builder as below to create a resultset:
$queryBuilder = $this->orm->createQueryBuilder();
$queryBuilder->select("s.id","partial s.{id, name, user}","s.name","concat(u.firstname,' ',u.lastname) fullName")
->add('from', 'Application\Entity\Schools s')
->leftJoin('s.user', 'u', 'WITH', 'u.id = s.user')
->getQuery()->getResult(Query::HYDRATE_ARRAY);
echo "<pre>";
print_r($dd);
die;
It gives following data:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => DAV Public School
)
[id] => 1
[name] => DAV Public School
[fullName] => Manu Sharma
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => Delhi Public School
)
[id] => 2
[name] => Delhi Public School
[fullName] => Vipul Sharma
)
)
So when I try to fetch its value using getter method:
$result->getName() doesn't work but I have to use $result[0]->getName().
and error says:
Fatal error: Call to a member function getName() on a non-object
How to choose specific fields inside queryBuilder's select method so that main array values must be accessed using getter methods. Why its not working. Is there anything wrong with my select query.
What is the benefit if specific selected fields can't be accessed using getter method. I believe I'm doing something wrong but it doesn't strike my mind.
Note: While using $result, I removed array_hydrate, that is I'm passing object and hence expecting getter methods to work properly.
The problem you're having is caused by adding additional fields to the SELECT clause. That's causing the result to put the object at index 0 to be able to add additional fields like fullName that you don't have in the entity to the result set.
As far as I understand, all you want is to have a method to get the full name of each user. Instead of solving that problem at the query level, you better just add a method to the User entity:
public function getFullName() {
return sprintf('%s %s', $this->getFirstName(), $this->getLastName());
}
And then just select users the usual way:
SELECT u FROM User u
You can now get the full name of the first user the following way:
$result[0]->getFullName();
You don't need partial objects here. Partial objects have completely different use cases.

Dynamodb query - OR condition in KeyConditionExpression

I have a DynamoDB table with feed_guid as the global secondary index. I want to query with a set of feed_guid in that table. Since feed_guid is not my primary keys, I can't use getBatchItem. When I tried the following method, I got this error:
Invalid operator used in KeyConditionExpression: OR
$options = array(
'TableName' => 'feed',
'IndexName' => 'GuidIndex',
'KeyConditionExpression' => 'feed_guid = :v_guid1 or feed_guid = :v_guid2',
'ExpressionAttributeValues' => array (
':v_guid1' => array('S' => '8a8106e48bdbe81bf88d611f4b2104b5'),
':v_guid2' => array('S' => '19cab76242a6d85717de64fe4f8acbd4')
),
'Select' => 'ALL_ATTRIBUTES',
);
$response = $dynamodbClient->query($options);
You can't use OR condition. You should use
rangeAttributeName BETWEEN :rangeval1 AND :rangeval2
if possible or
feed_guid IN (:v_guid1, :v_guid2)
See ExpressionAttributeValues and KeyConditionExpression
In order to achieve what you want here, you'll need to take the union of two separate queries.
Currently, DynamoDB's Query API only supports having one condition on Hash AND Range Keys in only the KeyConditionExpression because this limits the items you search and ultimately reduces cost of say a more complex query like what you've described here.

CakePHP find list with group name same as data name

I have to show a select box with the different movie genres grouped by general genres.
To do so, I'm using the CakePHP default method find('list',...).
My database table for movie genres is done like so:
|_id_|_name_________|_general_genre_|
| 1 | Action | Action |
| 2 | Martial arts | Action |
| .. | ............ | ............. |
The problem is that if the group has the same name as the data (e.g.: "Action") the data is not shown in benefit of the group name.
So my select box looks like:
Select a genre
- Action
- Martial arts
- ...
And what I would like is:
Select a genre
- Action
- Action
- Martial arts
- ...
Of course, the reason is, it's not possible to select a group item.
Thanks in advance!
Ok, after some more research I finaly get the answer.
This is an attribute to add to the Html->input() method. This attribute is showParents.
So, if you want to make a select box with groups and have, inside this group, items with the same name that this group, you have to do something like this:
Select the list with the groups:
$options = $this->TableName->find('list', array(
'fields' => array('id', 'name', 'group_name')
));
Show the select input:
$this->Html->input('InputId', array(
'type' => 'select',
'options' => $options,
'showParents' => true
));
Hope this can help ;)
This is expected behavior. Use the showParents option when generating the form element. The API even has an eg. for this case which you failed to check :)
You can override the default fields that a find('list') returns (which are the primary key and the displayField) by your own fields, by setting the $options['fields'] option. These can be more than two fields if you need to. This should come close to what you need:
$this->Genre->find('list', array(
'fields' => array('id', 'name', 'general_genre')
));
This will return an array like:
'Action' =>
1 => 'Action'
2 => 'Martial arts'
You should be able to use that in a dropdown.

Symfony2: How can I list table elements in a FormChoice, separating them by some property

Is there any way to fetch objets from the DB and list them by Category? I have a table Artist which have a Genre and each Genre have a Category, so when I'm choosing Genre at new Artist creation I would like to see them separately by each one Category. I recall that in previous versions you could do something like:
$this->widgetSchema['genre_id'] = new sfWidgetFormChoice(array(
'choices' => array('-- Select --',
'-- Category1 --'=>Doctrine_Core::getTable('Genre')->getGenres('Category1')),
'-- Category2 --'=>Doctrine_Core::getTable('Genre')->getGenres('Category2')),
'multiple' => false, 'expanded' => false
));
And the result was a drop down with 'CategoryX' in bold, followed by respectively genres.
After a deep search all I've found is:
->add('group', 'entity', array(
'class' => 'Vendor\MyBundle\Entity\Group',
'label'=>'Group:',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('g')
->... // whatever you want to do
}
))
I think that is what I need, but have no idea on how to adapt it to what I'm looking for.
Many thanks.