How to sort records in Composite object? - entity-framework-4.1

Suppose I have 2 tables in DB: Student, StudentCourse.
In meta data for Student, set StudentCourse as Composite:
[Include]
[Composition]
public EntityCollection<StudentCourse> StudentCourses { get; set; }
In Domain service for Student, inlcude StudentCourse like:
public IQueryable<Student> GetStudentByID(int id)
{
reutrn this.ObjectContext.Students.Include("StudentCourse").Where(s => s.ID == id) as IQueryable<Student>;
}
Question is: I want to records of StudentCourse can be sorted by a column from StudentCourses, for example, CourseID.
records of StudentCourse under one student is actually an entity collection of StudentCourse.
How to resolve this problem?

What you're asking is impossible to do even in plain SQL.
You can try if your provider will support somthing like that
this.ObjectContext.Students.Include("StudentCourse").Where(s => s.ID == id).OrderBy(x=> x.StudentCourse.First().CourseId) as IQueryable<Student>

Related

fetching data from three tables in laravel

The is the table structure,which basically has three tables,namely expenses,categories and sunbcategories
table expenses
(id,category_id,sub_category_id,date,description,amount)
table categories
(id,category_name)
table subcategories
(id,sub_category_name,category_id)
This is the SQL query that is needed
select expense.date, expense.description, expense.amount,
category.category_name, subcategory.sub_category_name
from expenses as expense,categories as category,subcategories as subcategory
where expense.category_id=category.id and
category.id=subcategory.category_id);
This is the function in Expense model with which I pass the category_id
The same query mentioned above is written in laravel, but I am not able to
fetch the data.
function fetchExpenseData($categoryId)
{
$expense = Expense::select("expenses.*","categories.category_name as
Categoryname","subcategories.Sub_category_name")
->join("categories","categories.id","=","expenses.category_id");
->join("subcategories",function($join)
{
$join>on("subcategories.category_id","=","expenses.category_id")
->on("suncategories.id","=","expenses.sub_category_id")
})->get();
return $expenses;
}
$expenses that are returned will be printed in blade.php.
Can I know what is the mistake
thanks in advance
Hye there ,
You need to add eloquent model for retrieving data fromenter code here three tables
Like
I have School Table , Student Table , Teacher Table
School is relating with both Student and Teacher then we will add relationship
In School Model
`
public function getStudent(){
return $this->hasMany('student_id' , App\Student);
}
public function getTeachers(){
return $this->hasMany('teacher_id' , App\Teacher);
}
In Student table
public function getSchool(){
return $this->hasOne('school_id' , App\School);
}
`
now call data from student
`
$students = Student::with('getSchool.getTeachers')->get()
This Demonstration for what I have get from your Question

Laravel list music genres with sub model popularity

I have 4 tables:
MusicGenre
Artist
Song
SongInfo
weekly_hit is a column on the SongInfo table.
MusicGenre is connected to Artist like this:
public function artists()
{
return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id');
}
Artist is connected to Song like this:
public function songs()
{
return $this->hasMany('App\Models\Song\Songs','artist_id');
}
And Song is connected to SongInfo like this:
public function info()
{
return $this->hasOne('App\Models\Song\SongsInfo','song_id','id');
}
There is a no problem querying the table.
My problem is that I want to get the best music genres using weekly_hit in SongInfo table.
Edit:
I resolved that problem with raw code
"select music_genres.*,
sum(distinct song_info.weekly_hit) as song_popularity
from `music_genres`
left join `artist_info` on
`music_genres`.`id` = `artist_info`.`genre_id`
left join songs on
artist_info.artist_id = songs.artist_id
left join songs_info on
songs.id = songs_info.song_id
group by music_genres.name
order by song_popularity DESC
limit 5
But, I can't get songs. I want to get 5 song ordered by weekly_hit in songs_info table from all returned music genres.
Guys i still searching a solution?
Can somebody help me?
You'll want to use Eloquent's hasManyThrough() relation to reach that deep in the relationship chain.

laravel 5.1 filter using pivot table

Hi i am developing job portal but facing a problem in pivot table filtering. i have a table like below
personal_details
id
name
sex
dob
nationality
visa_status
vacancies
id
name
description
created_at
updated_at
which have a many-to-many relation with pivot table.
Data here is inserted when a job-seeker apply for a vacancy
personal_detail_vacancy
id
personal_detail_id
vacancy_id
created_at
updated_at
PersonalDetail model
class PersonalDetail extends Model
{
public function vacancies()
{
return $this->belongsToMany('App\Vacancy')->withTimestamps();
}
}
Vacancy model
class Vacancy extends Model
{
public function personal_details()
{
return $this->belongsToMany('App\PersonalDetail');
}
what i want to do is select all personal detail who have applied for a job(in any vacancies) in a particular date
i tried
$personal_details = PersonalDetail::with(array('vacancies' => function($query){
$query->wherePivot('created_at', '2015-11-10 11:33:24');
}))->get();
but it is not filtering the date
ANY IDEA?
well solved this by using
$personal_details = PersonalDetail::whereHas('vacancies',function($query)
{
$query->where('created_at','2015-11-10 11:33:24');
})->get();
it was not working before. The problem was i had 'created_at' field in vacancies table also.

Eloquent nested relationship

i have a question to my db query.
My DB tables/schema:
customers_users (customer_id, user_id) relationship table
projects (project_id, customer_id, [.......]) belongs to customers
i try to get all Projects where a user has access through customers to, with the following query:
//call
User::find(Auth::id())->first()->projects();
//User.model
public function projects() {
return User::with('customers.projects')->get();
}
It works. But now i have User data, Customer data and Project Data in the result array. I want only the Projects. Is there an other way?
$user = auth()->user()->load('customers.projects');
$projects = $user->customers->pluck('projects')->collapse()->unique();

Hibernate Criteria : selection from two tables with Foreign key relationship?

I have in my mysql databse 2 tables :
1.teacher(id(pk),name,phone,email)
2.student(id(pk),teacher'sid(FK),name,phone,email)
The student table has a Foreign Key , teacher'sid ,that refrences to teacher.id .
I want to select the teachers that their id(teachers.id) is same as (student.teacher'sid) where student.name = "Steven".
I have made two classes with the same class name and properties as the table,and i have configured the mapping.
#Entity
class teacher {
#Id
int id;
String name;
String phone;
String email;
}
#Entity
class student {
#Id
int id;
int teachersid;
String name;
String phone;
String email;
}
I am able to select the student that his name is "steven" by:
Criteria q = session.createCriteria(student.class).add(
Restrictions.eq("name", "stevens"));
How can retreive the teacher of "Steven"?
Something like
(select * from teacher(where teacher.id = student.teachersid(where student.name="stevens")))
,but with Hibernate Criteria Api.
Thanks in advance!
Criteria c = s.createCriteria(Teacher .class,"tchr");
c.createCriteria("students", "s");//Teacher class should contain students collection
c.add(Restrictions.eq("s.name", "Ashok"));
List l=c.list();
Hope this will help you. Let me know if you have any questions.
Your Hibernate entity class does not reflect the relation between the tables correctly.
The Student entity should look something similar to
#Entity
class student {
#Id
int id;
teacher teach;
String name;
String phone;
String email;
}
Once you modified your entities correctly, then hibernate automatically issues a join between the tables.
I think the easiest way is to use the teacher entity as main entity and apply a sqlRestriction to filter this table.
I think you cannot use criterias and restrictions directly from the Students class because what you what to retrieve are the teacher entities and there's no mapping in this direction teacher -> student.
I think this will work for you:
Criteria q = session.createCriteria(teacher.class)
.add( Restrictions.sqlRestriction("exists (select 1
from student_table s where s.teachersid = {alias}.id
and s.student_name=?", "stevens", Hibernate.STRING) )