tl;dr: I need to show data from two different tables in the list view of the Symfony admin generator (preferably via a JOIN statement)
I'm using the sfDoctrineGuardPlugin to manage the users of my app. Each user also has their own profile that they can fill out. The schema (schema.yml) for the profile table looks like this:
sfGuardUserProfile:
connection: doctrine
tableName: sf_guard_user_profile
columns:
sf_guard_user_id: integer(8)
client_id: string(128)
relations:
sfGuardUser:
local: sf_guard_user_id
foreign: id
foreignType: one
onDelete: CASCADE
Right now table is a client_id and a foreign key to the id column on the sfGuardUser table.
(Yes, I know I need an id column in there just for DB accessibility, and more is coming later)
I've created an admin for my profile table using the Symfony admin generator. I would like my list view on the admin to look like this (generator.yml):
config:
...
list:
title: Clients
display: [client_id, sf_guard_user_id, username]
table_method: retrieveProfileList
However, my username column does not work, and I receive a Doctrine_Record_UnkownPropertyException of:
Unknown record property / related component "username" on "sfGuardUserProfile"
I was hoping a custom table_method to populate the data for the list view would help me, but my table method (in lib/model/sfGuardUserProfileTable.class.php) does not seem to help. I followed an example in the Symfony Book, and this is the method:
public static function retrieveProfileList(Doctrine_Query $q)
{
$rootAlias = $q->getRootAlias();
$q->leftJoin($rootAlias . '.sfGuardUser id');
return $q;
}
Any ideas on how to have the Symfony admin generator display a list view based on a join? Or, alternatively, how to pull from a separate model?
I have answered my own question!
I changed the join method on my sfGuardUserProfileTable class to something I could better understand (thanks to this helpful link):
public static function retrieveProfileList(Doctrine_Query $q)
{
return $q->select('r.*, u.username')
->leftJoin('r.sfGuardUser u');
}
And threw in a getUsername method in my sfGuardUserProfile model class
public function getUsername()
{
return $this->sfGuardUser->username;
}
Thanks for being my rubber duck, SO!
Related
Could anyone please tell me how to create a relation between 3 tables.I have 3 tables. User, Profile and Post. Now I want to list all posts on the home page with user name form users table and profile image from the profiles table and posts from posts table.
There are few things to consider before suggesting any code. Your database structure, eloquent model classes and any other condition you want to apply before getting posts from database.
Hope, your profiles and posts table (both) contains user_id field.
You have to define two relationships.
In Post model define:
public function user() {
return $this->belongsTo('App\User');
}
In User model define:
public function profile() {
return $this->hasOne('App\Profile');
}
Now, before forwarding your $posts to view you may run this statement:
$posts->load('user.profile);
This will load relevant user and their profile within posts. Like this:
posts:[
{...,
user:{
...,
profile:{...}
}
},
...
]
Further reading recommended:
Belongs To Relationships
Nested Eager Loading
I'm new to Stack Overflow and Laravel.
I try to develop a variable profilesystem in Laravel and I got 3 tables ('User', 'User_Fields', 'Fields').
The structure of the Fields table is following:
The structure of the user_fields table is following:
The User table is the standard table which came with Laravel 5
Now I want to get all fields from user_fields depending on which user is selected or logged in. If the user_field doesn't exists, the model should return null or create a new record for the user_field.
I tried a lot of "solutions" which came with eloquent like (hasManyThrough, belongsToMany) but I don't get the result I wanted.
Is there any solution with relationship methods?
Thanks for your help, and sorry for my bad english :/
You should be using the belongsToMany()->withPivot() to retrieve the intermediate table columns. Your user_fields is your pivot table so on your user model you should have:
class User extends Model
{
public function fields()
{
return $this->belongsToMany(Field::class, 'user_fields')->withPivot('value');
}
}
Then you can access your values for a user by:
$user = User::find($id);
foreach($user->fields as $field) {
$field->pivot->value;
}
I am working in laravel5.4. I have created four table as ticket, users, and company. Here, I have stored users id in ticket table. And stored company id in users table.
Here, I want to show ticket's user name with that users company name.
Here, I have create relation for it that looks like below.
Ticket model :
public function requesters(){
return $this->belongsTo('App\User','requester_id');
}
Here requester_id is who create ticket.
Users model :
public function company()
{
return $this->belongsTo('App\Models\Admin\Company');
}
Company model :
public function Users()
{
return $this->hasOne('App\Users');
}
Here, I have written query to get users information that looks like below.
Ticket::with('requesters')->orderBy('subject','asc')->paginate(10);
Now, I want to fetch company information or request_id So what changes should I have to do in this query to fetch company info along with ticket and users ?
If you want to to Eager load multiple relationship you can put them all in single with like this:
Ticket::with('requesters','requesters.company')->orderBy('subject','asc')->paginate(10);
but if you load nested relationship you can use shorter notation - you can omit parent relationship, so it's enough to use here:
Ticket::with('requesters.company')->orderBy('subject','asc')->paginate(10);
Try this, it should work for this case:
Ticket::with('requesters')
->with('requesters.company')
->orderBy('subject','asc')->paginate(10);
I'd like your input on this.
I have a Customer_table with a field name. I have another table called Reservation_table with a Customer_name field.
How can I relate them in such a way that I'd see all the bookings by the specific customer?
In Reservation_table you should have a field(foreign key) userid in order ta have a user for each reservation.
You can using primary key - foreign key relationship to relate/join those two tables. Also, instead of having a 'Customer_name' field as your FK referring to 'name' field in 'Customer_table' table, it is better to have an id (unique) generated for each customer; This way you can have an efficient way of uniquely identifying and relating customer across tables; can save space on Database side as well. Hope this helps!
If you want to use eloquent you must first define a relationship.
One reservation belongs to a user. Here is how to define the relationships:
Inside the Reservation model:
public function user()
{
return $this->belongsTo('App/User'); //User model
}
To define the inverse you do the following:
Inside User model:
public function reservations()
{
return $this->hasMany('App/Reservation'); // Reservation Model
}
Now you can do the following in your controller:
$reservations = Auth::user()->reservations;
Now you have all reservations by the currently logged in user.
I am not sure if I got the question right so ask away.
I have a users table and consultant_profile table set up on my db. They are related and in my controller action i can see in the users object(from a model) that it has a relations array and in there is the profile object.
I am now trying to create a table for qualifications which has a one to many relationship with the consultants profile so one profile can have many qualifications.
For ease i here is a shorter version of the schema for the tables:
consultant_profile [
- id
- address line 1
- postcode
]
consultant_qualifications [
- id
- consultant_profile_id
- qualification
]
I have put this in my model for the consultant_profile
public function qualifications()
{
return $this->hasMany('ConsultantQualifications', 'consultant_profile_id');
}
and this in my consultant_qualifications model
public function profile()
{
return $this->belongsTo('ConsultantProfile',null,"consultant_profile_id");
}
I have tried it with putting in foreign/local keys and without.
What i expect the out come to be is i can look in the relations array of user object and see the profile object, i could then look into profile object and in its relations array i should see qualifications object
But i don't, any reason why?