Getting relation table like nested objects in Propel ORM - select

I have a query:
UserQuery::create()
->leftJoinWith('User.Employee')
->select(array('Email','Password','Status','Employee.Email','Employee.FirstName','Employee.LastName'))
->find();
How to get Employee table as nested object, not like list of columns?
I have:
[
"Email":"test#test.pl",
"Password":"test",
"Status":true,
"Employee.Id":"4",
"Employee.FirstName":"roman",
"Employee.LastName":"stonoga"
]
But I have to have:
[
"Email":"test#test.pl",
"Password":"test",
"Status":true,
"Employee": {
"Id":"4",
"FirstName":"roman",
"LastName":"stonoga"
}
]
Many thanks for any help!

This is because Propel is treating your array the same way as how your objects are structured/related. i.e. Employee as sub-entity within User, and NOT as one object.
If you want one object, then maybe a view will help.
Another view-related question

You should use a foreign key for this
$userArray = $User->toArray()
$userArray['employee'] = $User->getEmployee()->toArray()

Related

How to get data sorted with relation using TypeORM?

I want to get user data with relation sorted but with this code it just sort the user but I want to sort data that have relation with user I'm using eager, could any one help me ?
getUerWithId(pramas: string): Observable<userEntity[]> {
return from(this.userRepository.find({
where: [
{ id: pramas }
],
order:{
id:'DESC'
}
}))
}
With the repository, I don't know if it is possible, but you can try with query builder to get ordered relations. Follow example bellow:
this.userRepository.createQueryBuilder()
.innerJoinAndSelect("User.notification", "Notification")
.orderBy({'Notification.createdAt': 'DESC'})
.where("User.id = :id", {
id: Number(id),
})
.getOne();
Remember to put the right relationship for your entity, and right names for your properties.

Hasura aggregation in related tables

I was evaluating Hasura GraphQL engine + Postgres on Heroku and have run into a situation. I have 2 simple tables as shown below;
Employees
Payrolls
Employees table and Payrolls have a foreign key employees.ID -> payrolls.employee_id
Employees table has a foreign key with itself employees.manager_id -> employees.ID
I have gone ahead and "auto-tracked" all the relationships in Hasura.
What I want to calculate is the "sum of salaries" for all employees reporting. But, when I'm using the Hasura explorer to form the GQL query, I'm not able to find the "sum" aggregation under the "managed_employees_aggregate" subquery.
The expected output is
[
{
"full_name": "anuj gupta",
"total_reportee_salary": 4000
},
{
"full_name": "sowmya",
"total_reportee_salary": 2000
},
... "total_reportee_salary" for everyone else is 0
]
Any suggestions or references is really appreciated.
Answering my own question
As suggested by #Ambassel in the comments I ended up creating a view
create view reportee_total_vw AS
select
employees.manager_id,
SUM(payrolls.salary)
from
employees,
payrolls
where
payrolls.employee_id = employees."ID"
group by
employees.manager_id
Next I created a relationship named "reportee" that bound the "ID" from employees table with the "manager_id" from the view.
After that I could issue the underlying GQL query to get the result I wanted (although not in the exact format, but I can live with that :))
{
employees {
full_name
reportee {
total_reportee_salary:sum
}
}
}

OrientDB Embedded-List (of String): wildcard query

I can't perform a wildcard-query on an embedded-list property of vertex (or edge).
For example:
Assume we have a Person class with a multi-value property named Nicknames and one instance of it:
{
"#type": "d",
"#rid": "#317:0",
"#version": 1,
"#class": "Person",
"Nicknames": [
"zito",
"ziton",
"zitoni"
]
}
then,
Select FROM Person WHERE Nicknames like "zit%"
returns empty result-set, while:
Select FROM Person WHERE Nicknames ="zito" returns 1 item correctly.
There's a NOTUNIQUE_HASH_INDEX index on the field Nicknames.
I've tried many ways (contains, index-query...) with no luck :(
I'm probably missing something basic.
I know is not an ideal solution what i'm going to write but, to stay stuck with your requirement of "query by wildcard" this is the only way that worked for me, as AVK stated is a better idea work with a Lucene index, but with the standard implementation i was unable to let it work, now here what i've done:
Use studio to create a javascript function with 2 parameter with name "array" and "rule", lets name the function "wildcardSearch"
past this code in the body of the function (is just simple javascript change it if it dosent do the job) :
for(i=0; i<array.length ; i++){
rule= rule.split("*").join(".*");
rule= rule.split("*").join(".*");
rule= "^" + ruleValue + "$";
var regex = new RegExp(rule);
if (regex.test(array[i]))
return true;
}
return false;
Remember to save the fucntion
now you can query:
Select from Person where wildcardSearch(nicknames,'zit*')=true
CONSIDERATIONS: is a brute force method, but show how "funny" can be play around with the "stored procedure" in OrientDb so i've decided to share it anyway, if performance are your main goal this things is not for you, it scan all the class and do the loop on the array to apply the regex. An Index is a way better solution, or change your db with a different data structure.
You can try this:
select from Person where Nicknames containstext 'zit'
Hope that helps

Extbase: get number of child objects

I have following model: "Shelf" and "Book", being in 1:n relation.
In the listing of shelves I need the number of books on each shelf. How can I get that?
In the database table "Shelf" there is a column "books" having the number I need. But in the model "books" is an ObjectStorage, so I cannot get the count of child object over this property.
In my list view I have
<f:for each="{shelves}" as="shelf">
...
{shelf.books.0.title} //<-- this works perfectly returning title of first book
{shelf.books.count} //<-- this produces no output
I found it myself: <f:count>{shelf.books}</f:count> is the solution

How do you clone ( duplicate ) a MongoDB object in a collection of the same db?

I need to duplicate (clone) an object in the collection via dbshell. Having something like this :
> db.users.distinct( 'nickname' )
[
"user1",
"user2",
"user3",
"user4"
]
>
where user1 select a complex object in users collection, how can I duplicate the object then change (rename) user1 field in userX ?
Code
> user = db.users.findOne({'nickname': 'user1'})
> user.nickname = 'userX'
> delete user['_id']
> db.users.insert(user)
Description
You need to find user object and put it into the variable. Than you need to modify the property you want and than you need to insert the whole object as new one. To achieve that you need to delete _id property that the object already has. And than just use insert to create the new one.
Do not delete the _id property; for some reason some values lose their type. For example, integers are converted to doubles.
Use this solution:
var user = db.users.findOne(...)
user._id = new ObjectId()
// set other properties
db.users.insert(user)
The _id field is a required field and we can't delete it like that. What I do is call toJSON() to the returned object and then delete the _id.
var rObject = dbObject.toJSON();
delete rObject._id;
db.insert(rObject);