Retrieving data from referenced key table - Laravel-4 - database

The structure of concerning tables is as follows (MySQL):
//Table Name : team
tid PK
team_name (varchar)
//Table Name : fixture
fid PK
home_team_id FK |_ both referenced to 'tid' from 'team' table
away_team_id FK |
My aim is to retrieve the team names. Considering this structure, I think I'll have to retrieve home_team_id and away_team_id and then do something like
Fixture::where('tid','=',$home_team_id)->get();
My question is, is this the correct way to accomplish what I aim to do?
and
should this be done from the controller? (if so, then I'll have to do two queries from same function)

First, rather than having your primary keys be tid and fid, just keep them both as id. This is not only best practice, but will allow you to more easily use Laravel's Eloquent ORM as it by default assumes your primary key column is named id.
Second thing, make sure your table names are in plural form. Although this is not necessary, the example I'm about to give is using Laravel defaults, and Laravel assumes they are in plural form.
Anyway, once you've 'Laravelized' your database, you can use an Eloquent model to setup awesome relationships with very minimal work. Here's what I think you'd want to do.
app/models/Team.php
class Team extends Eloquent {
// Yes, this can be empty. It just needs to be declared.
}
app/models/Fixture.php
class Fixture extends Eloquent {
public function homeTeam()
{
return $this->belongsTo('Team', 'home_team_id');
}
public function awayTeam()
{
return $this->belongsTo('Team', 'away_team_id');
}
}
Above, we created a simple model Team which Laravel will automatically look for in the teams database table.
Second, we created model Fixture which again, Laravel will use the fixtures table for. In this model, we specified two relationships. The belongsTo relationship takes two parameters, what model it is related to, in both cases here they are teams, and what the column name is.
Laravel will automatically take the value in away_team_id and search it against the id column in your teams table.
With just this minimal amount of code, you can then do things like this.
$fixture = Fixture::find(1); // Retrieves the fixture with and id of 1.
$awayTeam = $fixture->awayTeam()->first(); // var_dump this to see what you get.
$homeTeam = $fixutre->homeTeam()->first();
Then you can proceed as normal and access the column names for the tables. So say you have a 'name' column in the teams table. You can echo out the the home team name from the fixture like so.
$fixture = Fixture::find(1); // Get the fixture.
echo $fixture->homeTeam->name;
It's nearly 2AM, so there might be an error or two above, but it should work.
Make sure you check the docs for Eloquent, especially the bits relating to relationships. Remember to name your columns and tables in the way Laravel wants you to. If you don't, there are ways to specify your custom names.
If you want to get even more fancy, you can define the inverse relationship like this on your Team model.
app/models/Team.php
class Team extends Eloquent {
public function fixturesAtHome()
{
return $this->hasMany('Fixture', 'home_team_id');
}
public function fixturesAway()
{
return $this->hasMany('Fixture', 'away_team_id');
}
}
Then to get all of a particular team's home fixtures...
$team = Team::find(1); // Retreive team with id of 1;
$homeFixtures = $team->fixturesAtHome();

Related

How to retrieve a model and it's relationship as a single Array or Object | Eloquent, Eloquent: Relationships

i have 3 tables with one-to-one relationship. The phone table has one to one relationship with Model table and Model table has a one to one relationship with Manufacturer table.
phone_table
id
imei
image
model_id
model_table
id
name
image
manufracturer_id
manufracturer_table
id
name
logo
how to get a result like this :-
App\Phone{
imei : "356554512522148",
model : "Galaxy S-10",
manufracturer : "Samsung",
}
I would never throw it into the same array / object, i would firstly do that on transformation. If you use default Laravel transformation you can use getters for it. Simple example on how to access these fields into the same context would be.
$phone = Phone::with('model.manufactor')->find(1);
With secures the queries are optimal for accessing it. How to get data into same layer.
[
'imei' => $phone->imei,
'model' => $phone->model->name,
'manufactor' => $phone->model->manufactor->name,
]
For this to work, you need relations in your model too.
Phone.php
public function model()
{
return $this->belongsTo(Model::class);
}
Model.php
public function manufactor()
{
return $this->belongsTo(Manufactor::class);
}
Just join them:
\App\Phone::leftjoin('model_table AS mo', 'mo.id', '=','phone_table.model_id')
->leftjoin('manufracturer_table AS ma', 'ma.id', '='. 'mo.manufracturer_id')
->selectRaw('phone_table.imei, mo.name AS model, ma.name AS manufracturer')
->first()
And sometimes you need to think about why you want to split table to one-to-one relationship.
Is there a table not usually be used, or one of them need to be connected by another tables. is this just for saving space or reduce IO cost.
If there are not any other reason and you always need to get these tables' information, maybe you can merge to one table.

retrieve name from the other table

Is there an efficient way of retrieving the name by using select and join clause? I have a Note, NoteType and NoteStatus model. There are type and status field which will be stored as integer (representing the id of its respective model) inside Note model. NoteType and NoteStatus models have id and name fields.
foreach($notes as $note)
{
$type=NoteType::where('id',$note->type)->first();
$note->type=$type->name;
$status=NoteStatus::where('id',$note->status)->first();
$note->status=$status->name;
}
Model Relations
Setting up relations between your models would be the best way as you then don't need to re-invent the wheel each time you need to call the join. It will save you code in the long run.
More info on this here:
Laravel Eloquent Relationships
Query Builder
If you want to do this manually then it would be the same as if you ran the query in raw SQL:
$note = Note::join('NoteType','Note.NoteType_id','NoteType.id')
->select('Note.*','NoteType.Name as NoteName')
->first();
Now you can get all the info from $note
Note id = $note->id
NoteType Name = $note->NoteName
Obviously adjust this to your code but this should help you build your knowledge enough to work it out.
More info can be found here:
Laravel Query Builder Joins
Assume that Your model name is Note.php
assume in your notes table has note_status_id and note_type_id foreign key
Add Relationship in your main model Note.php
public function status()
{
return $this->belongsTo(NoteStatus::class);
}
public function notes()
{
return $this->belongsTo(NoteType::class);
}
You can retrieve data with relationship something like that
Note::with('status','notes')
->get()
For more info regarding laravel relationship Laravel Eloquent: Relationships

grails one to many relationship: no entries created on database

I have two domain classes in which one has a one to many relationship with the other
Class A
{
...
#NotNull
static hasMany = [bElements:B]
}
Class B
{
...
}
When I run the application, the relation table A_B is created and entries in A_B table are automatically added when user creates A objects. Then I've decided to change this relation, because I've noticed that it is better to have a relation between class A and class C, so class A now has
static hasMany = [cElements:C]
but when I create a new object of type A (after creation of some C objects), adding one or more objects of type C, in my database I don't see the entry into the A_C table, but only in A table.
Why do this beahavior happens? What must I control to resolve problem?
EDIT:
maybe it is needed some clarifications. The Class A is a class that describes an invoice and the class C is a class that describes the invoices items. So I need to give a one-to-many relationship between this two classes, but as described above, it does not work as expected...
EDIT 2:
I've noticed that maybe the problem depends on the fact that the field cElements in A object is null. In the view, I've described the cElements field as follows:
<g:select name="receiptItems" from="${HealthService.findAllByDoctor(Doctor.findBySecUser(new ReceiptController().getCurrentlyLoggedUser()))}"
multiple="multiple" optionKey="id"
optionValue="${{it.healthServiceType.healthService}}"
size="5" value="${receiptInstance?.healthServices*.id}" class="many-to-many"
onchange="${remoteFunction(
controller: 'Receipt',
action: 'sumReceiptItems',
params: '\'receiptItemsSelected=\' + jQuery(this).val()',
onSuccess: 'updateTotalAmount(\'totalAmount\', data, \'00000\')')}"/>
It is a multiple select. After each selection, with the remoteFunction, a method from controller is called to do some calculation and update the totalAmount field. It works well but, when save method is called, healthServices field is null...and I don't understand why...I will open another post to solve this issue (solved here)
If you declare a class like
Class A
{
...
#NotNull
static hasMany = [cElements:C]
}
Class C
{
static belongsTo= [a:A]
...
}
In this case it does not create A_C but if you declare it like
Class A
{
...
#NotNull
static hasMany = [cElements:C]
}
Class C
{
//no belongTo
...
}
then it creates A_C in database to map these fields id.
There is no need to have an intermediate table with A-B relations when you have one-to-many relation esablished. If relation was bidirectional (B class objects could have multiple A class objects) then the intermediate table would be useful.
Check your databse whether your B class objects contain pointers (foreign keys) to A class objects. If they do, your ORM decided to create one-to-many relationship and your A-B relations table is not used.
I would ditch the intermediate table for now and add the following to B class
static belongsTo = [parent:A]
(keep the hasMany in A):
This will create a bi-directional relationship from B to A (aka foreign key in B table). Make sure you are conscious of how cascading deletes are handled with belongsTo.
http://grails.org/doc/2.2.x/ref/Domain%20Classes/belongsTo.html
You mentioned pre-populating. Make sure you aren't violating any constraints. Bootstrap often fails silently. Add something like on your instance in question:
`
if (!b.save()) {
b.errors.each {
println it
}
}
`
After you get this relationship working, take a look at this talk if you need to refactor your relationship for gorm performance using an intermediary table. http://www.infoq.com/presentations/GORM-Performance

objectify query filter by list in entity contains search parameter

in an app i have an entity that contains a list of other entities (let's say an event holding a list of assigned employees)
using objectify - i need to find all the events a particular employee is assigned to.
is there a basic way to filter a query if it contains the parameter - kind of the opposite of the query in
... quick pseudocode
findAll(Employee employee) {
...
return ofy.query(Event.class).filter("employees.contains", employee).list();
}
any help would be greatly appreciated
i tried just doing filter("employees", employee) after seeing this http://groups.google.com/group/objectify-appengine/browse_thread/thread/77ba676192c08e20 - but unfortunately this returns me an empty list
currently i'm doing something really inefficient - going through each event, iterating through the employees and adding them to a new list if it contains the given employee just to have something that works - i know this is not right though
let me add one thing,
the above query is not actually what it is, i was just using that because i did not think this would make a difference.
The Employee and Events are in the same entity group with Business as a parent
the actual query i am using is the following
ofy.query(Event.class).ancestor(businessKey).filter("employees", employee).list();
unfortunately this is still returning an empty list - does having the ancestor(key) in there mess up the filter?
solution, the employees field was not indexed correctly.
I added the datastore-indexes file to create a composite index, but was testing originally on a value that I added before the employees field was indexed, this was something stupid i was doing - simply having an index on the "business" field and the "employees" field fixed everything. the datastore-indexes file did not appear to be necessary, after deleting it and trying again everything worked fine.
Generally, you do this one of two ways:
Put a property of Set<Key<Employee>> on the Event
or
Put a property of Set<Key<Event>> on the Employee
You could also create a relationship entity, but if you're just doing filtering on values with relatively low counts, usually it's easier to just put the set property on one entity or the other.
Then filter as you describe:
ofy.query(Event.class).filter("employees", employee).list()
or
ofy.query(Employee.class).filter("events", event).list()
The list property should hold a Keys to the target entity. If you pass in an entity to the filter() method, Objectify will understand that you want to filter by the key instead.
Example :
/***************************************************/
#Entity
#Cache
public class News {
#Id Long id;
String news ;
#Index List<Long> friend_list = new ArrayList<Long>();
// My friends who can see my news , exemele : friend_list.add(id_f1); friend_list.add(id_f2); friend_list.add(id_f3);
//To make an operation on "friend_list", it is obligatory to index it
}
/*************************************************/
public News(Long id_f){
List<Long> friend_id = new ArrayList<Long>();
friend_id.add(id_f);
Query<Nesw> query = ofy().load().type(News.class).filter("friend_list in",friend_id).limit(limit);
//To filter a list, just after the name of the field you want to filter, add "IN".
//here ==> .filter("friend_list in",friend_id);
// if friend_list contains "id_friend" ==> the query return value
.........
}

Entity Framework 4.0 Many-Many relationship Using Non-Primary Key

I am trying to build a generic repository using Entity Framework 4.0 using a legacy MS SQL database I have inherited. A pretty familiar scenario.
I need to add category information to a fairly long list of existing items.
The items can belong to several categories at the same time so I created an mapping table called CategoryMapping
Unfortunately SchemaDefinitionCode is not unique and cannot be made into a Foreign Key (FK) in the database.
I have tried to add my own partial class to the DefinitionSchema entity but as it's not indexed, this has a severe performance hit. Demo code for testing, I won't want to create a new context every time I load this:
public partial class DefinitionSchema
{
private MyEntities context;
public IQueryable<Category> Categories
{
get
{
context = new MyEntities();
var categories = context.Categories
.Where(c => c.CategoryMappings
.Where(m => m.SchemaDefinitionCode == this.SchemaDefinitionCode).Any());
return categories;
}
}
}
I can then call a list of items like so:
var q = context.SchemaDefinitions
.Where(s => s.Categories
.Where(c => c.Name == category)
.Any()
);
How can I link my tables and mapping in the most efficient manner without wiping out the existing database structure?
It can't work this way because EF doesn't support unique keys and SchemaDefinitionCode must be unique to form valid many-to-many relation with Category. If your SchemaDefinitionCode is not unique in DefinitionSchema table it can't be used as principal end in the relation with CatagoryMapping. If it is unique you can use SchemaDefinitionID instead because no more then one Id will have the same code value.

Resources