404 error while executing code below. The Class 'MagazineModel' extends Controller like Class 'Magazine'. Qestion is: Cant i create object of 'MagazineModel' in Class 'Magazine'?
If i cant, how can i do that in different way? Thanks for help!
Class Magazine extends Controller
{
public $id;
public $name;
public $logo_path;
public $order;
public $description;
//model instance container
public $magazine_model;
public function __construct()
{
parent::__construct();
//properties from model
if (class_exists('MagazineModel')) {
$this->magazine_model = new MagazineModel();
}
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getLogoPath()
{
return $this->logo_path;
}
public function getOrder()
{
return $this->order;
}
public function getDescription()
{
return $this->description;
}
public function testShow()
{
echo $this->twig->render('index.html.twig');
}
}
Related
I'm new to UnitTest and trying to integrate it into my Laravel application, but I'm getting the below error:
Call to a member function findOne() on null
at app/Services/User/UserService.php:32
28▕ $this->userWebsiteRepository = $userWebsiteRepository;
29▕ }
30▕
31▕ public function findOne($data = []){
➜ 32▕ return $this->userRepository->findOne($data);
33▕ }
34▕
This is my code.
AuthController.php
class AuthController extends Controller {
private $userService;
public function __construct(UserService $userService)
{
$this->userService = $userService;
}
public function show($id){
return $this->userService->findOne(['id' => $id]);
}
}
UserService.php
class UserService
{
public $userRepository;
public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
}
UserRepositoryInterface.php
interface UserRepositoryInterface
{
public function findOne($data);
}
UserRepository.php
use App\Models\User;
class UserRepository implements UserRepositoryInterface
{
private $model;
public function __construct(User $user)
{
$this->model = $user;
}
public function findOne($data)
{
if (empty($data)) return false;
$query = $this->model->with(['userWebsites', 'userWebsites.website', 'role']);
if(!empty($data['id'])) $query = $query->where('id', $data['id']);
return $query->first();
}
}
RepositoryServiceProvider.php
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}
}
AuthControllerTest.php
class AuthControllerTest extends TestCase
{
public $authController;
public $userRepositoryInterfaceMockery;
public $userServiceMokery;
public function setUp(): void{
$this->afterApplicationCreated(function (){
$this->userRepositoryInterfaceMockery = Mockery::mock(UserRepositoryInterface::class)->makePartial();
$this->userServiceMokery = Mockery::mock((new UserService(
$this->app->instance(UserRepositoryInterface::class, $this->userRepositoryInterfaceMockery)
))::class)->makePartial();
$this->authController = new AuthController(
$this->app->instance(UserService::class, $this->userServiceMokery)
);
}
}
public function test_abc_function(){
$res = $this->authController->abc(1);
}
}
I was still able to instantiate the AuthController and it ran to the UserService. but it can't get the UserRepositoryInterface argument. I think the problem is that I passed the Interface in the constructor of the UserService. .What happened, please help me, thanks
I don't know where $userService comes from to your controller's constructor, but it seems like it comes from nowhere. You need to pass it as argument, so Laravel can resolve its instance in service container.
class AuthController extends Controller {
private $userService;
public function __construct(
private AuthService $authService,
UserRepositoryInterface $userRepository
) {
$this->userService = new UserService($userRepository);
}
public function show($id)
{
return $this->userService->findOne(['id' => $id]);
}
}
Also there is literally no findOne method in UserService. You need one there.
class UserService
{
public function __construct(private UserRepositoryInterface $userRepository)
{
}
public function findOne(array $data)
{
return $this->userRepository->findOne($data);
}
}
Update
In that case you need this in service provider:
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
$this->app->bind(UserService::class, function ($app) {
return new UserService($app->make(UserRepositoryInterface::class));
});
I want to be able to pass in an object that implements IEntity interface but I'm facing a
PHP Fatal error: Declaration of ShirtOrderRepository::find(ShirtOrder $shirtOrder) must be compatible with RepositoryInterface::find(IEntity $entity) in /Users/okkamiadmin
/projects/Befeni/index.php on line 52
error how can I achieve this as I'm not sure if I'm doing it the right way?
<?php
interface IEntity {
public function getTableName();
}
class ShirtOrder implements IEntity
{
public $id;
public $customerId;
public $fabricId;
public $collarSize;
public $chestSize;
public $waistSize;
public $wristSize;
public function getTableName()
{
return 'shirt_orders';
}
}
interface RepositoryInterface
{
public function find(IEntity $entity);
public function save(IEntity $entity);
public function remove(IEntity $entity);
}
interface DatabaseInterface {
public function find(IEntity $entity);
public function save(IEntity $entity);
public function remove(IEntity $entity);
}
class ShirtOrderRepository implements RepositoryInterface {
protected $db;
public function __construct(DatabaseInterface $db) {
$this->db = $db;
}
public function find(ShirtOrder $shirtOrder)
{
$this->db->find($shirtOrder);
}
public function save(ShirtOrder $shirtOrder)
{
$this->db->save($shirtOrder);
}
public function remove(ShirtOrder $shirtOrder)
{
$this->db->remove($shirtOrder);
}
}
You need to pass type of IEntity instead of ShirtOrder at ShirtOrderRepository class like so:
public function find(IEntity $shirtOrder){
$this->db->find($shirtOrder);
}
Also you need to do this for the rest of the methods since RepositoryInterface expects type of IEntity passed in its methods parameters.
I'm trying to learn and implement the Repository Pattern in my app built with Laravel 5.6.
I have implemented my Controller:
class CompaniesController extends Controller
{
protected $company;
public function __construct(ICompanyRepository $company) {
$this->company = $company;
}
public function index(){
$companies = $this->company->getAllCompanies();
return view('companies::index')->with("companies", $companies);
}
}
Then I have implemented the repository interface:
interface ICompanyRepository
{
public function getAllCompanies();
public function findBy($att, $columns);
public function getById($id);
public function with($relations);
}
I have implemented My Repository:
class CompaniesRepository implements ICompanyRepository
{
protected $model;
public function __construct(Companies $model){
$this->model = $model;
}
public function getAllCompanies(){
return $this->model->all();
}
public function findBy($att, $columns)
{
return $this->model->where($att, $columns);
}
public function getById($id)
{
return $this->model->findOrFail($id);
}
public function with($relations)
{
return $this->model->with($relations);
}
}
And then I have created the model:
class Companies extends Model
{
protected $fillable = [];
protected $casts = [
'settings' => 'array'
];
//my question is here!
public function members(){
return $this->hasMany('Companies\Entities\CompaniesMembers');
}
}
For now I have put the relations (in this case members function) in the model, but with this way, If I had to change my ORM, I should change both repository and model, because for now I use Eloquent, but I don't know if in future I will use Doctrine or others.
So my question is:
Where is the best place the relationships and the functions for db?
Is right put all in the Model or It would be better put all in Repository?
So in the EloquentRepo you make a function that combines company with companymembers by a foreach and use modelToObject($model) Something like this. I hope this will help you the good direction.
EloquentRepo:
private function modelToObject($model)
{
if (is_null($model)) {
$entity = null;
} else {
$entity = new Product(
$model->{Model::COL_ID},
$model->{Model::COL_NAME}
);
}
return $entity;
}
Entity:
class Product{
private $id;
private $name;
public function __construct(int $id, string $name) {
$this->setId($id)
->setName($name);
}
public function setName(string $name): Product{
$this->name = $name;
return $this;
}
public function getName(): string {
return $this->name;
}
}
in php I have this code. I'm trying to get an inherited method to utilize a member variable of its child class.
abstract class HtmlObj{
//abstract protected function jQuery_Activity();
public $hyperlink;
abstract protected function php_Activity();
abstract protected function print_Widget();
function __construct($hyperlink=""){
if(isset($hyperlink)){
$this->hyperlink = $hyperlink;
}
$this->php_Activity();
$this->Print_Widget();
}
}
class child extends HtmlObj{
public $id;
protected function php_Activity(){return;}
protected function print_Widget(){
print $this->id;
}
function __construct($id){
this->id = $id;
}
}
unfortunately this prints nothing. any insights as to why?
in child class You need to reffer to parent::__construct() by doing something like
abstract class HtmlObj
{
//abstract protected function jQuery_Activity();
public $hyperlink;
abstract protected function php_Activity();
abstract protected function print_Widget();
function __construct($hyperlink = "")
{
if (isset($hyperlink)) {
$this->hyperlink = $hyperlink;
}
$this->php_Activity();
$this->Print_Widget();
}
}
class child extends HtmlObj
{
public $id;
protected function php_Activity()
{
return;
}
protected function print_Widget()
{
print $this->id;
}
function __construct($id)
{
$this->id = $id;
parent::__construct();
}
}
new child(10);
I'm building a big OOP Based PHP project, I've written 2 classes User and Page :
class User{
//user properties
private $_id,
$_name,
$_email;
//constructor
public function __construct(){}
//getters and setters
public function setId($id){
$this->$_id=$id;
}
public function setName($name){
$this->$_name=$name;
}
public function setId($email){
$this->$_email=$email;
}
public function getId(){
return $this->_id;
}
public function getName(){
return $this->_name;
}
public function getEmail(){
return $this->_email;
}
}
class Page{
//properties
private $_id,
$_title;
//constructor
public function __construct(){}
//getters and setters
public function setId($id){
$this->_id=$id;
}
public function setTitle($title){
$this->_title=$title
}
public function getId(){
return $this->_id;
}
public function getTitle(){
return $this->_title;
}
}
As you can conclude , I need to add another class DBManager containing add, edit, delete , list and views methods .
I don't know how to create a method (add for instance) in the class DBManager, that can add Users and Pages. without creating a class Manager for every class.
in other words , I want to create a class DBManager for all my objects.. instead of creating something like :
class User : class UserManager
class Page : class PageManager