I'm trying to select data from few documents and output in nested arrays.
Aggregate:
$aggBuilder = $this->documentManager->getDocumentCollection(User::class);
$outputData = $aggBuilder->aggregate([
['$match' => [
'_id' => ['$in' => $usersId]
]
],
['$unwind' => '$articles'],
['$lookup' => [
'from' => 'articles',
'localField' => 'articles._id',
'foreignField' => '_id',
'as' => 'article',
]],
['$unwind' => '$article'],
['$lookup' => [
'from' => 'comments',
'localField' => 'comments._id',
'foreignField' => 'article_id',
'as' => 'article.comment'
]],
['$group' => [
'_id' => [
'id' => '$_id',
'name' => '$name',
'desc' => '$desc',
],
'articles' => ['$addToSet' => [
'id' => '$article._id',
'title' => '$article.title',
'content' => '$article.content',
'comments' => ['id' => $article.comment.id', 'content' => '$article.comment.content'],
]],
]]
],
[
'cursor' => [
'batchSize' => 101,
],
]);
dd($outputData->toArray());
The problem is in part where I try to in every article get collection of comments for this article.
Some reason output looks this:
'Andy Jones' = [
'Article First' = [
'id' = 'sdgdf76g8df',
'title' = 'First Article'
'content' = 'Contetn of article'
'comments' = [
'id' = [
'dfdh54gdfter',
'32gdfhfgh43et',
'34te3gdfgerdt'
],
'content' = [
'content for first article',
'content for second article',
'content for third article'
]
]
]
]
As you see comments are splited in two arrays. One for 'id' and one for 'content'.
What I want is separate array for every comment and in that array data from single comment. Like bellow:
'comments' = [
[0] = [
'id' = 'dfdh54gdfter',
'content' = 'Lorem ipsum 1',
],
[1] = [
'id' = 'dfdg4554er',
'content' = 'Lorem ipsum 2',
],
[2] = [
'id' = 'dfdh54gdfter',
'content' = 'Lorem ipsum 3',
],
]
Thank you.
Related
I have three type of content elements (tt_content|types) which all use an image-column with each one FAL-relations for one image.
I'd like to use for 2 content elements the type = 'imageManipulation' (Docs) with 2 different configurations and for one just the image as it is.
Since the type = 'imageManipulation' is defined normally for sys_file_reference, so for all usages.
Is it possible with TCA overrides to archive different configurations for different content elements?
I tried a combination of columnsOverrides and overrideChildTca, but this doesn't work in the moment:
<?php
defined('TYPO3_MODE') or die();
(function () {
if (is_array($GLOBALS['TCA']['tt_content']['types']['mask_teaser_hero'])) {
$GLOBALS['TCA']['tt_content']['types']['mask_teaser_hero']['columnsOverrides'] = [
'tx_maskproject_teaserimage' => [
'config' => [
'overrideChildTca' => [
'columns' => [
'crop' => [
'label' => 'LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_file_reference.crop',
'config' => [
'type' => 'imageManipulation',
'cropVariants' => [
'mobile' => [
'title' => 'Mobile',
'selectedRatio' => '4:3',
'allowedAspectRatios' => [
'4:3' => [
'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.4_3',
'value' => 4 / 3
],
],
],
'desktop' => [
'title' => 'Desktop',
'selectedRatio' => '16:9',
'allowedAspectRatios' => [
'16:9' => [
'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.16_9',
'value' => 16 / 9
],
],
],
]
],
],
]
],
]
]
];
}
})();
I first thought about Typoscript TCEFORM:
https://metinyilmaz.de/artikel/typo3-image-cropvariants/
But this would also appear in each content element.
I found the mistake. The TCA override is correct. But the type was not.
I use EXT:mask_export for the content elements. In the example from the question I override the content elements which EXT:mask adds. But the exported content elements are different content elements.
The correct one is:
<?php
defined('TYPO3_MODE') or die();
(function () {
if (is_array($GLOBALS['TCA']['tt_content']['types']['myextname_teaser_hero'])) {
$GLOBALS['TCA']['tt_content']['types']['myextname_teaser_hero']['columnsOverrides'] = [
'tx_myextname_teaserimage' => [
'config' => [
'overrideChildTca' => [
'columns' => [
'crop' => [
'label' => 'LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_file_reference.crop',
'config' => [
'type' => 'imageManipulation',
'cropVariants' => [
'mobile' => [
'title' => 'Mobile',
'selectedRatio' => '4:3',
'allowedAspectRatios' => [
'4:3' => [
'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.4_3',
'value' => 4 / 3
],
],
],
'desktop' => [
'title' => 'Desktop',
'selectedRatio' => '16:9',
'allowedAspectRatios' => [
'16:9' => [
'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.16_9',
'value' => 16 / 9
],
],
],
]
],
],
]
],
]
]
];
}
})();
I have two tables in mongodb database
activityCountTbl contains data like
{
"_id": ObjectId("5c234f7e3250041280000ca3"),
"activityId": ObjectId("5c0e27ee590a06bf08003c33"),
"weekgroupId": ObjectId("5bfbddbcbb2c5645f8001495"),
"squadronId": ObjectId("5bfc7b7ebb2c56c320002a0a"),
"attendingCount": NumberInt(6),
....
}
{
"_id": ObjectId("5c234f7e3250041280000ca3"),
"activityId": ObjectId("5c0e27ee590a06bf08003c33"),
"weekgroupId": ObjectId("5bfbddbcbb2c5645f8001496"),
"squadronId": ObjectId("5bfc7b7ebb2c56c320002a0a"),
"attendingCount": NumberInt(6),
....
}
squadronTbl contains data like
{
"_id": ObjectId("5c19ccb7590a060691000554"),
"squadronCode": "336",
"squadronName": "336TRS",
}
{
"_id": ObjectId("5c19ccb7590a060691000556"),
"squadronCode": "337",
"squadronName": "337TRS",
}
I am storing count details of a particular activity of a weekgroup in activityCountTbl. I am performing lookup on squadronTbl with activityCountTbl
for fetching squadrons details of a particular weekgroup. The below code is not working.
When I comment/delete the $query code, it fetches all the squadrons of all weekgroups.
$query = ["ActivityArray.weekgroupId" => new MongoDB\BSON\ObjectID("5bfbddbcbb2c5645f8001495"), "ActivityArray.funRun" => "Yes"];
$pipeline = array(
[
'$match' => $query
],
[
'$lookup' => [
'from' => 'activityCountTbl',
'localField' => '_id',
'foreignField' => 'squadronId',
'as' => 'ActivityArray'
]
],
['$project' => [
'_id' => 1.0,
'squadronName' => 1.0,
'ActivityArray' => 1.0
]],
);
return $this->db->squadronTbl->aggregate($pipeline)->toArray();
Please help !!!
$query = ["ActivityArray.weekgroupId" => new MongoDB\BSON\ObjectID("5bfbddbcbb2c5645f8001495"), "ActivityArray.funRun" => "Yes"]
$pipeline = array(
[
'$match' => []
],
[
'$lookup' => [
'from' => 'activityCountTbl',
'localField' => '_id',
'foreignField' => 'squadronId',
'as' => 'ActivityArray'
]
],
[
'$match' => $query
],
['$project' => [
'_id' => 1.0,
'squadronName' => 1.0,
'ActivityArray' => 1.0
]],
);
something like that
I have two collection rounds and summaries
A record in rounds looks like
{
"_id": "2018-04",
"name": "Round 2018-04"
}
A record in summaries look like
{
"phase": "round:2018-04",
"userId": NumberLong(66325),
}
I want to query summaries and lookup into rounds joining based on phase of summaries into _id of rounds
PROBLEM: It will be pretty simple if there was no prefix of round: in phase.
Is there anyway to do this?
This is my code so far.
$cursor = $this->mongo->selectCollection('summaries')->aggregate([
array('$match' => []),
array(
'$lookup' => [
'from' => 'rounds',
'localField' => 'phase',
'foreignField' => '_id',
'as' => 'roundDetail'
]
),
array(
'$unwind' => '$roundDetail',
),
array(
'$project' => [
'userId' => 1,
'roundDetail.name' => 1
]
)
]);
MongoDB version 3.4.16
You can use substrBytes to remove characters from the string.
$cursor = $this->mongo->selectCollection('summaries')->aggregate([
array('$match' => []),
array('$addFields' => [ 'phase' => [ '$substrBytes' => [ '$phase', 6, 7 ] ] ] ),
array(
'$lookup' => [
'from' => 'rounds',
'localField' => 'phase',
'foreignField' => '_id',
'as' => 'roundDetail'
]
),
array(
'$unwind' => '$roundDetail',
),
array(
'$project' => [
'userId' => 1,
'roundDetail.name' => 1
]
)
])
I have code like this:
$users = User::raw(function ($collection) use($profession, $userId, $skip, $pageSize) {
return $collection->aggregate([
[
'$lookup' => [
'from' => 'user_profile',
'localField' => 'uid',
'foreignField' => 'user_id',
'as' => 'profile'
]
],
[
'$match' => [
'profile.field_of_work' => $profession['id'],
'uid' => ['$ne' => $userId]
],
],
['$skip' => $skip],
['$limit' => $pageSize]
]);
});
from there, how to add where clause like this:
::where('name', 'like', '%test%');
I dont know how to using lookup in mongodb and I just know the basic pattern of eloquent.
Thank you
Today I updated to ZF 2.4 to use float validator but unfortunately i realized that my file upload form field gives unexpected error messages.
Here is my form object
$this->add([
'name' => 'profileimage',
'type' => '\Zend\Form\Element\File',
'attributes' => [
'id' => 'profileimage',
'class' => 'styled',
],
]
);
And Here is my validator
$inputFilter->add([
'name' => 'profileimage',
'required' => false,
'allow_empty' => true,
'priority' => 300,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => '\Zend\Validator\File\IsImage',
],
[
'name' => '\Zend\Validator\File\UploadFile',
],
[
'name' => '\Zend\Validator\File\ImageSize',
'options' => [
'minWidth' => 300,
'minHeight' => 300,
]
],
[
'name' => '\Zend\Validator\File\Size',
'options' => [
'max' => '20MB',
]
],
]
]);
As you see the image upload field is not required and may be empty. But in my form I get these errors:
array (size=1)
'profileimage' =>
array (size=4)
'fileIsImageNotReadable' => string 'File is not readable or does not exist' (length=38)
'fileUploadFileErrorNoFile' => string 'File was not uploaded' (length=21)
'fileImageSizeNotReadable' => string 'File is not readable or does not exist' (length=38)
'fileSizeNotFound' => string 'File is not readable or does not exist' (length=38)
How can I handle this issue? I need to this field to be optional.
change your filter
$inputFilter->add([
'name' => 'profileimage',
'type' => '\Zend\InputFilter\FileInput',
'required' => false,
'allow_empty' => true,
'priority' => 300,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => '\Zend\Validator\File\IsImage',
],
[
'name' => '\Zend\Validator\File\UploadFile',
],
[
'name' => '\Zend\Validator\File\ImageSize',
'options' => [
'minWidth' => 300,
'minHeight' => 300,
]
],
[
'name' => '\Zend\Validator\File\Size',
'options' => [
'max' => '20MB',
]
],
]
]);
read about it here: http://framework.zend.com/manual/current/en/modules/zend.input-filter.file-input.html