I'm currently working on an application working with Laravel 4. I'm working on building a commenting system and have the basic Commands for doing Create, Update, Delete.
What I'm trying to do next is create commands for the specific objects to which the comments will be attached, for example a Blog Post.
So if my command file is called CreateCommentCommand I would like to create another command called CreatePostCommentCommand. I would like this newer class to extend the CreateCommentCommand and have the type hardcoded within.
So for CreateCommentCommand I have the following:
class CreateCommentCommand extends Command {
public $commentable_type;
public $commentable_id;
public $comment;
public $user_id;
public function __construct($commentable_type = null,
$commentable_id = null,
$comment = null,
$user_id = null)
{
$this->commentable_type = $commentable_type;
$this->commentable_id = $commentable_id;
$this->comment = $comment;
$this->user_id = $user_id;
}
}
... and for CreatePostCommentCommand I have:
class CreatePostCommentCommand extends CreateCommentCommand {
public $commentable_type = 'post';
public function __construct($commentable_id = null,
$comment = null,
$user_id = null)
{
$this->commentable_id = $commentable_id;
$this->comment = $comment;
$this->user_id = $user_id;
}
}
For the Handler I have CreatePostCommentCommandHandler which extends CreateCommentCommandHandler. However the problem I'm encountering is when it hits the field validators which are part of the CreateCommentHandler then it says $commentable_type was not supplied.
Related
I'm working with this case where I need to instantiate an object after a form is submitted in a controller. Everything's working fine until I call this object (as a property) from another method. It appears to be null.
If I intentiate the object from constructor method, I have no problem at all.
I can't keep this object in session because of closure.
Here's what i got so far.
// Version with the object iniate within the constructor that's working
class SearchConsoleController extends Controller
{
private $console;
protected function __construct() {
$callback = route('searchconsole.callback') ;
$this->console = $this->setConsole(env('CLIENT_ID'), env('CLIENT_SECRET'), $callback);
}
private function setConsole($cliendId, $cliendSecret, $callback){
$console = new Console(new Google_Client(), $cliendId, $cliendSecret, $callback);
return $console;
}
public function index(Request $request) {
return view('searchconsole.index')->with('authUrl', $this->console->getAuthUrl());
}
public function callback(Request $request){
if ($request->has('code')) {
$this->console->acceptCode($request->get('code'));
return redirect()->action('SearchConsoleController#listSites', [$request]);
}
else{
die('error');
}
}
Now the version which i'm stucked wih
class SearchConsoleController extends Controller
{
private $console;
private $callback;
protected function __construct() {
$this->callback = route('searchconsole.callback') ;
}
private function setConsole($cliendId, $cliendSecret, $callback){
$console = new Console(new Google_Client(), $cliendId, $cliendSecret, $this->callback);
return $console;
}
public function index(Request $request) {
// VIEW WITH A FORM FROM WHICH I GET CLIENT_SECRET & CLIENT_ID var
return view('searchconsole.index');
}
public function getAuthUrl(Request $request) {
// FORM FROM INDEX IS SUBMITTED
$clientId = ($request->has('google-client-id')) ?
$request->get('google-client-id') :
null
;
$clientSecret = ($request->has('google-client-secret')) ?
$request->get('google-client-secret') :
null
;
$this->console = $this->setConsole($clientId, $clientSecret, $this->callback);
return $this->console->getAuthUrl();
}
public function callback(Request $request){
if ($request->has('code')) {
// ***** MY PROBLEM *********
$this->console->acceptCode($request->get('code')); // HERE $this->console IS NULL;
// *******************
return redirect()->action('SearchConsoleController#listSites', [$request]);
}
else{
die('error');
}
}
I just can't figure out how I can do this so console is still available
UPDATE :
following #iamab.in advice, i looked into Service Provider but i just dont know how i can instante the Console Object within the service provider.
Here's what i've done.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Helpers\Console;
use Google_Client;
use Illuminate\Support\Facades\Route;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(Console::class, function() {
$request = app(\Illuminate\Http\Request::class);
$clientId = ($request->has('google-client-id')) ?
$request->get('google-client-id') :
null
;
$clientSecret = ($request->has('google-client-secret')) ?
$request->get('google-client-secret') :
null
;
$callback = Route::get()->name('searchconsole.callback');
return new Console(new Google_Client(), $clientId, $clientSecret, $callback);
});
}
public function boot(){}
....
I just dont know how and where to implement it.
Thanks again
Update#2 :
okay my solution was working, I just didnt launch the correct app ..... 😅
I'm new to this package since I usually use the image intervention. So the problem is whenever I save an image, it is saving in storage directory instead of in public directory. I tried to review the documentation and did some research and didn't see any answer. Please see my code below for saving.
$user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
Package:
https://spatie.be/docs/laravel-medialibrary/v7/introduction
you can simply define the filesystem config with $user->addMediaFromRequest('avatar')->toMediaCollection('avatars', 'disk');
here is how im using the package(simple usage), if you need it later
on my model:
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;
class User extends Model implements HasMedia
{
use HasMediaTrait;
public function registerMediaCollections()
{
$this
->addMediaCollection('avatar')
->singleFile()
->useDisk('public');
->useFallbackUrl(asset('/images/default-user.png'))
->useFallbackPath(public_path('/images/default-user.png'));
}
public function registerMediaConversions(Media $media = null)
{
$this->addMediaConversion('avatar-thumb')->width(120)->height(120);
}
...
to get the file-url im using model accessor, added this function in my model:
public function getAvatarAttribute()
{
$file = $this->getMedia('avatar')->first();
if($file) {
$file->url = $file->getUrl();
$file->thumbnail = $file->getUrl('avatar-thumb'));
$file->alternate = $file->getCustomProperty('alternate') ?: $file->file_name;
}else{
$file = new \StdClass;
$file->url = $this->getFallbackMediaUrl('avatar');
$file->thumbnail = public_path('/images/default-user-thumb.png');
$file->alternate = 'avatar.png';
}
return $file;
}
also using custom path, in config\medialibrary.php set this array 'path_generator' => Path\To\Your\Class::class,
then the class :
use Spatie\MediaLibrary\Models\Media;
use Spatie\MediaLibrary\PathGenerator\PathGenerator as PathGenerators;
class PathGenerator implements PathGenerators
{
public function getPath(Media $media): string
{
return $this->getBasePath($media).'/';
}
public function getPathForConversions(Media $media): string
{
return $this->getBasePath($media).'/conversions/';
}
public function getPathForResponsiveImages(Media $media): string
{
return $this->getBasePath($media).'/responsive-images/';
}
protected function getBasePath(Media $media): string
{
//here im using trait to generate default path, e.g: path/mimes/avatar/media->id
//its up to you to define folder structure, just make sure each folder
//for conversions has unique name, or else it will be deleted
$base_folder = $this->get_base_folder($media->mime_type);
return "{$base_folder}/{$media->collection_name}/{$media->getKey()}";
}
}
I am using Magento 2.3.4 inside a docker container for a payment gateway extension. First things first, here is the affected code:
<?php
namespace Magento\PGM\Block;
use Magento\AdminNotification\Model\Inbox;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Response\Http;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\Order\Payment\Transaction;
use Magento\Sales\Model\Order\Payment\Transaction\Builder as TransactionBuilder;
use Magento\Sales\Model\OrderFactory;
use Magento\Store\Model\ScopeInterface;
use Magento\PGM\Logger\Logger;
class Main extends Template
{
protected $_objectmanager;
protected $checkoutSession;
protected $urlBuilder;
protected $response;
protected $config;
protected $messageManager;
protected $transactionBuilder;
protected $inbox;
private $logger;
private $orderFactory;
public function __construct(Context $context, Session $checkoutSession, OrderFactory $orderFactory = null, Logger $logger, Http $response, TransactionBuilder $tb, Inbox $inbox)
{
$this->checkoutSession = $checkoutSession;
$this->orderFactory = $orderFactory ?: ObjectManager::getInstance()->get(OrderFactory::class);
$this->response = $response;
$this->config = $context->getScopeConfig();
$this->transactionBuilder = $tb;
$this->logger = $logger;
$this->inbox = $inbox;
$this->urlBuilder = ObjectManager::getInstance()
->get('Magento\Framework\UrlInterface');
parent::__construct($context);
}
public function getParentId()
{
return $this->getData(OrderAddressInterface::PARENT_ID);
}
protected function _prepareLayout()
{
$method_data = array();
$order = $this->orderFactory->create()->load($this->getParentId());
if ($order) {
$payment = $order->getPayment();
// The error is thrown here (" Call to a member function setTransactionId() on null")
$payment->setTransactionId("-1");
...
$payment->save();
$order->save();
...
}
private function setApiData($order, $testmode, $instance)
{
...
}
}
I am getting this error:
Call to a member function setTransactionId() on null
I think that this is just a symptom though. The order object is not created, my IDE marks the $order->getPayment() method as not found at all.
The code itself should not be the problem, but the folder 'Sales\Model' does not contain an orderFactory.php file. Is the file missing or deprecated? Several modules use this file and create orders like this, for example the Paypal PGM, and use the OrderFactory.php file.
As i know The Factory class name is the name of Model class and append with the Factory word. So for our example, we will have TopicFactory class. You must not create this class. Magento will create it for you. Whenever Magento’s object manager encounters a class name that ends in the word ‘Factory’, it will automatically generate the Factory class in the var/generation folder if the class does not already exist. You will see the factory class in
ROOT/generated/code/<vendor_name>/<module_name>/Model/OrderFactory.php
So the first step you should go to the folder Generation to see the class is there or NOT.
If it's not there, i think you're are facing permission issue , magento cant generate (can't create file or folder) the Factory Class in Generation folder.
Hi orderFactory does not have payment in DB, so you cannot use this to get payment. You can try this:
use Magento\Sales\Model\ResourceModel\Order\Payment\Transaction\CollectionFactory;
protected $transactions;
public function __constructor(CollectionFactory $transactions)
{
$this->transactions = $transactions;
}
In your method:
$transactions = $this->transactions->create()->addOrderIdFilter($orderId);
...
$transactions->setTransactionId("-1");`
My Model is like this
class redModel extends Model {
public $table = 'tbl50red';
public $primaryKey = 'RedID';
public $timestamps = true;
}
and here is another model
class AddOnModel extends Model {
public $table = 'tbladdon';
public $primaryKey = 'AddOnID';
public $timestamps = true;
public function AppRed() {
return $this->hasMany("\App\Models\AddOn\Red\redModel", "AddOnID", "AddOnID");
}
}
In the controller when i run this:
$AddOns = AddOnModel
::with(array("AppRed" => function($query) use($StartDate, $EndDate) {
return $query->whereBetween("EarningDate", array($StartDate, $EndDate));
}))
->get();
I get an exception :
Class '\App\Models\AddOn\Red edModel' not found
Am I missing something?
You may try the given way...
use Illuminate\Database\Eloquent\Model;
class Application extends Model
{
public function AppRed()
{
return $this->hasMany(redModel::class);
}
}
This is not a bug in Laravel. Please read the PSR-1 Basic Coding Standard which will enable you to easily avoid this error in the future. Namely, this is the part you are violating:
Class names MUST be declared in StudlyCaps.
When I open below file:
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php
and checked this function
public function hasMany($related, $foreignKey = null, $localKey = null)
{
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related;
$localKey = $localKey ?: $this->getKeyName();
return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
}
I saw I was not able to instantiate the model because model name was starting with r . lol
and i was pulling my hair since morning and another coincident was that it was like this \r so it was missing the model name and I think replacing \r with blank space and due to that the url was becoming like this
\App\Models\AddOn\Red edModel
and actual was this
\App\Models\AddOn\Red\redModel
I think it's a bug in Laravel framework.
Change "\App\Models\AddOn\Red\redModel" to:
"\App\Models\AddOn\Red\\redModel"
i.e. the extra \ will escape the \r. \r is a control character, which is why you're getting an issue.
I'm trying to use Doctrine in Joomla 1.5 but have not been able to get anything running.
According to this article: http://magazine.joomla.org/issues/issue-may-2011/item/447-using-doctrine-ORM-in-joomla
I immediately get Fatal error: Class 'Fatal error: Call to undefined method JController::getInstance() in /var/www/html/hosts/joomla/public_html/components/com_bugs/bugs.php on line 13
The bugs.php looks like this:
// no direct access
defined('_JEXEC') or die; // Include dependancies
jimport('joomla.application.component.controller');
//require_once(JPATH_LIBRARIES . '/doctrine/vendor/autoload.php');
require_once(JPATH_LIBRARIES . '/doctrine/bootstrap.php');
require_once(JPATH_LIBRARIES . '/doctrine/JoomlaDoctrineBootstrapper.php');
require_once(JPATH_COMPONENT.DS.'controller.php');
//$controller = new BugsController(JRequest::getVar('task', ''));
$controller = JController::getInstance('Bugs');
Not sure how to implement this, when trying to use the $controller = new BugsController the error is: Fatal error: Class 'JController' not found in
This because I have the autoload on in bugs.php and have /public_html/components/com_bugs/controller.php extend /public_html/libraries/doctrine/JoomlaDoctrineBootstrapper.php the JoomlaDoctrineBootstrapper exends JController but JController cannot be found anymore after composer and the autoload did something.
I'm starting to think that it's not possible to use Joomla with Doctrine since Doctrine has to be installed with composer (didn't find any other documentation on how to download and configure it) and composer seems to want everything in vendor so have to put all the Joomla classes in vendor too?
[UPDATE]
It looks like whatever composer does in /public_html/libraries/doctrine/vendor/autoload.php completely breaks jimport('joomla.application.component.controller')
Not including the autoload however gives me another problem, like none of the Doctrine classes are found: Class 'Doctrine\Common\Cache\ArrayCache' not found
Maybe I'll try and hack /public_html/libraries/doctrine/vendor/composer/autoload_real.php to try and see if that one can load Joomla classes for me.
Either jimport or composer won't work because jimport defines __autoload. Instead of __autoload I'm using spl_autoload_register that only seems to work with PHP version starting from 5.1.2.
Changed loader:
/public_html/libraries/loader.php
class JLoader
{
public static function autoload($class)
{
if(JLoader::load($class)) {
return true;
}
return false;
}
//... other code and comments
function import( $filePath, $base = null, $key = 'libraries.' )
{
static $paths;
if (!isset($paths)) {
$paths = array();
//assuming PHP 5 >= 5.1.2
spl_autoload_register(array('JLoader', 'autoload'), true, true);
}
//remove the __autoload function
The bugs.php looks like this:
/public_html/components/com_bugs/bugs.php
<?php
// no direct access
defined('_JEXEC') or die; // Include dependancies
require_once(JPATH_LIBRARIES . '/doctrine/vendor/autoload.php');
require_once(JPATH_LIBRARIES . '/doctrine/bootstrap.php');
require_once(JPATH_LIBRARIES . '/doctrine/JoomlaDoctrineBootstrapper.php');
require_once(JPATH_COMPONENT.DS.'controller.php');
//using links like /index.php?option=com_bugs&format=text&task=save
// defaults to link so above is same as: http://joomla/index.php?option=com_bugs&format=text&task=save&router=link
$route=JRequest::getVar('router', 'Link');
$controllerName = 'bugsController'.$route;
//include the controller
include_once(dirname(__FILE__) . '/controllers/'.$route.".php");
$controller = new bugsControllerlink(JRequest::getVar('task', ''));
$controller->setEntityManager(bootstrapDoctrine());
$controller->execute(JRequest::getVar('task', ''));
$controller->redirect();
/**
* Initialize doctrine by setting the entities and proxies locaties. Also define
* a default namespace for the proxies.
*/
function bootstrapDoctrine() {
$doctrineProxy = new JoomlaDoctrineBootstrapper(JoomlaDoctrineBootstrapper::APP_MODE_DEVELOPMENT);
$doctrineProxy->setEntityLibrary(dirname(__FILE__) . '/models');
$doctrineProxy->setProxyLibrary(dirname(__FILE__) . '/proxies');
$doctrineProxy->setProxyNamespace('Joomla\Proxies');
$doctrineProxy->setConnectionOptions(getConfigurationOptions());
$doctrineProxy->bootstrap();
return $doctrineProxy->getEntityManager();
}
function getConfigurationOptions() { // Define database configuration options
$joomlaConfig = JFactory::getConfig();
return array('driver' => 'pdo_mysql', 'path' => 'database.mysql'
, 'dbname' => $joomlaConfig->getValue("config.data.db")
, 'user' => $joomlaConfig->getValue("config.data.user")
, 'password' => $joomlaConfig->getValue("config.data.password"));
}
?>
The link controller looks like: (file name has start with a capital L)
/public_html/components/com_bugs/controllers/Link.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library (done by composer)
//jimport('joomla.application.component.controller');
class bugsControllerlink extends JoomlaDoctrineBootstrapper{
function __construct()
{
parent::__construct();
$this->registerTask( 'show','show' );
$this->registerTask( 'save','save' );
}
function save(){
$model=$this->getModel('link');
$view = $this->getView('show','text');
$view->setLayout('save');
$model->em=$this->getEntityManager();
$view->data = $model->save();
$view->display();
}
}
Didn't change the boodtrap, that still looks like this:
/public_html/libraries/doctrine/bootstrap.php
<?php
interface JoomlaDoctrineController {
public function setEntityManager(Doctrine\ORM\EntityManager $entityManager);
}
?>
The /public_html/libraries/doctrine/JoomlaDoctrineBootstrapper.php looks like:
<?php
/** * Configuration class to integrate Doctrine into Joomla. *
* #author pderaaij <removed email, check link in question> */
use Composer\Autoload\ClassLoader,
Doctrine\ORM\EntityManager,
Doctrine\ORM\Configuration,
Doctrine\Common\Cache\ArrayCache;
jimport( 'joomla.application.component.controller' );
class JoomlaDoctrineBootstrapper extends JController{
const APP_MODE_DEVELOPMENT = 1;
const APP_MODE_PRODUCTION = 2;
private $applicationMode;
private $cache;
private $entityLibrary;
private $proxyLibrary;
private $proxyNamespace;
private $entityManager;
private $connectionOptions;
public function __construct($applicationMode=1) {
$this->applicationMode = $applicationMode;
$this->_name="bugs";
parent::__construct();
}
public function getConnectionOptions() {
return $this->connectionOptions;
}
public function setConnectionOptions($connectionOptions) {
$this->connectionOptions = $connectionOptions;
}
public function getProxyLibrary() {
return $this->proxyLibrary;
}
public function setProxyLibrary($proxyLibrary) {
$this->proxyLibrary = $proxyLibrary;
}
public function getProxyNamespace() {
return $this->proxyNamespace;
}
public function setProxyNamespace($proxyNamespace) {
$this->proxyNamespace = $proxyNamespace;
}
public function getCache() {
return $this->cache;
}
public function setCache($cache) {
$this->cache = $cache;
}
public function getEntityLibrary() {
return $this->entityLibrary;
}
public function setEntityLibrary($entityLibrary) {
$this->entityLibrary = $entityLibrary;
}
public function getApplicationMode() {
return $this->applicationMode;
}
public function setApplicationMode($applicationMode) {
$this->applicationMode = $applicationMode;
}
public function getEntityManager() {
return $this->entityManager;
}
public function setEntityManager($entityManager) {
$this->entityManager = $entityManager;
}
/** * Bootstrap Doctrine, setting the libraries and namespaces and creating * the entitymanager */
public function bootstrap() {
$this->registerClassLoader(); // Load cache
if ($this->getApplicationMode() == self::APP_MODE_DEVELOPMENT) {
$this->cache = new ArrayCache;
} else {
$this->cache = new ApcCache;
} /** #var $config Doctrine\ORM\Configuration */ $config = new Configuration;
$config->setMetadataCacheImpl($this->cache);
$driverImpl = $config->newDefaultAnnotationDriver($this->getEntityLibrary());
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($this->cache);
$config->setProxyDir($this->getProxyLibrary());
$config->setProxyNamespace($this->getProxyNamespace());
if ($this->applicationMode == self::APP_MODE_DEVELOPMENT) {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
} $this->entityManager = EntityManager::create($this->getConnectionOptions(), $config);
}
/** * Register the different classloaders for each type. */
private function registerClassLoader() { // Autoloader for all the Doctrine library files
//Doctrine was done by public_html/libraries/doctrine/vendor/autoload.php
// $classLoader = new ClassLoader('Doctrine', dirname(__FILE__) . '/');
// $classLoader->register(); // Autoloader for all Entities
//name of ComposerAutoloader is defined in /public_html/libraries/doctrine/vendor/composer/autoload_real.php
$modelLoader = ComposerAutoloaderInit825f56ea1383e6b7fef7ea99c51fea36::getLoader();
$modelLoader->set("Entities\\",dirname(__FILE__)."/../../components/com_"
//not sure how to do the proxies yet, have to check this with production settings
// $proxiesClassLoader = new ClassLoader('Proxies', $this->getProxyLibrary());
// $proxiesClassLoader->register();
}
}
?>
The Joomla model save function looks something like this (checking received JSON should be done in a controller or helper function):
public function save() {
//a textbox having the name 'json' or xhr post
$link = JRequest::getVar('json',false,'post');
if($link==false){
return;
}
$link = json_decode($link);
$newLink = new Link();
$newLink->setId($link->id);
$newLink->setName($link->name);
foreach($link->categories as $category){
$cat = new Category();
$cat->setId($category->id);
$cat->setName($category->name);
$newLink->addCategorie($cat);
}
$this->em->persist($newLink);
$this->em->flush();
return $link;
}
I guess the code as is will break when using APP_MODE_PRODUCTION in /public_html/components/com_bugs/bugs.php