Save many to many relationship in datamapper ORM - Codeigniter - codeigniter-datamapper

I am a beginner. Here I would to ask all of you that how can I save many to many relationship in to the three tables(table A , table A-b , table B)? Now I am trying to save a new record with new ID into table A, and I have some IDs of table B that I want to save them to the middle table A-B depend on ID of table A. If anyone have experiences with this, please kindly share.
Example:
$a = new modelA();
$a->name = ‘new name’;
$a->des = ‘something to say’;
$b = new modelB();
$IDs = new array(1,2,3); //IDs of records in table B
$a->save(array($IDs=>$b));

Passing ID's is not supported, Datamapper needs objects to be able to relate.
If you have an array of ID's, you can fetch the objects using an where_in() query, and then save the relation using
$a->save($b->all);

I have some values which get from post form like:
'new name'
'new description'
array('val1','val2','val3')
in my controller I want to save a new record to table A with:
- 'new name'
- 'new description'
and array('val1','val2','val2') I got IDs of them from table B which has many to many with table A(a new record going to save).
So when I save into table A, the IDs of table B also save to middle table A-B with new ID of table A I just save. How to save?

Related

Find the same and replace it

There are 2 tables:
Now I want to query the title of the author table for the same value as the barcode field in the holding table. Then? Add the value of Auth in author to callNo in holding .
This is what I wrote:
update holding h set
h.CALLNO = (select a.author
from AUTHOR a
where a.TITLE = h.BARCODE)
This statement is wrong, it transformed my column into null.

Is it possible to join eloquent relationship query?

I have a users, departments, and positions table.
I want to select one department with all the users in that department with their position name.
Currently, I have this.
$department = DepartmentView::with('users')
->findOrFail($departmentId);
It returns me a department with users, but I want to join the users with positions table so I can also get the position name. (user table only has position id)
Assuming you have your relationships setup properly in your User model and the relationship is called position, it should be like this:
$department = DepartmentView::with('users.position')
->findOrFail($departmentId);
Look at eager loading -> nested eager loading.
You can do
$department = DepartmentView::with('users.position')->findOrFail($departmentId)
position is referred to the relationship set on the User model to get the user position.

Laravel Eloquent select function cause empty relation

Following is my query
$user = User::select(['uuid','name','about'])->with(['education','work'])->first();
this returns empty data for relationship education and work,
but if I remove select function from query I am getting data in relationship and it also returns all columns of user table which I don't want.
how can solve this problem
The problem is that relationships (with(...)) execute an additional query to get the related results. Let's say you have one to many relationship where users have many works. User::with('work')->find(1) will then execute these 2 queries:
select user where id = 1 and select works where user_id = 1.
So basically in order to be able to execute the second query (fetch relationship data) you need to include id (or whichever column you're referencing) in you select statement.
Fix:
$user = User::select(['uuid','name','about', 'id'])->with(['education','work'])->first();
Same principle in different forms applies to all relationships. For example in the inverse of hasMany which is belongsTo you would need to select the foreign key (for example user_id).

Insert and delete from "associations table on EF model"

I have to tables "ALUMNOS" and "MATERIAS". In SQL exist another table "ALUMNOS BY MATERIAS". I know this is not necessary in EF model, because exists the properties navigation. But how can insert or delete from this " associations table"??
Thanks in advance!!
Guille
The only way would be to remove the link between the Materia and Alumno objects before saving again. This way they are individual objects or only contain links that are required. This will set the mapping table correctly.
Created the tables and got the objects like you have got. To insert a new row, I do the following
StackExchangeExampleEntities exchangeExampleEntities = new StackExchangeExampleEntities();
Alumno alumno = new Alumno() {AlumId = 2, Description = "aldesc"};
Materia materia = new Materia() {materia_Id = 2, description = "mdesc"};
materia.Alumnos.Add(alumno);// attach the relationship
exchangeExampleEntities.Materias.Add(materia);
exchangeExampleEntities.SaveChanges();
To Delete the mapping table, I clear down the collection in one side ( you can use either collection - Alumno or materia)
StackExchangeExampleEntities exchangeExampleEntities = new StackExchangeExampleEntities();
Materia materiaFetched = exchangeExampleEntities.Materias.Single(m => m.materia_Id == 2);
materiaFetched.Alumnos.Clear();
exchangeExampleEntities.SaveChanges();
If you just want the mapping table to be used, then add a new entityframework file and then select only the mapping table( dont select any thing else). This will create entity just for this mapping table in the context.
Now you can use it like this:
StackExchangeExampleEntities1 exchangeExampleEntities1 = new StackExchangeExampleEntities1();
MateriaAlumono s = exchangeExampleEntities1.MateriaAlumonoes.First();
Console.WriteLine(s.Alumuno_id);

Getting latest record for each userid in rails 3.2

I have user with name, location, created_at as important fields in table.
I want to retrieve for each user the latest location,i.e, I want something like this:
username location created_at
abc New York 2012-08-18 16:18:57
xyz Mexico city 2012-08-18 16:18:57
abc Atlanta 2012-08-11 16:18:57
only input is UId(1,2) array of userids.please help me to accomplish this.I just want to know how to write query using active record query interface.
Generally, this should be a standard way to solve this kind of problems:
SELECT l1.user, l1.location
FROM locations l1
LEFT JOIN locations l2 ON l1.user = l2.user AND l2.created_at > l1.created_at
WHERE l2.id IS NULL
The idea is to join the table with itself, and find those rows which don't have any row with the same user and greater created_at.
Of course, you should have (user, created_at) index on your table.
Now you should see how would that be represented in AR interface.
When
u_id
is the array of user ids, then
u_id.map{|i| User.find(i).location}
should be an array of the users locations.
You can Use
User.where(:uid => [1,2,3]).maximum('location')
which will create something like
SELECT MAX(`users`.`location`) AS max_id FROM `users` WHERE `users`.`id` IN (1, 2,3)

Resources