-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Eager Loading Notification Categories #280
Comments
Hey, thanks for the hint. But I think that there is something simpler - cause your solution doesn't load the relation it checks. So why not simply: if(!$notification->category->exists) {
throw new ModelNotFoundException();
}
$category = $notification->category; |
That's even better! here is a working example, because exists gives an error: /** src/Notifynder/Parsers/NotificationParser@parse */
public function parse($notification, $categoryId)
{
$category = $notification->category;
if(is_null($category)) {
throw new ModelNotFoundException();
}
} However, this will work only if we are passing a NotificationModel Object, I've checked the code and it seems that we never pass "$notification" as an array or a BuilderNotification as suggested in the function comment. Finally, if we are only passing a NotificationModel we can remove the category_id argument entirely from the parse function. /** src/Notifynder/Parsers/NotificationParser@parse */
public function parse($notification)
{
$category = $notification->category;
if(is_null($category)) {
throw new ModelNotFoundException();
}
} Thanks for your quick reply! |
Yes, the param doc tag is too much - it could be: I think that this comes from #229 but even there we create a model - https://github.com/fenos/Notifynder/blob/master/src/Notifynder/Builder/Notification.php#L162 But this is another issue - so you can fix all this stuff in one PR or just fix the 1+n query issue and after this I will take care about the doctag issue. |
@Gummibeer A little bit unrelated, but how do you run the unit tests? The following instructions give an error, and I couldn't find a contribution guide composer install
composer dump-autoload
vendor/bin/phpunit
PHP Fatal error: Class 'Fenos\Tests\Models\UserL53' not found in /Notifynder/tests/NotifynderTestCase.php on line 132 |
I had to run and it worked
|
@gaussian1 we run them with TravisCI - if you want to know which commands we use: https://github.com/fenos/Notifynder/blob/master/.travis.yml |
|
Problem:
On Retrieving Notifications an N+1 query problem occurs for notification categories
Here are the SQL Queries after running:
Analysis:
On attempting to eager load, the following function is called inside the NotificationParser that forces one query per category
Possible Solution:
I can create a pull request that checks whether the passed Notification's category relation is eager loaded, to be something like
If the above solution is okay, I can easily add the pull request, Cheers!
Notes:
The relationLoaded function was added in a later version of Laravel, I guess it was 5.1 but I am not sure, if we decide not to use it we can add a function to the Notification Model
Environment:
Laravel Framework 5.4.27
Notifynder 4.2.1
The text was updated successfully, but these errors were encountered: