Suppose you have
Facility 1------* Section 1------* Session *------1 Event
I.e., a facility has many sections; each section can hold many sessions; each session belongs to an event.
How can I define this as a relationship in the Facility model to retrieve all unique instances of Event that a facility is hosting? I have tried this:
class Facility extends Eloquent\Model {
public function events() {
return $this->hasMany('Event')
->join('session', 'session.event_id', '=', 'event.id')
->join('section', 'section.id', '=', 'session.section_id')
->join('facility', 'facility.id', '=', 'section.facility_id');
}
}
I don't know if I'm very close with that; Laravel adds a constraint implicitly ("events"."facility_id" in (...)) and everything gets messed up.
What is the proper way of doing this?
I would avoid putting any logic in a relationship method on your models. Instead use eloquent to load your relationships, either one after the other or eager load into the Facility model. The latter being preferable.
class Facility extends Eloquent\Model
{
public function sections()
{
return $this->hasMany('Section');
}
}
Just make sure each of the models has the correct relationship set up so you can chain an eager load. Such as:
class Section extends Eloquent\Model
{
public function sessions()
{
return $this->hasMany('Session');
}
}
Then when you come to load the relationships you can use dot notation to eager load them.
$facility = Facility::with('sections.sessions.event')->get();
$facility will now contain a Facility model with nested relationships. You can use some of the different array helpers to extract/pluck all of the events.
This is the closer i've got to my initial purpose:
I created an SQL view:
CREATE VIEW facility_events AS
SELECT DISTINCT ON (e.id) e.id,
e.name,
e.created_at,
e.updated_at,
f.id AS facility_id
FROM events e
JOIN sessions s ON e.id = s.event_id
JOIN sections_extended fse ON s.section_id = fse.id
JOIN facilities f ON fse.root_facility_id = f.id;
Then I create the corresponding FactoryEvent Eloquent model and, finally, in my class Facility:
public function events() {
return $this->hasMany('App\FacilityEvent');
}
I look forward to see Laravel-only solutions for this. In other frameworks, such as Yii, I have been able to do things like that without the need to work on the database directly.
Related
I have a chat table that both a user and admin can chat the table is defined as follow:
id, from_id, to_id, message, is_from_admin.
what I want is, if the is_from_admin is true laravel should use the admin table at sql level for the from. otherwise it should use the user table for from and same applies to the to field. Thanks
If you have the chance, I'd rework the table a bit and name it like so:
id, from_user_type, from_user_id, to_user_id, message
The pair from_user_type and from_user_id can be used to creat a custom polymorphic relation ("type" refers to the model/table name, and "id" refers to the id of a row in this table) as seen here: https://laravel.com/docs/9.x/eloquent-relationships#one-to-many-polymorphic-relations .
If you also want to send admin-to-admin, you should also add to_user_type, to_user_id so you can create a polymorphic relationship on the receiving side as well.
The polymorphic relation will look something like this:
class ChatMessage
{
public function fromUser()
{
// This function should automatically infer `from_user_type` and `from_user_id`
// from this function name.
return $this->morphTo();
}
}
class AdminUser
{
public function chatMessages()
{
return $this->morphMany(ChatMessage::class, 'fromUser');
}
}
Laravel can not solve what you are doing, which is a polymorphic relationship, based on a boolean. Theoretically you could bind the polymorphic class definition to 0 or 1, but this is a hack at best. Alternatively you could rewrite your table structure to support polymorphic relations.
Instead i would say you achieve something that is working, with what you have. Create two relationships combined with some logic in an accessor. Create a relationship for the admin and for the user.
Chat extends Model
{
public function fromAdmin()
{
return $this->belongsTo(Admin::class, 'from_id')->where('is_from_admin', true);
}
public function fromUser()
{
return $this->belongsTo(User::class, 'from_id')->where('is_from_admin', false);
}
}
Now create the accessor on the Chat model, using your new relationships.
public function getFromAttribute()
{
return $this->fromAdmin ?? $this->fromUser;
}
With this approach, you should be able to access the attribute like this.
Chat::find(1)->from; // either a user or admin based on the data.
The actual scope examples provide the use of hard-coded parameters passed to the query:
public class Employee extends Model {
static {
addScope("byDepartment", "department = 'marketing'");
}
}
Is it possible to make that parameter dynamic and all the scope as follows:
public class Employee extends Model {
static {
addScope("byDepartment", "department = '?'");
}
}
Example of use:
Employee.scope("byDepartment").where(....) <-- how to pass in a department value ?
Thank you.
Current implementation only works with hard-coded scopes. In general, having dynamic scopes is not any different than just having an additional parameter in a where() method, but will significantly complicate the implementation.
This question prompts for some philosophical discussion. Normally, you would be using a model as its own service. In other words, using a model like this from outside the model is not a preferred way:
List<Employee> employees = Employee.scope("byDepartment").where("start_date > ?", startDate);
It is best to wrap all access to the EMPLOYEES table into the Employee class like this:
public class Employee extends Model{
public static List<Employee> getStartedByDepartment(Date started, String department){
return Employee.scope(department).where("start_date > ?", started);
}
}
We code all JavaLite projects with this pattern, and do not allow ActiveJDBC API to bleed outside models (for the most part, lol).
As you can see, there is little that scopes will give you as the internal implementation of the model may or may not use scopes, you will get the same results. This coding pattern is much better becuase:
You have static methods on Models you can test
You have static methods that may have guard statements if (department = null) throw new IllegalArgumentException("blah...")
You have static methods on Models that have good semantic names
The implementation and access to your table is wrapped in one class, and not bled outside (controllers).
Easy to do refactoring down the road.
However, if you use this approach, the value of scopes is near zero.
Needless to say, I do not use scopes in my work.
I do not see anything wrong in making the parameter dynamic like in your Employee model:
public class Employee extends Model {
static {
addScope("byDepartment", "department = '?'");
}
}
the issue is actually with
Employee.scope("byDepartment").where(....)
beside the scopeName, scope() does not provide any way for extra params for the scopeValue(s).
Instead you can call Employee.where(subquery, params)
where subquery would be your scopeQuery that you can access with ModelDelegate.getScopes().get(scopeName) and params would be your scopeValue.
So I am trying to implement a friendlist, the above is the SQL diagram I made for my simple project and after generating the Models. I realized there was something wrong with the way Gii generated the model.
I wanted to make a many-to-many relationship with User to itself, but this is what I got:
class User {
...
public function getPosts()
{
return $this->hasMany(Post::className(), ['userId' => 'id']);
}
}
class Friend {
...
public function getFriend()
{
return $this->hasOne(Member::className(), ['id' => 'friendId']);
}
}
The User class doesn't have any relationship with itself, I expected something like getUsers() inside of User, but it didn't generate it. I initially thought about not making a model with the junction table, but I did so just to see what would happen. I don't think I need it. So I am not sure how to do this correctly? Do I need to get rid of my Junction Table Models and Do I need to make the relationship between User to itself and User to Message manually? I thought about doing a many-to-many in User and Message and a many-to-many in User for User. Is this the right thing? Tell me if I am wrong. Thank you.
You are on a true way. You need a junction table for implementing your goal. Easily as you done this, you must define two model: User and Friend. Now on your User model at first you must define a relation for get the list of all friends, Suppose call it getFriendsLists:
public function getFriendsLists()
{
return $this->hasMany(Friend::className(), ['userId' => 'id']);
}
This relation says that "Get me all account that are connected with me, i.e. if my id is 102, this relation return all record of friend table that their userIds are 102". Well, now we get all friends with a relation on User model, let call him getFriends:
public function getFriends()
{
return $this->hasMany(User::className(), ['friendId' => 'id']
->via('friendsList');
}
Notice that 'friendsList' as is a parameter of via method, is our predefined relation on top of this answer. Now easily you can get all account that are friends of our example (User with id 102):
public FriendController extends Controller
{
// Some code goes here!
public function actionFriendList($id)
{
$user = User::findOne($id);
$friends = $user->friends;
return $this->render('friend-list', ['friendsArray' => $friends]);
}
}
And use them on your friend-list view file as $friendsArray variable. Extra note that $user->friends use friends relation that you defined on User model with getFriends method.
I am new to Laravel and a bit confused about some definitions of ORM.
I am currently working on a simple Trouble ticket management system, and here is my question :
(table: column, column,...)
tickets : id, description, equipment_id
equipments: id, name, vendor_id
vendor: id, name
This is a very short resume of my tables and its relations, following Laravel's conventions. How can I build these models?
Basically I need to retrieve, for example, how many tickets were opened to a certain vendor (how many times I called the vendor for support).
Thank you in advance
What zwacky said is entirely (edit: maybe not entirely correct in the end) true for close relations, but in your situation there is nested relation:
Vendor -> Equipment -> Ticket
Then to retrieve tickets for particular vendor you would define relation on Vendor model like this:
class Vendor extends Eloquent {
public function equipment()
{
return $this->hasMany('Equipment');
}
public function tickets()
{
return $this->hasManyThrough('Ticket', 'Equipment');
}
class Equipment extends Eloquent {
public function tickets()
{
return $this->hasMany('Ticket');
}
public function vendor()
{
return $this->belongsTo('Vendor');
}
class Ticket extends Eloquent {
public function equipment()
{
return $this->belongsTo('Equipment');
}
and to get count of total tickets for the vendor (not currently open):
Vendor::find($id) // retrieve particular vendor
->tickets()->count(); // get count on tickets table
// and this way you retrieve collection of related tickets
Vendor::find($id) // retrieve particular vendor
->tickets; // get Eloquent Collection
Also you may find it helpful: http://softonsofa.com/querying-relations-with-eloquent-in-laravel-4/
you'd need to declare these relationships within their models. e.g. your Ticket.php model could look like this:
class Ticket extends Eloquent {
public function equipment()
{
return $this->hasOne('Equipment');
}
public function vendor()
{
return $this->hasOne('Vendor');
}
...
}
for retrieval you'd do it like this:
foreach (Ticket::all() as $ticket) {
$ticket->vendor()->id;
}
check this section of the laravel docs.
edit: for the specific query how many tickets are open to a certain vendor:
Ticket::where('open', '=', 1)->vendor()->where('id', '=', 42);
I'm working on a web app using the Lithium Framework with a MongoDB database.
On one page of the application - I want to display data from multiple object types. I understand the concept of relationships (i.e. belongsTo, hasMany, etc.) between models. But, my questions has to do with Controller relationships.
For example, assume I have two objects named "People" and "Companies". I want to show specific information about Companies on a "people" view. I have done the following:
1) In the "People" model, I've added the following line:
public $belongsTo = array('Companies');
2) In the "PeopleController" file, I've also included a reference to the Companies Model, such as:
use app\models\Companies;
Now, within the PeopleController, I want to call a method in the CompaniesController file.
Do I access this by directly calling the CompaniesController file? Or, do I have to go thru the Company model.
In either case, I'll need help with the syntax. I'm having rouble figuring out the best way this should be called.
Thanks in advance for your help!
You should rethink your structure - you controller method should really grab all the resources you need for that view, it doesn't matter what they are.
So if you have a url '/people/bob' and you want to get the company data for Bob just add that to the view method of your People controller. Something like
People::first(array('conditions' => array('name' => 'Bob'), 'with' => 'Companies'));
You could instantiate a CompaniesController (maybe passing in $this->request to the 'request' option in the process) and then call the method in it. However, a better way to organize it is to move the common functionality from CompaniesController to Companies and call it from both places.
use app\models\Companies does not really make a "reference." It simply indicates that Companies really means app\models\Companies. I think an "alias" is a better way to think of it. See http://php.net/manual/en/language.namespaces.importing.php.
Example:
// in app/models/Companies.php
namespace app\models;
class Companies extends \lithium\data\Model {
public static function doSomething() {
// do something related to companies.
}
}
// in app/controllers/CompaniesController.php
namespace app\controllers;
use app\models\Companies;
class CompaniesController extends \lithium\action\Controller {
public function index() {
$result = Companies::doSomething();
return array('some' => 'data', 'for' => 'the view');
}
}
// in app/controllers/PeopleController.php
namespace app\controllers;
use app\models\Companies;
class PeopleController extends \lithium\action\Controller {
public function index() {
$result = Companies::doSomething();
return array('some' => 'data', 'for' => 'the view');
}
}