get data from 3rd associated table in cakephp 3.x - recursion

Ho to do recursion in cakephp 3.x
I have 4 tables
users
----------
|id|name |
|1 | mick|
----------
responses
-------------------------
id|question_id| response|
1 | 1 | slim |
-------------------------
questions
------------------
id | question |
1 | body type |
-------------------
user_respopnses
----------------------------------------------
id | user_id | question_id | response_id |
1 | 1 | 1 | 1 |
----------------------------------------------
following query is auto generated by Cakephp to fetch data
$users = $this->Users->get($id, [
'contain' => ['Responses' ]
]);
Response by running above query
Id Question Id Response Actions
1 1 slim View Edit Delete
I needs to display question attribute(text) instead of question Id but stuck.
Relation defined in User Table
$this->table('users');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Responses', [
'foreignKey' => 'user_id',
'targetForeignKey' => 'response_id',
'joinTable' => 'users_responses'
]);
}
after debugging I got this data
'responses' => [
(int) 0 => object(App\Model\Entity\Response) {
'id' => (int) 1,
'question_id' => (int) 1,
'response' => 'slim',
'_joinData' => object(Cake\ORM\Entity) {
'response_id' => (int) 1,
'id' => (int) 1,
'user_id' => (int) 1,
'question_id' => (int) 1,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'UsersResponses'
},
Actually, User has relation with Responses table and no association with Questions table, but we can get them from 3rd table "users_responses" as this table has question_id, user_id, reponse_id. but cakephp 3.x does not allow recursion. Right now query hitting Response table and users_response table only. Any suggestion or hint to solve this issue much be appreciated.
Following SQL query to achieve result what I want (I used user_id = 4 static value as It was run on mysqlyog )
SELECT users.id , questions.question, responses.response
FROM questions, responses, users_responses , users
WHERE
users.id = 4 AND
users_responses.user_id = 4 AND users_responses.response_id = responses.id
AND
users_responses.question_id = responses.question_id
AND
responses.question_id = questions.id;

"Recursion" isn't quite the right word here, but what you want is
$users = $this->Users->get($id, [
'contain' => ['Responses' => ['Questions']]
]);
This is pretty clearly described in the Eager Loading Associations portion of the manual.

Related

How to count array elements across filtered records in DynamoDB

I have DynamoDB records using this structure, category being the partition key:
{ 'category':'1',
'name' : 'wind instruments',
'instruments' : [
{ 'name' : 'oboe',
'count' : 3 },
{ 'name' : 'recorder',
'count' : 2 },
{ 'name' : 'trumpet',
'count' : 2} ]
}
{ 'category':'1',
'name' : 'string instruments',
'instruments' : [
{ 'name' : 'violin',
'count' : 6 },
{ 'name' : 'cello',
'count' : 3 } ]
}
I would like to run the following queries on this DynamoDB table :
how many kinds of instrument do I have ? (answer is 5)
how many instruments do I have in total ? (answer is 16)
By either using :
AWS CLI
PartiQL
Python code
Thanks for your help !
side note: please do not suggest any change in the data model, it's designed like this on purpose (The app reading the instruments inventory per instrument category is the principal access pattern).
DynamoDB does not do these types of queries for you, there is no count or distinct for example. My suggestion is to either have an aggregate table or store aggregate data back to the table. To do this you would use DynamoDB Streams.
----------------------------------------------------------------------------
| pk | sk | instruments | inst_kind | inst_type |
----------------------------------------------------------------------------
| category1 | wind | map of instrumnets | | |
----------------------------------------------------------------------------
| category1 | string | map of instruments | | |
----------------------------------------------------------------------------
| category1 | aggregate | | 5 | 16 |
----------------------------------------------------------------------------
To update the aggregates you use Streams and Lambda and adjust the values each time you modify the item.
To retrieve the aggregates, you simply do a GetItem where PK = "{categoryId}" and SK = "aggreagate"

MariaDB JSON_ARRAYAGG gives wrong result

I have 2 problems in MariaDB 15.1 when using JSON_ARRAYAGG
The brackets [] are omitted
Incorrect wrong result, values are duplicates or omitted
My database is the following:
user:
+----+------+
| id | name |
+----+------+
| 1 | Jhon |
| 2 | Bob |
+----+------+
car:
+----+---------+-------------+
| id | user_id | model |
+----+---------+-------------+
| 1 | 1 | Tesla |
| 2 | 1 | Ferrari |
| 3 | 2 | Lamborghini |
+----+---------+-------------+
phone:
+----+---------+----------+--------+
| id | user_id | company | number |
+----+---------+----------+--------+
| 1 | 1 | Verzion | 1 |
| 2 | 1 | AT&T | 2 |
| 3 | 1 | T-Mobile | 3 |
| 4 | 2 | Sprint | 4 |
| 5 | 1 | Sprint | 2 |
+----+---------+----------+--------+
1. The brackets [] are omitted
For example this query that gets users with their list of cars:
SELECT
user.id AS id,
user.name AS name,
JSON_ARRAYAGG(
JSON_OBJECT(
'id', car.id,
'model', car.model
)
) AS cars
FROM user
INNER JOIN car ON user.id = car.user_id
GROUP BY user.id;
Result: brackets [] were omitted in cars (JSON_ARRAYAGG has the behavior similar to GROUP_CONCAT)
+----+------+-----------------------------------------------------------+
| id | name | cars |
+----+------+-----------------------------------------------------------+
| 1 | Jhon | {"id": 1, "model": "Tesla"},{"id": 2, "model": "Ferrari"} |
| 2 | Bob | {"id": 3, "model": "Lamborghini"} |
+----+------+-----------------------------------------------------------+
However when adding the filter WHERE user.id = 1, the brackets [] are not omitted:
+----+------+-------------------------------------------------------------+
| id | name | cars |
+----+------+-------------------------------------------------------------+
| 1 | Jhon | [{"id": 1, "model": "Tesla"},{"id": 2, "model": "Ferrari"}] |
+----+------+-------------------------------------------------------------+
2. Incorrect wrong result, values are duplicates or omitted
This error is strange as the following conditions must be met:
Consult more than 2 tables
The DISTINCT option must be used
A user has at least 2 cars and at least 3 phones.
Duplicate values
for example, this query that gets users with their car list and their phone list:
SELECT
user.id AS id,
user.name AS name,
JSON_ARRAYAGG( DISTINCT
JSON_OBJECT(
'id', car.id,
'model', car.model
)
) AS cars,
JSON_ARRAYAGG( DISTINCT
JSON_OBJECT(
'id', phone.id,
'company', phone.company,
'number', phone.number
)
) AS phones
FROM user
INNER JOIN car ON user.id = car.user_id
INNER JOIN phone ON user.id = phone.user_id
GROUP BY user.id;
I will leave the output in json format and I will only leave the elements that interest.
Result: brackets [] were omitted and duplicate Verizon
{
"id": 1,
"name": "Jhon",
"phones": // [ Opening bracket expected
{
"id": 5,
"company": "Sprint",
"number": 2
},
{
"id": 1,
"company": "Verzion",
"number": 1
},
{
"id": 1,
"company": "Verzion",
"number": 1
}, // Duplicate object with the DISTINCT option
{
"id": 2,
"company": "AT&T",
"number": 2
},
{
"id": 3,
"company": "T-Mobile",
"number": 3
}
// ] Closing bracket expected
}
Omitted values
This error occurs when omit phone.id is omitted in the query
SELECT
user.id AS id,
user.name AS name,
JSON_ARRAYAGG( DISTINCT
JSON_OBJECT(
'id', car.id,
'model', car.model
)
) AS cars,
JSON_ARRAYAGG( DISTINCT
JSON_OBJECT(
--'id', phone.id,
'company', phone.company,
'number', phone.number
)
) AS phones
FROM user
INNER JOIN car ON user.id = car.user_id
INNER JOIN phone ON user.id = phone.user_id
GROUP BY user.id;
Result: brackets [] were omitted and Sprint was omitted.
Apparently this happens because it makes an OR type between the columns of the JSON_OBJECT, since the company exists in a different row and number in a other different row
{
"id": 1,
"name": "Jhon",
"phones": // [ Opening bracket expected
//{
// "company": "Sprint",
// "number": 2
//}, `Sprint` was omitted
{
"company": "Verzion",
"number": 1
},
{
"company": "AT&T",
"number": 2
},
{
"company": "T-Mobile",
"number": 3
}
// ] Closing bracket expected
}
GROUP_CONCAT instance of JSON_ARRAYAGG solves the problem of duplicate or omitted objects
However, by adding the filter WHERE user.id = 1, the brackets [] are not omitted and also the problem of duplicate or omitted objects is also solved:
{
"id": 1,
"name": "Jhon",
"phones": [
{
"id": 1,
"company": "Verzion",
"number": 1
},
{
"id": 2,
"company": "AT&T",
"number": 2
},
{
"id": 3,
"company": "T-Mobile",
"number": 3
},
{
"id": 5,
"company": "Sprint",
"number": 2
}
]
}
What am I doing wrong?
So far my solution is this, but I would like to use JSON_ARRAYAGG since the query is cleaner
-- 1
SELECT
user.id AS id,
user.name AS name,
CONCAT(
'[',
GROUP_CONCAT( DISTINCT
JSON_OBJECT(
'id', car.id,
'model', car.model
)
),
']'
) AS cars
FROM user
INNER JOIN car ON user.id = car.user_id
GROUP BY user.id;
-- 2
SELECT
user.id AS id,
user.name AS name,
CONCAT(
'[',
GROUP_CONCAT( DISTINCT
JSON_OBJECT(
'id', car.id,
'model', car.model
)
),
']'
) AS cars,
CONCAT(
'[',
GROUP_CONCAT( DISTINCT
JSON_OBJECT(
'id', phone.id,
'company', phone.company,
'number', phone.number
)
),
']'
) AS phones
FROM user
INNER JOIN car ON user.id = car.user_id
INNER JOIN phone ON user.id = phone.user_id
GROUP BY user.id;

Drupal update 8.9.11 to 8.9.15 generate a Unknown column error

after update 8.9.11 to 8.9.15 drupal generating error i have update db also
error is ->
Drupal\Core\Database\DatabaseExceptionWrapper: Exception in Day[day]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_learning_target_id' in 'on clause': SELECT DISTINCT node_field_data.nid AS nid, COUNT(flagging_node_field_data.uid) AS flagging_node_field_data_uid, COUNT(node_field_data_paragraph__field_learning.nid) AS node_field_data_paragraph__field_learning_nid, MIN(node_field_data.nid) AS nid_1, MIN(node_field_data_paragraph__field_learning.nid) AS node_field_data_paragraph__field_learning_nid_1, MIN(flagging_node_field_data.id) AS flagging_node_field_data_id FROM {node_field_data} node_field_data INNER JOIN {node_field_data} node_field_data_paragraph__field_learning ON field_learning_target_id = node_field_data_paragraph__field_learning.nid LEFT JOIN {flagging} flagging_node_field_data ON node_field_data_paragraph__field_learning.nid = flagging_node_field_data.entity_id AND (flagging_node_field_data.flag_id = :views_join_condition_0 AND flagging_node_field_data.uid = :views_join_condition_1) WHERE ((node_field_data.nid = :node_field_data_nid)) AND ((node_field_data.status = :db_condition_placeholder_2) AND (node_field_data.type IN (:db_condition_placeholder_3))) GROUP BY node_field_data.nid; Array ( [:node_field_data_nid] => 547 [:db_condition_placeholder_2] => 1 [:db_condition_placeholder_3] => day [:views_join_condition_0] => orientation_completed [:views_join_condition_1] => 1 ) in Drupal\views\Plugin\views\query\Sql->execute() (line 1543 of core/modules/views/src/Plugin/views/query/Sql.php).
Please help how to resolve that
This table is from your field content called learning
You have to discover which content is from and why it dissapear.

WordPress | WP_Query | Get latest port from category and sub-categories

I have write a theme for WordPress and I like to have the latest posts posted in category and it's sub-categories to be displayed on top of any other post.
An example. Lets say I have the following categories:
Cat 1
Cat 1 - 1
Cat 1 - 2
Cat 1 - 2 - 1
And then I create the following posts :
Post #5 | Cat 1 - 2 | Date 2013
Post #4 | Cat 1 - 1 | Date 2012
Post #3 | Cat 1 - 2 | Date 2011
Post #2 | Cat 1 | Date 2010
Post #1 | Cat 1 - 1 - 2 | Date 2009
In the front end, when I navigate to Cat 1 I do not get as latest post the Post #5 that belong to Cat 1 - 2 where it is sub category of the Cat 1, But instead I am getting the Post #2.
Currently I am using this code :
$categoryID = get_query_var('cat');
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category__in' => array($categoryID),
'post_status' => 'publish'
);
$eiPost = new WP_Query($args);
The problem is that this code return the latest post only from the top level category and not from the sub categories. How can I modify this code, in order to retrieve the latest posts from all the sub-categories and the top category ?
Is there any solution to this problem ?
'category__in' only displays posts from that category, not children categories.
Try using 'cat' => $categoryID insted. So your $args would be:
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'cat' => $categoryID,
'post_status' => 'publish'
);

Fetching array data from database in symfony2

I have a table "config" with three fields
------------------
id | name | value
------------------
1 | na1 | va1
2 | na2 | va2
3 | na3 | va3
. | ... | ...
I need to fetch the above data to an array "$confData" as given below
$confData ===>
array(
'na1' => 'va1',
'na2' => 'va2',
'na1' => 'va3',
' . ' => '...'
);
I want to know wether there is any predefined function/method in symfony-doctrine to get this array? If it is not available, how can use doctrine way?
There is no such default way. You should build this array manually for fetched object.
//after fetching $objectCollection
$config = array();
foreach ($objectCollection as $object) {
$config[$object->getName()] = $object->getValue();
}

Resources