cake php webservice to display recursive data - web-services

I have table with self_join
menus
id | menu_id | title | content
1 0 main1
2 0 main2
3 1 sub_1a content
4 1 sub_1b
5 4 sub_4a
6 5 sub_5a
7 6 sub_6a content
-------------------------------
I have 2 main menus with menu_id = 0
main1(id=1) has 2 sub menus with id 3 and 4
sub_1b(id=4) has 1 sub menu with id 5
sub_4a(id=5) has 1 sub menu with id 6
sub_5a(id=6) has 1 sub menu with id 7
this can be upto 5 levels
I have to display json recursive array
I have tried custom function in controller
public function menuz($id,$arr)
{
$arr[]=$id;
$id=$id-1;
if($id>0)
{
menuz($id,$arr);
}
}
I have called above function from index action
but it is giving error
what will be best aproach

If you're using cakephp then attach Tree behavior to your model (you'll have to add a couple of columns to your table, too)
then you can do
$data = $this->YourModel->find('threaded');
(see the manual abour it) to obtain a nested array that you can easily transform in json

Related

GCP Data Prep- forward and backward fill

I have the following table which I am trying to wrangle in GCP Data prep:
Timestamp Event
2018-04-01 0
2018-04-02 0
2018-04-03 0
2018-04-04 0
2018-04-05 1
2018-04-06 0
2018-04-07 0
2018-04-08 0
I am trying to transform it in a way such that if Event is 1, then the previous 3 entries in the Event are set to 1 and the next 2 entries in Event are set to 2.
So, essentially the data set will look like the below after transformation
Timestamp Event
2018-04-01 0
2018-04-02 1
2018-04-03 1
2018-04-04 1
2018-04-05 1
2018-04-06 2
2018-04-07 2
2018-04-08 0
I have tried to use window and conditionals to achieve this, but w/o success.
Any ideas on how this transformation can be achieved? I am open to splitting the column or creating a new derived column if that can help achieve this result.
Thanks!
You can use window functions as part of your conditions in your IF statements. Using the PREV and NEXT window functions you can get the values at X rows above or below the current row in your window. Once you got the values, you can compare if they match the expected value and shape your IF statement accordingly.
For your use case, you need to verify if the PREV value at 1 or 2 position prior is equal to one and replace these rows by the number 2. If not true, if the NEXT value at position 1, 2 or 3 is equal to 1, the rows should be replaced with the number 1. Lastly, you need to check if the value at the current row is 1 and replace the remaining rows with 0. Converting this into a formula accepted by Dataprep would look like the following:
IF(PREV(Event, 1) == 1 || PREV(Event, 2) == 1, 2, IF(NEXT(Event, 1) == 1 || NEXT(Event, 2) == 1 || NEXT(Event, 3) == 1, 1, IF(Event == 1, 1, 0)))
To enter this formula on Dataprep, under the Function tab, select “Custom Formula”. Under the custom formula window, set the formula type to “Multiple row formula” as the PREV and NEXT function requires an additional argument specifying which column to sort by.

Set values of a column to a specific list of numbers

I want to create a small test data set with some specific values:
x
-
1
3
4
5
7
I can do this the hard way:
. set obs 5
. generate x = .
. replace x = 1 in 1
. replace x = 3 in 2
. replace x = 4 in 3
. replace x = 5 in 4
. replace x = 7 in 5
I can also use the data editor, but I'd like to create a .do file which can recreate this data set.
So how do I set the values of a variable from a list of numbers?
This can be done using a (to my mind) poorly documented feature of input:
clear
input x
1
3
4
5
7
end
I say poorly documented because the title of the input help page is
[D] Input -- Enter data from keyboard
which is clearly only a subset of what this command can do.
Here is another way
clear
mat x = (1,3,4,5,7)
set obs `=colsof(x)'
generate x = x[1, _n]
and another
clear
mata : x = (1,3,4,5,7)'
getmata x=x

Replace certain records in a column with given condition in kdb

I have a table below:
tab:([]a:(`$"1-01";`2;`$"3-01";`4;`$"5-01";`6);source:`a`a`b`b`a`b)
a source
-----------
1-01 a
2 a
3-01 b
4 b
5-01 a
6 b
I would like to change the 1-01 and 5-01 back to 1 but not for 3-01 depending on the source. I wrote the code below:
`$({ssr[string x;"-01";""]}each tab[`a])
by doing this I can put this back to a column, but this is not what I want. I also did the following:
`$({ssr[string x;"-01";""]}each tab[`a] where source=`a)
but after doing this I do not know how to put it back to the table. Then I thought of using execution control: but not sure how i should code it. I have it half way done and it does not really work:
?[tab[`source] = `a;`$({ssr[string tab[`a];"-01";""]});tab[`a]]
An "update" seems to be what you need:
q)update `$ssr[;"-01";""] each string a from tab where source=`a
a source
-----------
1 a
2 a
3-01 b
4 b
5 a
6 b

BaseQuery.paginate() return inconsistent results

When I manually execute this query in MySQL, the number of results I get is 28:
SELECT *
FROM position_reporting_structures prs
INNER JOIN positions AS pos ON pos.id = prs.reports_to
INNER JOIN jobs ON jobs.id = pos.job_id
INNER JOIN position_fulfillments AS pf ON pf.position_id = pos.id
INNER JOIN parties ON parties.id = pf.party_id
WHERE jobs.title LIKE '%QA/QC%' OR parties.name LIKE '%QA/QC%';
This is the interpreted SQLAlchemy value of the query above:
query = (
PositionReportingStructure.query
.join(Position,
Position.id == PositionReportingStructure.reports_to)
.join(Job,
Job.id == Position.job_id)
.join(PositionFulfillment,
PositionFulfillment.position_id == Position.id)
.join(Party,
Party.id == PositionFulfillment.party_id)
.filter(db.or_(
Job.title.like('%QA/QC%'),
Party.name.like('%QA/QC%')))
)
I have a pagination decorator that wraps a function whose returning value is a BaseQuery. Inside the decorator, these are the code snippets that I've used:
query = f(*args, **kwargs)
page = 1 // just a dummy value
per_page = 5 // just a dummy value
paginate = query.paginate(page=page, per_page=per_page)
I'm expecting that it will return more or less 6 pages and 28 items in total, but it's not. The outcome is 1 page, 4 items per page, 4 total items and no previous/next pages which is incorrect. For further investigation, I have tried changing the value of page variable to compare each one:
page = 1 : 1 page, 4 items, 4 total items
page = 2 : 6 pages, 3 items, 28 total items
page = 3 : 6 pages, 2 items, 28 total items
page = 4 : 6 pages, 3 items, 28 total items
page = 5 : 6 pages, 5 items, 28 total items
page = 6 : 6 pages, 3 items, 28 total items
As you may notice, per page items is too inconsistent. Anybody who can explain what causes this contingency? I am currently using Flask-SQLAlchemy-2.0.
I already knew the root cause of this problem. My query returns some duplicate rows which I think is not acceptable to SQLAlchemy so in return automatically gives the distinct ones (even without using GROUP BY or DISTINCT).
As the context of my problem stated, I passed a count of 5 items in the per_page parameter of the paginate function, at the first page, it would only return 4 items since it groups the duplicate ones, thus, satisfies the paginate function's conditional statement (the one that has an emphasis):
**if page == 1 and len(items) < per_page:
total = len(items)**
else:
total = self.order_by(None).count()

SlickGrid Column Groups

Is there a way to add a column groupings? For example:
Unit 1 | Unit 2 |
Pre Mid Post | Pre Mid Post |
--- --- ---- | --- --- ---- |
2 4 5 | 3 4 4 |
1 2 4 | 3 4 5 |
Basically, I need a header row for Unit that has three subcolumns in the Unit group; Pre, Mid, and Post.
This concept can also be seen in following pictures:
Yes, There is no columns grouping available in slickgrid but i created a plugin for one level column group.Take a look
https://github.com/naresh-n/slickgrid-column-group
Unfortunately grouping columns is a low priority for the creator of slickgrid as you can see in the issue post here
Although that issue was written 2 years ago, it was asked again 5 months ago and there was no reply to that post.
Apart from that by looking at his updates in the git history there is no indication it is implemented in any way.
My only suggestion is to try and move to another component.
You could maybe see if this could fit your needs: jQuery EasyUI
I actually saw a demo there that has groupings like you wanted: demo
Goodluck!
You can now do this in the active SlickGrid repo: https://github.com/6pac/SlickGrid
Here's an example: http://6pac.github.io/SlickGrid/examples/example-column-group.html