There are two related tables A and B. The relation is one to many - A may point to the multiple entities in B.
Example:
Table A
id | title
--------
1 | 'title 1'
2 | 'title 2'
Table B
id | id_from_a
--------
1 | 1
2 | 1
I want to find all entities in table A that do not have anything in table B using graphQL.
For time being I am using the following contraption:
query getData {
table_a(where: {_not: {table_b: {id_from_a: {_neq: -9999}}}}) {
id
title
}
}
It works, but it is ugly as hell, so I guess there should be some better graphql solution.
Related
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
I have two tables: 'tours' and 'tourists' in my DB.
A 'tour' can have MANY 'tourist's, and 'tourist' can participate in MANY 'tour's.
So, I have successfully related them via 'begonsToMany" relationship (when adding rows to "tours' and 'tourists" an intermediate table 'tour_tourist' is given a row also.
"tour_tourist" table structure is:
tour_id | tourist_id
1 | 37
1 | 38
1 | 39
(tourists with id's 37, 38 and 39 go are assinged to the tour with id = 1)
THE PROBLEM : One of 'tourists' (37 or 38 or 39 from example abouve) can be a 'buyer' of the 'tour' (1 from above table). So only 1 person pays for the tour. Also this person can go to the tour/only pay for it. I pass this data (tour, tourists, who is buyer and does he goes to the tour) when submitting a tour via web-form.
So I want to create a 3rd table, called 'buyers' which represent a "tourist" who pays for the tour:
tour_id | tourist_id | is_tourist
1 | 39 | 0
(1. one 'tour_id' can match only ONE 'tourist_id')
(2. 'tourist_id' should be one of those who 'belong' to the 'tour_id' in the previous "tour_tourist" table - tourist 39 belongs to tour 1 - see table above).
(3. 'is_tourist' is 1/0, means the buyer goes to the tour/only pays, in my example it's 0 - he only pays, doesn't go).
I am wondering how can I do this using Eloquent relationships (one-to-one? hasManyThrough?) .
Appreciate any help.
Probably a simpler approach for that is to create two other columns called is_payer and is_tourist in your tour_tourist table in order to avoid creating your third table.
The result of your tour_tourist table could be something like this:
tour_id | tourist_id | is_payer | is_tourist
1 | 37 | 1 | 0/1
1 | 38 | 0 | 1
1 | 39 | 0 | 1
Then with eloquent you can simply access your extra columns' using withPivot method chaining your belongsToMany method.
For example, your Tour model can be something like this:
Tour
class Tour extends Eloquent {
protected $table = 'tours';
public function tourists() {
return $this->belongsToMany('Tourist')->withPivot('is_payer', 'is_tourist');
}
}
In the result you will have is_payer & is_tourist.
You can then filter them out using Collection methods.
I am trying to the count of number of groups in my report I know I could do it in the SQL however trying to avoid adding redundant data to my dataset if I can.
I have a MainDataSet that could have multiple entries per distinct group item. All I want is the no. of groups not the count of items within the group.
For example words starting with alphabet letters, lets say I have 2 groups A and B only (NB: number of groups can change dynamically as I filter the MainDataSet based on user parameter selection):
Group | Data
------|-----
A | Apple
A | Ant
B | Balloon
B | Book
B | Bowl
Final Result:
Group | Index | NGroups
A | 1 | 2
B | 2 | 2
I know I can get the Index using a aggregate function as follows:
RunningValue(Fields!Group.Value, CountDistinct, "TablixName")
But how do I get the NGroups value?
I guess I could also create another dataset based on the MainDataSet (make use of a sql function) and do:
SELECT 'X' AS GroupCount, COUNT(Distinct Group) AS NGroups
FROM dbo.udf_MainDataSet()
WHERE FieldX = #Parameter1
Then use a LookUp:
Lookup("X", Fields!GroupCount.Value, Fields!NGroups.Value, "NewDataSet")
But is there a simple solution that I am not seeing?
CountDistinct(Fields!Group.Value, "TablixName")
This question is about the Laravel style of doing things:
Everything in Laravel can be done in an elegant way.
I currently have a many to many relationship between mongrels and breeds through a table named breed_mongrel but this table also has a certainty value describing how certain the dog is that he's indeed a mix of that specific breed.
mongrel_id | breed_id | certainty|
----------------------------------
| 1 | 4 | 50 |
| 1 | 2 | 25 |
| 2 | 5 | 75 |
this means that mongrel#1 is 50% sure she's of breed#4 and 25% sure she's also a mix with breed#2. However mongrel#2 is a whooping 75% certain he is breed#5.
My question is what is the elegant way to add records to this table?
I could use something like this:
DB::table('breed_mongrel')->insert(
array('mongrel_id' => 3, 'breed_id' => 5, 'certainty' => 37));
But in Laravel I would normally do things more eloquently like:
$relation = new Relation;
$relation->breed_id = 1;
$relation->save();
But because it is a relationship and I can't follow the normal model named in singular camel caseform and DB table named in plural lower (snail) case I'm not sure what is Laravelly way to add entries to this database table?
You can use attach. It also accepts a second parameter allowing extra values to be added to the pivot table.
$mongrel = Mongrel::find(1);
$breed = Breed::find(4);
$mongrel->breeds()->attach($breed, array('certainty' => '50'));
I am developing a custom module for a project. I created a custom form and that custom form's data saved in two tables. Now when form open in edit mode I am not able to get saved data from both tables. I have no idea how can I get resolve this issue, please help me.
Here are my two tables structures:
Table1-
-------------------------
id | page_id | title
1 | 3 | ABC
2 | 4 | PQRS
3 | 10 | XYZ
Table2-
--------------------------------
id | page_id | child | position
1 | 3 | 8 | left
2 | 3 | 7 | right
3 | 3 | 15 | right
4 | 4 | 14 | right
5 | 4 | 15 | left
6 | 10 | 15 | left
--------------------------------
Here i am attaching a screen-shot to more explain myself. I want to selected saved option values in 'left' & 'right' text-area in edit mode, values comes from table2.
Please suggest me. Thanks in advance.
Left and Right are here multiselect field types and these kind of fields receives values in comma separated string. so the example you presented will work in this way.
lets consider you have models Table1 and Table2 and you pass your Table Model from edit action in you controller you have written
$table1Id = $this->getRequest()->getParam('id');
$table1Model = Mage::getModel('page/table1')->load($table1Id);
if ($table1Model->getId()) {
Mage::register('page_data', $table1Model);
...
In your form file Block/Adminhtml/Edit/Tab/Form.php there is method $form->setData()
if ( Mage::getSingleton('adminhtml/session')->getPageData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getPageData());
Mage::getSingleton('adminhtml/session')->setPageData(null);
}
elseif ( Mage::registry('page_data') ) {
$values = Mage::registry('page_data')->getData();
$values['left'] = '8';//You can get this value from Table2 collection on basis of $values['page_id'] you got
$values['right'] = '7,15';////You can get this value from Table2 collection on basis of $values['page_id'] you got
$form->setValues($values);
}
I found the solutions of my query.
This extension is the best example click here
To get resolve this need to edit many files like model, resource and block files. That's why I mentioned this link to understand complete process.
most file that are need attention are :
app/code/community/[PackageName]/[ModuleName]/Model/Resource/Pagesummary.php
app/code/community/[PackageName]/[ModuleName]/Model/Pagesummary.php
app/code/community/[PackageName]/[ModuleName]/Block/Adminhtml/Cms/Page/Edit/Tab/Pagesummary.php
hope this helps others!