My CodeIgniter/DataMapper site needs to display a list of categories. Each category has a many-to-many relationship with itself, for multiple parents/children. I want to initially select only the categories with with no parents, or in other words, with no objects connected through a specified relationship.
I've run through a couple of possibilities (like including the related object count in the select and adding it to the where clause, which doesn't work because WHERE is evaluated before generating the column values in SQL), but to no avail.
Advice? Thank you.
Found the solution: compare the related objects ID to NULL. If it's an integer auto-increment column, no values will actually be NULL, but if there are no related rows in the join, it will come out as NULL.
$c = new Category();
$top_level_categories = $c
->where_related_parents('id IS NULL')
->get();
Related
I have a author table and a publication table. Both are related to each other in a many to many relation. When I'm inserting the publications the authors of the publications are inserted in the pivot table by the order of authors id. But I need to insert it by the order i'm selecting the authors in the front-end. Whatever the order of the authors in the front end is it is getting ordered by the author's id in the pivot table. How can i stop this automatic ordering
You can't add rows in a specific order into a pivot table, because it doesn't really make sense.
Let's consider an users table:
The first user you enter will have the id 1
The second will be assigned to the id 2
And so on...
So you can enter the users in a specific order and retrieve them by their id.
However, in a standard pivot table, the primary key is composed by two columns, in your case the author_id and publication_id. Nothing new is created here, you just associate the primary key of two existing rows in two differents tables in order to achieve one - and unique - composed primary key.
If i explained well (and i hope so :p), you should understand why saying
But I need to insert it by the order i'm selecting the authors in the front-end.
doesn't really make sense.
But, don't worry, it is still possible to achieve your goal here.
Instead of using a pivot table, you can use a normal table with an id. This way, the order of insertion will be preserved. It will work but that's not very nice.
A better approach would be to add an additional column to the pivot table. A column like position. This column could be incremented for each author you insert. Then, you can order the table by the position column, by simply adding ->orderBy('position') to your relationship or every queries that needs to.
Here is an example to illustrate what i said above:
foreach($authors as $position => $author)
{
$publication->authors()->attach($author, ['position' => $position]);
}
If $authors contains the authors in the order you selected them on the front-end, they will be added accordingly.
If you need to sync instead of attach, that's also possible, it's just a little bit more verbose:
$syncData = $authors->mapWithKeys(function($author, $position){
return [$author->id => ['position' => $position]];
});
$publication->authors()->sync($syncData);
Don't forget that you can add false as a second parameter on the sync method so it'll only add new authors.
After that, just change your authors relationship in your Publication model like this:
public function authors(){
return $this->belongsToMany(Author::class)->orderBy('position');
}
Or everywhere you need to:
$publication->authors()->orderBy('position')->get();
I hope it helps !
I've got a database structure as follows where item A is in a many to many relationship with B.
This is a true many to many, so:
Item X (object in table A) relates to Y (object in B) multiple times.
Using the laravel attach() function easily adds these records. However, I need to update and delete these items.
The detach() and sync() methods seem to not be appropriate. They assume only one pivot table entry between one record and another record.
If I detach() then ALL items are deleted. That is not what I want.
So the main point of my question:
If I have the id of the pivot table record, how do I delete/update it... given that detach doesn't work as expected.
Or put another way:
How can I interact with a pivot table object directly?
You can use newPivotQuery().
Something like that:
Parent::relation()->newPivotQuery()->where('id', $someId)->delete();
The where() and delete() statements will apply to the pivot query.
Thanks to all,
#devk has pointed out a good solution to the problem if you have the child objects available. I've upvoted this answer because it is true and useful for future reference.
Unfortunately, at this particular point I only have the many-to-many id, so i am answering this with a "No - there is no specific way to access the pivot table by id" using the Pivot or similar objects.
However: to be clear, this can still be done in eloquent using the DB class.
DB::table('many-to-many-lookup')
->where('id', $id)
->take(1)
->delete();
I have a database with a table articles and a table category.
My table articles have some fields. And one that is category_id and a another orientation.
Categories, in my design, are arranged by orientation. Here is an example
So, I would like to get all category BUT these categories must belong to the right orientation. I want list of all category who have articles with 'web' orientation, by example.
I do not know if it's possible with this architecture and if you understand me.
Any help is welcome
UPDATE : adding the schemas
If you have your models and relations set up, I believe this should do what you want:
$categories = Category::whereHas('articles', function($query) {
$query->where('orientation', 'web')
})->get()
From the Laravel documentation:
If you need even more power, you may use the whereHas and orWhereHas
methods to put "where" conditions on your has queries. These methods
allow you to add customized constraints to a relationship constraint,
such as checking the content of a comment
This is a problem I've had for a while and not sure what is the best way to solve it.
Its probably easier for me to explain using an example.
So I have many collections and many items. Some items can belong to one or more collections or none at all. Collections must have at least one item but there is no limit to the number of items that it can have.
How do I store this information in a MySQL database / what is the most efficient way of doing it?
I already have a table of items with a unique ID per item, thats the easy part, creating the collections part is where I'm stuck.
Do I create a new table for every collection, give it a unique table name and insert new records for each item that belongs in that collection?
Do I create just one large collection table with each record being a new collection and a single field of all the item ids?
Is there another way of doing it?
This will be queried using a php site if that makes any difference to the database structure.
I think you should..
Create a collection table with all collections, an item table with all items, and a collection-item map table which will have id(primary key), collection_id (foreign key to collection table), item_id(foreign key to item table). This table will represent the relation between a collection and item. It can have any number of collection item relation.
Hope this helps.
I have some problem while listing categories from database.
First i have a table called "Videos" where i store som videos-information like v_name, v_description and category_name.
In the second table called "Categories" where i store categories-information like c_name and c_description.
OFC i have id's in every table :)
But now i want to list the categories and in the same query count every videoitem in every category.
This is the code and i can't figure out how to do in the model now and later how to show the numbers in the view file, so pleace help me!
Thanks for your time and support :D
$this->db->select('c.*');
$this->db->from('categories as c');
$this->db->join('videos as v', 'c.c_name = v.v_category', 'right');
return $this->db->get()->result_array();
For your code to work you need two changes:
First you join type should be a "left join". Than way you still will get a count result (0) even if a category has no videos yet.
Second you need to group your results to be able to use the aggregate function count().
Try with this:
$this->db
->select('categories.c_name, COUNT(videos.id) as num_videos')
->from('categories')
->join('videos', 'categories.c_name = videos.v_category', 'left')
->group_by('categories.c_name');
Also you should reconsider your DB design. If you have id columns in both tables (wich I assume are the primary key) then you should define the relationship between the tables (foreign keys) using the id column, not the name.