Displaying data from multiple entities in a single NSTableView (Core-Data) - cocoa

I have an entity called Pupil, and an entity called Loan.
The Pupil entity has the attributes: firstName, lastName, address, postCode, telephoneNumber.
The Loan entity has the attribute: loanID, and the relationship: pupilID, which is a relationship to the entity Pupil.
I wish to display the loanID, with the pupil (if any) related to the loanID in a table.
e.g.:
LoanID | firstName | lastName | address | postCode | telephoneNumber
1 | bob | smith | 98 Any Road | N1 1QW | 0123456789
How would I go about this? I am currently using the bindings for my other tables.
Thank you!

You can do this assuming that you have set the reverse relationship for pupilID (i.e. a relationship from Pupil to the Loan). If you call that relationship loan, and have an NSArrayController, PupilsController bound to the collection of Pupils, then your first table could be bound to PupilsController.arrangedObjects.loan.loadID and your other columns bound as you'd expect.
On a purely stylistic side note, the pupilID property would more appropriately be named pupil. Core Data is no an ORM and you're not in SQL JOIN land any more. Name the properties what they are, not how they're implemented under the hood by Core Data.

Related

Laravel groupby to get sum but only based on another columns value

I have multiple joins that I am doing to get one list. I can group by a certain value to get a sum but need to merge another column that is not being grouped by. My tables look kinda like this (simplified)
Quote Table
id|Name |Location |
---------------------------------
1|My First Quote|123 Main Street|
Quote Items table
quote_id| item_id
--------------------
1 | 1
--------------------
1 | 2
Items Table
id|Name |Quantity|Type
--------------------------------
1|Dog Food|2 |product
2|Delivery|1 |service
I need to show a final result like this in datatables
Name Location Qty Item
My First Quote | 123 Main Street | 2 | Dog Food
So basically i need to sum on item.quantity and show the product type as item. I have the group by working with a raw sum on the item grouped by item.id where item type is product but the where gets rid of the item service that i need to show on the row.
You can try this:
DB::table("Quote")
->select('Quote.Name','Quote.Location')
->(['Items.Quantity AS Qty','Items.Name AS Item'])
->join("Items","Items.item_id","=","Quote.quote_id")
->get();
You can define a relationship for quotes table
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Quote extends Model
{
public function items()
{
return $this->belongsToMany(Item::class);
}
}
after that, call a sum method with that relation for the column you want.
Quote::withSum('items','Quantity')->get();

How can I remove duplicates results when using belongsToMany

Having the following tables:
----------- ----------------- ---------------
| PROJECT | | ACCESSES | | ENVIRONMENT |
----------- ----------------- ---------------
| id | | id | | id |
| title | | project_id | | title |
----------- | environment_id| ---------------
| username |
| password |
-----------------
My goal is to get all the environments used by a project through the accesses table
In my Project model:
public function environments(){
return $this->belongsToMany('App\Models\Environment', "accesses");
}
My problem is that if I have multiple rows with the same project_id and environment_id values in the accesses table, it will fetch multiple time the same environment.
How may I force it to retrieve each environment only once?
This is an old question, but the benefit of future travellers:
The distinct() method can help in this situation:
public function environments() {
return $this
->belongsToMany('App\Models\Environment', "accesses")
->distinct();
}
From the docs:
The distinct method allows you to force the query to return distinct results:
$users = DB::table('users')->distinct()->get();
As far as I can tell, this works at least as far back as Laravel 4.x, so it should be fine for all currently supported versions.
You can achieve this using sync method or toggle method it depends on your use case.
From the docs:
The many-to-many relationship also provides a toggle method which "toggles" the attachment status of the given IDs. If the given ID is currently attached, it will be detached. Likewise, if it is currently detached, it will be attached:
$project->environments()->toggle([1, 2, 3]);
You may also use the sync method to construct many-to-many associations. The sync method accepts an array of IDs to place on the intermediate table. Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the given array will exist in the intermediate table
$project->environments()->sync([1, 2, 3]);
For more information please have a look in the docs.
https://laravel.com/docs/5.6/eloquent-relationships#updating-many-to-many-relationships

How to create a form that contains data for multiple entities?

I've been trying to think of the best possible way to submit data for multiple entities in a single form.
My use case is that I have a table that contains multiple rows of different entities. I want the ability to go into 'edit' view where some of the columns will become textfields and allow for the value to be changed.
Example
Type | Name | Age
Animal | Eevee | 4
Animal | Sandy | 7
Human | Bob | 24
[Submit]
In edit mode they would be able to change the age for any of the entities. When the user hits save button for the entire list, I want the form to submit all the changes. The problem is that in the update method, I need to know which entities to update with which values
I was thinking of using name attribute to store the entity id of each entity on the input
<input name="{{$entity->id}} value="{{$entity->age}}"></input>
but Animals and Humans are stored in different DB tables. So in the update method of the controller I would have to take the name (which would be the id of the entity) and if it exists as a human
entityToUpdate = Human::find(id)
if entityToUpdate does not exist
entityToUpdate = Animal::find(id)
We use UUIDs in our DB so this would work but it doesn't feel right.
is there a way to group a couple inputs together in a form?
group1
type=animal
id = (Sandys ID)
age = 8
group2
type=human
id =(bobs ID)
age = 25
You could use array notation in your input names in order to get what you want on the server side:
#foreach($entities as $entity)
<input type="hidden" name="group[{{$loop->index}}][type]" value="{{$entity->type}}"/>
<input type="hidden" name="group[{{$loop->index}}][id] value="{{$entity->id}}"/>
<input type="text" name="group[{{$loop->index}}][age] value="{{$entity->age}}"/>
#endforeach
Then on the server side:
foreach(request()->get('group') as $entity) {
// $entity contains ['id'], ['type'], ['age']
}

Laravel's Eloquent. One/many to many of different models (types)

I've got a model that can be related to arbitrary number of items (other models). What is the best way to retrieve those as an array/collection of items of their types, e.g.
$basket = Basket::find(1);
dd($basket->items); // [Banana, Yoghurt, Bread, Bread, Ham, Cheese, Cheese]
Table: basket
| id | int |
| user_id | int |
Table: basket_items
| id | int |
| basket_id | int |
| item_id | int |
| item_type | string |
Models: Basket, Banana, Yoghurt, Bread, Ham, Cheese
Where I've got so far is: I can not use Eloquent's relations here since items method is referencing multiple models. The question is: can I (probably don't see how)?
As a plan-B I'd just implement that Basket::items method issuing a query and then fetching each and every model and hydrating them. I wonder if there a better way of dealing with that.

nested Rowkey in Hbase tables

i have a weather data base with 4 tables : province,city,station, instantHarvestinfo,dailyHarvestInfo
and the relation between tables is parent-child:
(province,city): R(1,m)
(city,station):R(1,m)
(statin,istantharvestInfo):R(1,m)
(station,dailyHarvestInfo):R(1,m)
i want put all of them in one bigtable in hbase and for echa one create a column family..but i dont know how define my row key...i think i need a nested row key that in each step get a split of my rowkey that related a comuln family and give me information of same cf..but how i cant define it?
please help me
there.
I guess you are going to save huge amount of istantharvestInfo and dailyHarvestInfo for each station.
Since there is parent-child relationship in your data model, I think you could
design the schema as:
-------------------------------------------------------------------------
**Row-Key**: Province + city + station + timestamp
--------+---------------------+------------------------------------------
Family | Qualifier | Value
--------+---------------------+------------------------------------------
| istantharvestInfo | "value of istantInfo"
F +---------------------+------------------------------------------
| dailyHarvestInfo | "value of dailyInfo"
--------+---------------------+------------------------------------------
Note that there is only one Family, because we should always make #family as small as possible.

Resources