How to override any Model of vendor folder in laravel - laravel

I am using MongoDB as database so I need to change model of package tzsk\payu as using original is giving me following error
Call to a member function prepare() on null
I tried excluding original model and overriding using composer it doesn't work.

The only way would be if the package offered the option to use a custom model, like Passport is doing.
As far as I can see, there does not seem to be a way to do that. Thus you'd need to fork the package and edit the Model yourself.

Related

findOrFail() method not found by PhpStorm

PhpStorm does not found findOrFail() method. When I try to call it, there isn't auto-complete and the result is always fail. The user found is always on the message.
I try to use Laravel Ide helper and query()->findorfail but I didn't resolve.
The result:
in phpstorm try the Laravel plugin to generate the IDE classes.
please check your User model. It must extend Model class like this.
use Illuminate\Database\Eloquent\Model;
class Lead extends Model {
I guess your User model is missing this "extends Model".
As I know, PHPStorm never detects the findOrFail method even we already installed the Laravel plugin.
But you use it wrong because this method takes an id and returns a single model. If the method can't find any matching data, it will return an error.
You can change the method into:
User::findOrFail($user->id);

PrestaShop working with Doctrine & Entites

Hello :) On start: I use PrestaShop 1.7.6.2 and MySQL 5.6 and PHP 7.2
I want to create a module on new way with Symfony Controller and Entites without ObjectModel (beacouse like say one of develoeper of PrestaShop: Pablo Borowicz - ObjectModel is deprecated)
So on start I create simple module available at the link
https://github.com/DarkSidePro/testmodule
Controller and routing works perfect the problem is when I try use enity manager
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$testRepository = $entityManager->getRepository(TestmoduleTest::class);
$test = $testRepository->findAll();
And I have error like that:
The class 'DarkSide\Testmodule\Entity\TestmoduleTest' was not found in the chain configured namespaces PrestaShopBundle\Entity
Maybe I doing something wrong? But doc of prestashop of coures is suck about new way of creating a PrestaShop modules
Looking 4 help :)
Thx all :)
The PrestaShop documentation is a completely mess concerning the handling of the Symfony repositories.
Doctrine is looking (by the auto_mapping orm configuration attribute) for the presence of your entity under the Prestashop Entity namespace, so given that your TestmoduleTest entity isn't there, it cannot be found and therefore loaded.
You may have to register your entity under the following namespace : namespace PrestaShop\Module\Testmodule\Entity;
You'll be able to find more about how to create your own repository class inside the official productcomments module here.
Ok the problem is on other way :)
In this case i have problem with my Repository class (probably with constructor of this class)
when I deleted them module start work
Problem solved :)

How to mock User model within composer package development tests?

I started creating a laravel 5.8 based modular API framework for our company which should be extended using composer packages.
Now I stumbled over the problem to test each package by itself (each package has it's own GIT project of course) if the package needs to have access to the User model given in the base framework (App/Models/User).
There will be various packages naturally depending on the User model such as specific auth modules.
Unfortunately testing also gets more complex because we are using GraphQL (Lighthouse).
So how should this be done? I tried mocking App/Models/User with a User model contained in the tests folder of my package, but this did not work as expected:
$this->userMock = \Mockery::mock('CompanyName\\PackageName\\Tests\\User');
$this->app->instance('App\\Models\\User', $this->userMock);
When, after that, posting a GraphQL request the resolver method throws a Class App\Models\User does not exist error.
I am quiet new to testing with phpunit so maybe I am just missing something here?
Edit:
I just found out that the error message above is displayed because the User model is also referenced within the GraphQL schema file.
So I there is any solution out there it has to somehow "emulate" the not existing User model class for the whole request lifecycle I guess...
Ok I finally solved my problem which was more conceptual wise I guess. As the user model is pretty strongly tied to the (core) package I want to test, I have now moved the model into the package itself and removed it from the base project.
This has the advantage that the "end user developer" doesn't even see and has to cope with the user model which is handles by the package anyway.
Now I can test the package independently and only have to put a line of documentation into the README to tell, that a user has to change the auth.providers.users.modelvalue to let laravel use the appropriate model (e.g. CompanyName\\PackageName\\Models).
If there will be other packages extending the user model, they will have to depend on the core package (which they should either way) and can extend the model class and tell the user to update auth.providers.users.model again. This way it is also quiet transparent to see which user model is used currently.
For the GraphQL / Lighthouse part I have added the following code to the boot method of the package's service provider to make lighthouse know about new models within the package automatically:
$lighthouseModels = config('lighthouse.namespaces.models');
array_push($lighthouseModels, 'CompanyName\\PackageName\\Models');
config([
'lighthouse.namespaces.models' => $lighthouseModels
]);
This can be repeated for every package adding models as well so lighthouse knows about all of them.

Call model from another plugin in Strapi

I have a Notification model which belongsToMany users from users-permissions plugin.
I have another model called Profile which belongsToOne user.
Now, I want to populate the notification with related user and their profile ...
I have tried:
await strapi.query("user", "users-permissions").find({_id: `model id`}).populate('profile')
Throwing Impossible to find the plugin where strapi.query has been called. at me.
Also tried:
notification.user.populate('profile')
No luck either
I also tried strapi.plugins["users-permissions"].models.user got undefined!
You don't need query.
You will have to use the mongoose model instance of the User model of Users & Permissions plugin.
return strapi.plugins['users-permissions'].models.user.
.findOne(user_id)
.populate('profile');
I am having the same problem here.
All of these are undefined:
strapi.query
strapi.plugins
The global strapi object exists, just doesn't provide the properties listed above...
Am I missing something? I have read the docs at least three times... no luck.
Global strapi object do not have either plugins or query properties.
All strapi object has is as shown in screenshot.
Please note that I am trying to query plugin model from:
/admin/src/containers/HomePage/index.js
I think this works on the backend side, whilst you are trying to reach it from the front end

Laravel Auditing AuditableTransitionException Error on Morphmap on transitionTo()

I am able to successfully record changes to classes, and return what has been changed (not including many-to-many). However, I am unable to revert back any change using the built-in new transitionTo() method.
I get the following error on all classes:
Expected Auditable type App\XYZ, got XYZ instead
I have all of my morphable classes (which are all I am using for tracking audits) attached correctly within AppServiceProvider in a morphmap like so:
\Illuminate\Database\Eloquent\Relations\Relation::morphMap([
'Employee' => \App\Employee::class,
];
All classes work correctly with all other Laravel morphTo methods.
The auditable code looks like it is tripping the error in line 467 of the Auditable class:
if (!$this instanceof $audit->auditable_type) {}
It doesn't appear to be looking to the map for any of the morphed classes. Or, I could be totally missing something of course!
Any help on how to get this to work using the auditing method -- has anyone gotten this to work with standard morph classes? (It will of course revert the class manually by looping on the old fields and saving the object).
Using Laravel 5.5 and the latest version (5.0) of Laravel-Auditing.
Sent a note to the developer - this was in fact a bug. Vendor code was needed to include morphMapped objects.
Developer at Laravel Auditing responded within an hour and had a fix a few hours later. I can confirm this is now functioning as expected. Outstanding support.

Resources