Skip to content
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

Cannot get Recommendations of different model types. #23

Closed
RaidOuahioune opened this issue Nov 21, 2023 · 1 comment · Fixed by #25
Closed

Cannot get Recommendations of different model types. #23

RaidOuahioune opened this issue Nov 21, 2023 · 1 comment · Fixed by #25

Comments

@RaidOuahioune
Copy link
Contributor

I have a table called user_views like shown bellow
image
And here is the code to config the recommendation i used

<?php

namespace App\Business\Recommendation;

use App\Models\Course;
use App\Models\Document;
use App\Models\Question;

trait HasViewsRecommendation
{


    public static $recommendations = [
        Document::class => 'suggested_documents',
        Course::class => 'suggested_courses',
        Question::class => 'suggested_questions'
    ];

    public static $recommendationCount = 5;
    private static function generateRelationAlgorithmArray(string $model, string $name): array
    {

        return
            [$name => [
                'recommendation_algorithm'         => 'db_relation',
                'recommendation_data_table'        => 'user_views',
                'recommendation_data_table_filter' => [],
                'recommendation_data_field'        => 'viewable_id',
                'recommendation_data_field_type'   => $model,
                'recommendation_group_field'       => 'user_id',
                'recommendation_count'             => self::$recommendationCount
            ]];
    }
    public static function getRecommendationAlgorithmArray(): array
    {
        $result = [];
        foreach (self::$recommendations as $model => $name) {
            $result = array_merge($result, self::generateRelationAlgorithmArray($model, $name));
        }
        return $result;
    }
}

And for sure I overided the the static function like this

class User extends Authenticatable implements  InteractWithRecommendation
{

    use HasFactory,
        HasRecommendation,
        HasViewsRecommendation,
  

    /**
     * The user recommendation config.
     * @return array
     */
    public static function getRecommendationConfig(): array
    {
        return self::getRecommendationAlgorithmArray();
    }

And i generated some recommendations using the laravel-model-recommendation table and here is some rows
image

Now based on my understanding .if the user logged in is of id 7 and i called the fuction php $user->getRecommendations('suggested_courses'); I should get the First 5 rows that are of type Courses .But the thing is that i always get an empty array .
To better undestand the situation I inspected the source code and I think it"s because of this query
image
The target_type is hardcoded to be the same as the source type.

@umutphp
Copy link
Owner

umutphp commented Nov 22, 2023

@RaidOuahioune

Many thanks for reporting the issue and creating the PR. You are 100% right :).

@umutphp umutphp linked a pull request Nov 22, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants