How to do nested eloquent relation - laravel-5

I have 3 model. Example they are A, B, and C. A is hasMany B, and C hasOne B. Example the relation between A & B is "relationB". so if I retrieve the data from A, I can do "A::with('relationB')".
My problem is, how can I access the relation between B & C if I retrieve all datas from A?

You can simply do it by the following line:
A::with('B.C')->get()
Then every A-object will have their associated B models, and every B-model will have their C-objects included.

Related

Prolog rule is returning an exception with semi-colon usage

So, whilst I cannot share the exact code, I have something like this:
belongsTo(A,B) :- bag(A,B); shoe(A,B); sunglasses(A,B).
If bag B belongs to person A, or shoe B belongs to person A, or sunglass B belongs to person A, then yes should be returned. When I type in a query to check if a bag belongs to a person, it works well. But when I type in a query to check if a bag belongs to a person (and it should return no), I get the following error:
uncaught exception: error(existence_error(procedure,component/2),belongsTo/0)
For example:belongsTo(bag, wrongPerson) returns the error instead of No as it should.
Same thing happens if the query tests for a shoe or sunglasses instead of a bag.

How to store k-v relationship in Lucene?

Currently, I use indice to store a key-value relationship, which helps to do 'join' query between indices.
Minimum example may be like:
Indice A: field id, field xxx
Indice B: field id, field xxx
Indice C: field a, id of a; field b, id of b
I want to do query: Query of A and Query of B and a join condition that a.id = b.id.
Indice C help me to organize the query and optimize the query. After get the result of Query of A, I will look up the indice c, and then search the docs that with eligible id.
I tried to implements this join relationship with LongPoint.
Document doc = new Document();
doc.add(new LongPoint("a", idOfA));
doc.add(new LongPoint("b", idOfB));
This will contruct inverted indice to implement the relationship.
Actually, this may be not a good choice to implements k-v relationship of <Long, List<Long>>.
Is there any structure or anther option so that I can get a better performance of storage and queries.
Thank you.

Sort by key in Eloquent

This is my Matrix model:
Matrix -> Finding -> Norm -> Normtype
(finding_id) (norm_id) (normtype_id)
I'd like to fetch Matrixes, sorted first by finding_id, then by normtype_id.
From what I've read, orderBy only applies to keys in the actual model, such as:
public function findings() {
return $this->hasMany('App\Finding')->orderBy('finding_id');
}
How can I do this in Eloquent?
Just to clarify, a matrix has many findings, and each finding belongs to a norm, and each norm has a normtype.
When you put the orderBy call on a relation definition, you only change how the related models are sorted. If you want to sort by related model's attributes, you'll need to build a proper join yourself.
The following should do the trick in your case:
Matrix::join('findings', 'finding_id', 'findings.id')
->join('norms', 'findings.norm_id', 'norms.id)
->orderByRaw('finding_id, norms.normtype_id')
->get();

JDBC: select entities with Many to one relation

I have the two entity classes with bi-directional Many-to-one relation.
class A {
#Column(name="ID")
long Id;
}
class B {
#ManyToOne
#JoinColumn(name="A_ID")
A a;
}
The entities are well-coded with additional data fields and getters and setters. And now I want to construct a query string to fetch data from table B, where B's "A_ID" column is equal to A's "ID".
I tried something like this:
"select b.data1, b.data2 from B b, A a WHERE b.a.Id=a.Id"
But it does not work. What is the correct way to construct such a query? And if A and B are in a uni directional relation, would there be any difference?
Thanks in advance.
You don't need to join the tables, the whole idea behind #ManyToOne and #OneToMany is to do away with the need for most joins.
I refer you to a tutorial on JPA, like http://en.wikibooks.org/wiki/Java_Persistence/ManyToOne and http://en.wikibooks.org/wiki/Java_Persistence/OneToMany.
Now, without seeing your actual db definitions it's a bit difficult to guess the actual structure of your program and database, but it should be something like this:
class A {
#Id
#Column(name="ID")
long Id;
#OneToMany(mappedBy="a")
List<B> bees;
}
class B {
#ManyToOne
#JoinColumn(name="A_ID") // Note that A_ID is a column in the B table!!!
A a;
}
With the example above you could just select any list of B's you need, and JPA will automatically fetch the associated A for each found B. You don't need to do anything to be able to access it, b.a.Id will just work.
As we also have the OneToMany relationship, every A can have multiple B's associated with it. So, for any select that fetches a set of A's, each returned A's bees field will give access to the proper list of B objects, without the need to pull the B able into the query.

Delphi7 master detail relations query results in ORA-01036

I'm using Delphi7, Devart's dbExpress driver 4.70.
I drop two TSQLTables (call them A and B), two TDataSetProviders (dspA and dspB), two TClientDataSets (cdsA and cdsB), two TDataSources (dsA and dsB) and two DBGrids (gridA and gridB). Everything is set fine. If I set cdsA.Active to true I can see the data in gridA. The same per cdsB.
Now I want to implement the relation
A JOIN B ON a = b.
The field a is the true A's foreing key referred by B's field b and b is B's primary key too. I set the stuff as follow (I use graphic tools):
cdsB.MasterSource := dsA;
cdsB.MasterFields := a;
cdsB.IndexFieldNames := b;
When I do cdsB.Open, I got this error:
ORA-01036: illegal variable name/number".
The field a value is always null in table A (there is no data). TSQLMonitor reports the following queries:
Execute: select * from A
...
Execute: select * from ENTI where (b is NULL)
:1 (Number,IN) = <NULL>
What did I miss, and how can this be fixed?
When using Datasnap, you should set the M/D relationship on the source datasets, not the client ones. It will create a "dataset field" in the master client dataset. You then assign this field to the child client dataset. This approach is also more perfomant.
Anyway it should work as well, it looks there is something wrong with your SQL.

Resources