Querying many to many relations - symfony

I can't figure how to resolve this problem... Well, a bit of code is better than a long speech... so, look at
$res = $this->getEntityManager()->createQueryBuilder()
->select(array('off.id', 'off.title', 'off.lang', 'han.id as handler', 'off.handlerWebsiteId', 'off.handlerReference',
'off.vacationsNumber', 'cat.id as category', 'acs.id as activitySector', 'wop.id as workplaces',
'off.isTelework', 'off.workDescription', 'off.experienceNeeded', 'ski.id as skills',
'off.specificSkills', 'stl.id as studyLevels', 'lak.id as languageKnowledges'))
->from('UbberjobOfferBundle:Offer', 'off')
->leftJoin('UbberjobOfferBundle:Handler', 'han', 'WITH', 'off.handler = han')
->leftJoin('UbberjobOfferBundle:Category', 'cat', 'WITH', 'off.category = cat')
->leftJoin('UbberjobOfferBundle:ActivitySector', 'acs', 'WITH', 'off.activitySector = acs')
->leftJoin('off.workplaces', 'wop')
->leftJoin('off.skills', 'ski')
->leftJoin('off.studyLevels', 'stl')
->join('off.languageKnowledges', 'lak')
->orderBy('off.dateUpdate', 'desc')
->setMaxResults($items_per_page)
->setFirstResult($start)
->getQuery()
->getResult();
Doctrine returns this:
...
5: {
id: 23
title: "INSTRUCTEUR AUTO-ÉCOLE (H/F)"
lang: "fr"
handler: 2
handlerWebsiteId: "14371214"
handlerReference: "1332551"
vacationsNumber: 3
category: 23
activitySector: 19
workplaces: 3
workDescription: "..."
experienceNeeded: 12
specificSkills: "..."
studyLevels: 13
languageKnowledges: 1
}-
6: {
id: 23
title: "INSTRUCTEUR AUTO-ÉCOLE (H/F)"
lang: "fr"
handler: 2
handlerWebsiteId: "14371214"
handlerReference: "1332551"
vacationsNumber: 3
category: 23
activitySector: 19
workplaces: 11
workDescription: "..."
experienceNeeded: 12
specificSkills: "..."
studyLevels: 13
languageKnowledges: 1
}
and what I would like is:
6: {
id: 23
title: "INSTRUCTEUR AUTO-ÉCOLE (H/F)"
lang: "fr"
handler: 2
handlerWebsiteId: "14371214"
handlerReference: "1332551"
vacationsNumber: 3
category: 23
activitySector: 19
workplaces: [3, 11]
workDescription: "..."
experienceNeeded: 12
specificSkills: "..."
studyLevels: 13
languageKnowledges: 1
}
Many to many relations cause result lines duplication (see workplaces). What can I do for having everything in a single array ?
Thank you !!!

Related

get all possible keys / key paths in a json string in r

How can I get all the different possible json paths in a json string? Often I get huge
For example, I would like to get something back like:
result = data.frame(paths = c('name',
'name.first'
,'name.last'
,'address'
,'address.city'
,'address.state'
,'age'
,'income'
,'block'))
result
given something like this...
myjson='{
"name": {
"first": "jack",
"last": "smith"
},
"address": {"city": "bigtown", "state": "texas"},
"age": "21",
"income": "123",
"block" :["abc","xyz"]
}'
I've tried experimenting with jsonlite::fromJson but that doesn't seem to get me to what I'm after exactly.
This will get you the full paths:
data.frame(result = names(as.data.frame(jsonlite::fromJSON(myjson))))
result
1 name.first
2 name.last
3 address.city
4 address.state
5 age
6 income
7 block
If you need all partial paths along with all full paths:
data.frame(
result = sort(unique(
c(names(fromJSON(myjson)),
names(as.data.frame(jsonlite::fromJSON(myjson))))))
)
result
1 address
2 address.city
3 address.state
4 age
5 block
6 income
7 name
8 name.first
9 name.last

Using TCPDF with Symfony garbles the output, unless I place $pdf within the controller

Is it not possible to generate a PDF using TCPDF while placing the HTML within the template view?
Example:
details.html.pdf:
<?php
$html = ' //all the stuff i want to appear on the pdf ';
$filename = $emailReport['subject']; //Outputs 'Email Report From Database' in output
$pdf->writeHTMLCell($w = 250, $h = 200, $x = '', $y = '', $html, $border = 0, '', $fill = 0, $reseth = true, $align = 'center', $autopadding = true);
$pdf->Output($filename,'I');
}
DefaultController.php
public function printAction()
{
return $this->delegateView(
[
'viewParameters' => [
'pdf' => $pdf
],
'contentTemplate' => 'PrintoutBundle:Default:pdf.html.php',
'passthroughVars' => [
'activeLink' => '#printout_email',
'mauticContent' => 'email',
'route' => $this->generateUrl('printout_email'),
],
]
);
}
Gives me:
%PDF-1.7 %���� 7 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20161121165447-06'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 595.276000 841.890000] /CropBox [0.000000 0.000000 595.276000 841.890000] /BleedBox [0.000000 0.000000 595.276000 841.890000] /TrimBox [0.000000 0.000000 595.276000 841.890000] /ArtBox [0.000000 0.000000 595.276000 841.890000] /Contents 8 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /PZ 1 >> endobj 8 0 obj <> stream x���S�6���W��ݴ7 ,ɒ�}�]������\g��t�D$n+�\��ܿ{rb��HlCl�0!�l��<�G�,�v ��7����?����}��o���;����/���N�����o��dk�]�S(���~vN��+Sf�3?�:�G�?�c�b��/�������s�C ����w(�x~p�o��Y�l=��PBU��Q���r0#�֪��w�+|�?A�nfW
// more of this for dozens more lines
��������j�W�\�6m�֭[/^LwY���:fp��A��\�vmpp�v16�� ��Q0�Bl5��T���A&�=`F�XX^!c�LK(� �_��WCCC���� � �Q���LK���W� endstream endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 3 0 R /F2 4 0 R /F3 5 0 R >> /XObject << /I0 13 0 R /I1 14 0 R /I2 15 0 R /I3 16 0 R >> >> endobj 6 0 obj <> /H /I>> endobj 17 0 obj << /Title (��Report Generated Nov 21, 2016) /Subject (��Email Subject from Database) /Producer (��TCPDF 6.2.12 (tcpdf.org)) /CreationDate (D:20161121165447-06'00') /ModDate (D:20161121165447-06'00') /Trapped /False >> endobj 18 0 obj << /Type /Metadata /Subtype /XML /Length 4307 >> stream application/pdf Generated Nov 21, 2016 Email Subject from the Database 2016-11-21T16:54:47-06:00 2016-11-21T16:54:47-06:00 2016-11-21T16:54:47-06:00 TCPDF 6.2.12 (tcpdf.org) uuid:4945196b-d05c-76e8-8a79-e9009fcad893 uuid:4945196b-d05c-76e8-8a79-e9009fcad893 ns.adobe.com/pdf/1.3/ pdf Adobe PDF Schema ns.adobe.com/xap/1.0/mm/ xmpMM XMP Media Management Schema internal UUID based identifier for specific incarnation of a document InstanceID URI www.aiim.org/pdfa/ns/id/ pdfaid PDF/A ID Schema internal Part of PDF/A standard part Integer internal Amendment of PDF/A standard amd Text internal Conformance level of PDF/A standard conformance Text endstream endobj 19 0 obj << /Type /Catalog /Version /1.7 /Pages 1 0 R /Names << >> /ViewerPreferences << /Direction /L2R >> /PageLayout /SinglePage /PageMode /UseNone /OpenAction [7 0 R /FitH null] /Metadata 18 0 R >> endobj xref 0 20 0000000000 65535 f 0000006237 00000 n 0000035774 00000 n 0000006309 00000 n 0000006415 00000 n 0000006526 00000 n 0000035942 00000 n 0000000015 00000 n 0000000465 00000 n 0000003474 00000 n 0000003925 00000 n 0000004460 00000 n 0000004930 00000 n 0000006640 00000 n 0000006938 00000 n 0000014933 00000 n 0000023217 00000 n 0000036214 00000 n 0000036602 00000 n 0000040992 00000 n trailer << /Size 20 /Root 19 0 R /Info 17 0 R /ID [ <4945196bd05c76e88a79e9009fcad893> <4945196bd05c76e88a79e9009fcad893> ] >> startxref 41201 %%EOF
However, if I place all of my code within the controller and completely skip using the template file, it loads perfectly.
$this->delegateView() sends data from my controller to the template.
(I've also tried making this $html = $this->delegateView(..) to no avail.)
Editing the controller like so:
return $pdf->writeHTMLCell($this->delegateView(
[ 'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), //$pdf->Output($filename,'I'),
'viewParameters' => [
'pdf' => $pdf,
'objectId' => $objectId
//more stuff sent to the template
]
]
));
in attempts to generate the PDF (while the content has been moved from the controller to the template), the PDF does not create.
I think I am wrapping the TCPDF service incorrectly, but I cannot figure out how to do it appropriately.
You should change The second Parameter of the output method to string "S". Outsource the creation of the pdf in a symfony service and use a method like "createPdf()" to get The return value of output method of tcpdf. (that's the pdf Binary content)
To get a valid PDF output, you should change the content-type header to application/pdf
If you are using a template to render your page, you get the html content-type defaultly.
Although imperfect, I got it to load:
$html = $this->delegateView(
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), //$pdf->Output($filename,'I'),
'viewParameters' => [
//variables being sent to the template here
]
]
);
$pdf->writeHTMLCell($w = 250, $h = 200, $x = '', $y = '', $html, $border = 0, '', $fill = 0, $reseth = true, $align = 'center', $autopadding = true);
return new Response($pdf->Output($filename,'I'));
}
}
However, it doesn't render JS just yet and the
HTTP/1.0 200 OK Cache-Control: no-cache
header is at the top. A work in progress.

Kibana 4 - numeric fields

Very new to this. Importing with logstash a csv file with 2 columns, like this
lun 16 feb 15; 3,00
mar 17 feb 15; 4,00
...
1st colum is date, 2nd is Humidity
I then want to produce a very simple grapf with kibana showing Data on X, Humidity value on Y - superbasic stuff
Looks like I am not able to have the 2 fileds imported properly or recognized by Kibana as field1: date field2: number
here is what I get
{
"_index": "prova-2015.02.12",
"_type": "logs",
"_id": "AUt9lYFzON9412qlRdDl",
"_score": 1,
"_source": {
"message": [
"lun 16 feb 15;3,00"
],
"#version": "1",
"#timestamp": "2015-02-12T11:38:43.283Z",
"host": "ELK-Dev-and-Demo",
"path": "/home/elkadmin/Documenti/Analytics/data-value.csv",
"Data": "lun 16 feb 15",
"HUM": "3,00"
},
"fields": {
"#timestamp": [
1423741123283
],
"Data": [
"15"
]
}
}
Still in Kibana 4 looks like the numeris value is interpreted like a string. What am I doing wrong in importing it?
Logstash conf file
input {
file {
path => "/home/elkadmin/Documenti/Analytics/data-value.csv"
start_position => "beginning"
}
}
filter {
csv {
columns => ["Data", "HUM"]
separator => ";"
}
}
output {
elasticsearch {
action => "index"
protocol => "http"
host => "localhost"
index => "prova-%{+YYYY.MM.dd}"
workers => 1
}
stdout {
codec => rubydebug
}
}
Data file has been saved to csv from an Excel file ( with proper cells type set for date and number);
mer 11 feb 15;1,00
gio 12 feb 15;4,00
ven 13 feb 15;5,60
sab 14 feb 15;8,00
dom 15 feb 15;12,50
lun 16 feb 15;3,00
mar 17 feb 15;4,60
mer 18 feb 15;7,00
gio 19 feb 15;2,20
ven 20 feb 15;5,00
sab 21 feb 15;4,50
dom 22 feb 15;2,35
lun 23 feb 15;3,00
mar 24 feb 15;6,00
mer 25 feb 15;9,10
gio 26 feb 15;2,20
Final question is also how to define the proper Visualization to show Dates and Values, why in Y do I always get options for Aggregate and not the specific value for a date?
Date histogram on X does not work , Fatal Erro when I select it and apply ( more details in a further Q when I have understood you to have Kibana recognize date and numbers )
Thanks in advance
F
I don't see that you're doing anything to make these fields be interpreted as anything other than a string.
Two options come to mind:
Set a mapping for the index that specifies 'date' as a date field and 'hum' as an integer.
Use logstash's mutate->convert feature (for the int) and date{} (for the date) to get the fields in the correct format before inserting into elasticsearch.
If you use #1, note that Elasticsearch will drop any record that can't be coerced into the right type.

Datetime xaxis in high charts showing extra labels

xAxis: {
type: 'datetime',
maxZoom: 14 * 24 * 3600000,
dateTimeLabelFormats:{
day: '%e-%b-%Y',
week: '%e-%b-%Y',
month: '%b \'%Y',
year: '%Y'
},
title: {
text: 'Days'
},
labels: {
y: 40,
rotation: 60
},
tickmarkPlacement:'on',
startOnTick: true,
endOnTick: true
}
I have added a column chart using high chart gallery. I used xaxis as datetime type. This chart shows previous 30 days report. I am giving a starting date to it but when chart is rendered its showing 1 or 2 extra dates in starting and same in ending. Ex- if I am giving 1 may as a starting date it should show from 1 may to 30 may but its showing from 30 apr to 1 june or 30 apr to 31 may?

How to find 5 closest number from matrix having attributes?

I have a matrix as follows
`> y
1 2 3
1 0.8802216 1.2277843 0.6875047
2 0.9381081 1.3189847 0.2046542
3 1.3245534 0.8221709 0.4630722
4 1.2006974 0.8890464 0.6710844
5 1.2344071 0.8354292 0.7259998
6 1.1670665 0.9214787 0.6826173
7 0.9670581 1.1070461 0.7742342
8 0.8867365 1.2160533 0.7024281
9 0.8235792 1.4424190 0.2030302
10 0.8821301 1.0541099 1.2279813
11 1.1958634 0.9708839 0.4297043
12 1.3542734 0.7747481 0.5119648
13 0.4385487 0.3588158 4.9167998
14 0.8530141 1.3578511 0.3698620
15 0.9651803 0.8426226 1.6132899
16 0.8854192 1.2272616 0.6715839
17 0.7779642 0.8132233 2.3386331
18 0.9936722 1.1629110 0.5083558
19 1.1235897 1.0018480 0.5764672
20 0.7887222 1.3101684 0.7373181
21 2.2276176 0.0000000 0.0000000`
I found one clue, but it can give position for the whole matrix,`
n<-read.table(file.choose(),header=T)
y<-n[,c("1","2","3")]
my.number=1.12270420185886 .
z<-abs(y-my.number)==min(abs(y-my.number))
which(z)
[1] 19 `
I want to find at least the 5 closest values with letter & column no too, in another way, I want the 5 closest single values from the matrix with their position.
I don't know what language it is; is it R?
In a procedural language, I would save all values to a map (val, (pos)) = (val (row, col); example (0.880..-> (1, 1)), then sort by value.
Then iterate over i<-pos (1 to map.size-5), and get the diff (pos (i), pos (i+5)), search for the minimum (diff), get the values and their position then.
Here is a solution in Scala:
val matrix = """1 0.8802216 1.2277843 0.6875047
2 0.9381081 1.3189847 0.2046542
3 1.3245534 0.8221709 0.4630722
4 1.2006974 0.8890464 0.6710844
5 1.2344071 0.8354292 0.7259998
6 1.1670665 0.9214787 0.6826173
7 0.9670581 1.1070461 0.7742342
8 0.8867365 1.2160533 0.7024281
9 0.8235792 1.4424190 0.2030302
10 0.8821301 1.0541099 1.2279813
11 1.1958634 0.9708839 0.4297043
12 1.3542734 0.7747481 0.5119648
13 0.4385487 0.3588158 4.9167998
14 0.8530141 1.3578511 0.3698620
15 0.9651803 0.8426226 1.6132899
16 0.8854192 1.2272616 0.6715839
17 0.7779642 0.8132233 2.3386331
18 0.9936722 1.1629110 0.5083558
19 1.1235897 1.0018480 0.5764672
20 0.7887222 1.3101684 0.7373181
21 2.2276176 0.0000000 0.0000000"""
// split block of text into lines
val lines=matrix.split ("\n")
// split lines into words
val rows = lines.map (l => l.split (" \\+"))
// remove the index from the beginning (1, 2, ... 21) and
// transform values from Strings to double numbers
// triples is: Array(Array(0.8802216, 1.2277843, 0.6875047), Array(0.9381081, 1.3189847, 0.2046542),
val triples = rows.map (_.tail).map(triple=> triple.map (_.toDouble))
// generate an own index for the rows and columns
// elems is: elems: Array[Array[(Double, (Int, Int))]] = Array(Array((0.8802216,(0,0)), (1.2277843,(0,1)), (0.6875047,(0,2))), Array((0.9381081,(1,0)), ...
val elems = triples.zipWithIndex.map {t=> t._1.zipWithIndex.map (vc=> (vc._1 -> (t._2, vc._2)))}
// sorted = Array((0.0,(20,1)), (0.0,(20,2)), (0.2030302,(8,2)), (0.2046542,(1,2)),
val sorted = elems.sortBy (e => e._1)
// delta5 = List(0.3588158, 0.369862, 0.2266741, 0.2338945, 0.10425639999999997, 0.1384938,
val delta5 = sorted.sliding (5, 1).map (q => q(4)._1-q(0)._1).toList
val minindex = delta5.indexOf (delta5.min) // minindex: Int = 29, delta5.min = 0.008824799999999966
// we found the smallest intervall of 5 values beginning at 29:
(29 to 29 +5).map (sorted (_))
res568: scala.collection.immutable.IndexedSeq[(Double, (Int, Int))] =
Vector( (0.8802216,(0,0)),
(0.8821301,(9,0)),
(0.8854192,(15,0)),
(0.8867365,(7,0)),
(0.8890464,(3,1)),
(0.9214787,(5,1)))
Since Scala counts from 0 to 20 and 0 to 2, where your index runs from 1 to 3 and 1 to 21 respectively, you have to add (1,1) to each of the positions=> (1,1), (10,1), and so on.

Resources