Where to check if a class is loaded or not? - symfony

In HasRoleRepository.php :
use \XXX\MyBundle\Model\Role_RT;
use \XXX\MyBundle\Model\Role;
...
public static function toModelClass($hasRoleArray, $RefGroupeRtArray) {
$roles = array();
foreach($hasRoleArray as $hasRole) {
$role = $hasRole->getIdRole()->getId() === 1 ? new Role_RT() : new Role();
....
}
I have a fatal error : Error: Class 'XXX\MyBundle\Model\Role_RT' not found in \src\XXX\MyBundle\Repository\HasRoleRepository.php line 37
No problem with the Role class, it is just with the Role_RT class.
Same namespace in Role and Role_RT :
namespace XXX\MyBundle\Model;
I do not understand. this one is not loaded. why ? how ?
Thank you

It's considered a bad practice to use _ in Class Names meaning Symfony doesn't detected it (because it's build to prevent bad practices). Change your Model name to RoleRT and try again.

I agree with Splendonia. If you want to use Symfony (and most generally Composer) you will have to do with the PSR-0 naming conventions.
Please have a look to :
http://symfony.com/doc/current/contributing/code/standards.html#naming-conventions
http://www.php-fig.org/psr/psr-0/

Related

excluding tables with setExcludedDoctrineTables when loading fixtures with liip TestFixturesBundle

I'm trying to use Liip test bundle to load fixtures from code using the loadFixtures() method from FixturesTrait
However, I need to exclude the RESOURCE table that I don't want to be dropped in the process. If I understand correctly, this should be easy using the setExcludedDoctrineTables method according to the doc https://github.com/liip/LiipTestFixturesBundle/blob/master/doc/database.md
Unfortunately, when I run this code, the table RESOURCES gets dropped with all the others.
Can anybody see why ?
not sure if that's relevant but I'm using a mysql db in a separate docker container.
<?php
namespace App\Tests\Controller;
use Symfony\Component\Panther\PantherTestCase;
use Symfony\Component\Panther\Client;
use Facebook\WebDriver\WebDriverBy as By;
use Facebook\WebDriver\Exception\TimeoutException;
use Liip\FunctionalTestBundle\Test\WebTestCase;
use Symfony\Component\Panther\PantherTestCaseTrait;
use Liip\TestFixturesBundle\Test\FixturesTrait;
use App\Repository\UserRepository;
use App\DataFixtures\UserFixtures;
use App\DataFixtures\AccountFixtures;
abstract class AbstractPantherTest extends WebTestCase{
// use trait so we can combine Liip and Panther features
use PantherTestCaseTrait; // this is the magic. Panther is now available.
// provide fixtures loading feature
use FixturesTrait;
// #var Symfony\Component\Panther\Client
protected static $client;
//Initialize the test case
function setUp():void
{
static::bootKernel();
if(self::$client === null){
self::$client = self::createPantherClient(['browser' => PantherTestCase::FIREFOX]);
$this->setExcludedDoctrineTables(["RESOURCES"]);
$this->loadFixtures([
UserFixtures::class,
]);
// retrieve the test user
$userRepository = static::$container->get(UserRepository::class);
// retrieve the test user
$testUser = $userRepository->findOneByUsername('Administrator');
// simulate $testUser being logged in
self::doLogin($testUser->getUsername(), 'xxx');
}
}
}
Answering my own question as I just found out the issue. Sometimes a good night of sleep is all you need :)
So this code is actually working. The table RESOURCES got dropped because the setUp method was redefined in the concrete class implementing the test and that redefinition included another call to loadFixtures, but for a different data class and without the exclusion :
<?php
namespace App\Tests\Controller;
use App\DataFixtures\DiscountFixtures;
class DiscountControllerWebTest extends AbstractPantherTest
{
function setUp():void
{
parent::setUp();
$this->loadFixtures([
DiscountFixtures::class,
]);
}

using google analytics in laravel project

hey guys i'm trying to use google analytics in my project but i always get an error : Argument 1 passed to Spatie\LaravelAnalytics\LaravelAnalytics::__construct() must be an instance of Spatie\LaravelAnalytics\GoogleApiHelper, none given
actually i don't know how to deal with that error as it's my first time to use it .
that's simply the code i've written :
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Product;
use App\ProductTrans;
use App\Service;
use Spatie\LaravelAnalytics\LaravelAnalytics;
class HomeController extends Controller {
//
public function getIndex() {
$services = Service::get();
$products = Product::get();
$analyticsData =new LaravelAnalytics();
$analyticsData->getVisitorsAndPageViews();
dd($analyticsData);
return view('admin.pages.home',compact('services','products'));
}
so can anyone help me ?

Class Not Found Error with custom Class Concrete5.8

New to C5 and Namespaces. Trying to add my own class, but keep getting error that it can't find the class, even though PHPstorm seems to know where it is. Seems simple enough but I'm missing something.
Path to class application/src/cpi/funcs/Utils.php
Class
<?php
namespace Application\Src\Cpi\Funcs;
defined('C5_EXECUTE') or die(_("Access Denied."));
class Utils
{
public function Getcreditcard_year($y = 10)
{
$currentYear = date("Y");
$currentYearVal = date("y");
$year_list = array();
for ($i = 1; $i <= $y; $i++) {
$year_list[$currentYearVal] = $currentYear;
$currentYear = $currentYear + 1;
$currentYearVal = $currentYearVal + 1;
}
return $year_list;
}
}
?>
Code in View
<?php
use Application\Src\Cpi\Funcs\Utils;
$years = new Utils();
//$years->Getcreditcard_year();
?>
A few core features have been updated in Concrete5.8 that change the way you should use namespaces. You have two options to go about this.
Option 1:
Concrete5.8 has had some minor updates to its core Routing which now uses application/src/concrete/ instead of application/src/ to search for your custom classes, and is triggered to look in to that folder when a class has \Application\Concrete\ in its namespace. Basically, you would do the following:
Class
Update the namespace of your class and move it to application/src/Concrete/Cpi/Funcs/Utils.php
<?php
namespace Application\Concrete\Cpi\Funcs;
class Utils{
...
}
Code In View
use \Application\Concrete\Cpi\Funcs\Utils
...
Option 2
Re-enable the legacy Routing to read in application/src/ when \Application\Src\ is used in a class's namespace.
C5 core team have created a function to enable legacy namespaces, enableLegacyNamespace(), which you need call during the bootstrap. I've added the coded in my application/bootstrap/start.php file. Not sure if it is the best place to enable it but it works. The file now looks like this:
<?php
use Concrete\Core\Application\Application;
use Concrete\Core\Foundation\ClassLoader;
/*
* ----------------------------------------------------------------------------
* Instantiate concrete5
* ----------------------------------------------------------------------------
*/
// This line allows us now to use \Application\Src\ in our class namespace
ClassLoader::getInstance()->enableLegacyNamespace();
$app = new Application();
...
Using this option means you don't have to modify or move your class and can use as you have it set up currently.
Just sharing some advice
I have my concrete5.8 sites to use both options. For my custom classes, I use option 2 but when I want to overwrite or expand a core class I use option 1 and place the file inside application/src/concrete/ and use the \Application\Concrete\ namespacing.
e.g.
Say, I want to extend the core PageController class. First, I create the php file and add it under application/src/concrete/. The file path would look like this: application\src\Concrete\Page\Controller\PageController.php. I am trying to mimic the folder path of its core file which is found in concrete\src\Page\Controller\PageController.php.
Class
<?php
namespace Application\Concrete\Page\Controller;
...
class PageController extends \Concrete\Core\Page\Controller\PageController
{
...
}
Then overwrite the core alias pathing with your custom alias inside application/config/app.php so concrete knows to read from a custom class instead:
return [
'aliases' => [
// extend the page controller class
'PageController' => '\Application\Concrete\Page\Controller\PageController',
];
Hope this helps.
Cheers.
Gianni's answer is still valid on c5 8.5.1, incase anyone is wondering. The correct path is application/src/Concrete, not application/src/concrete. If the c is not capitalized, c5 will through an error for not being able to find your class.

Symfony $this->>createFormBuilder throws notice from controller

I am doing a small project in Symfony to gain some experience with this framework. At the moment I am just following tutorials while making my own tool which is a GUI to make my resume.
In my controller I am calling $this->createFormBuilder($cvBase) which is throwing an "Undefined property" notice which I think is strange because I am calling a method.
Does anyone know what is causing this?
<?php
// My php code
namespace NuiCart\CvToolBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use NuiCart\CvToolBundle\Entity\CvBase;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller{
public function formAction(Request $request){
$cvBase = new CvBase();
$form = new $this->createFormBuilder($cvBase);
The notice:
Notice: Undefined property:
NuiCart\CvToolBundle\Controller\DefaultController::$createFormBuilder
in
/var/www/devel/cv/public_html/src/NuiCart/CvToolBundle/Controller/DefaultController.php
line 19 500 Internal Server Error - ContextErrorException
createFormBuilder() already returns a newly created FormBuilder object and you therefore don't need to create a new Object. By attempting to use new, it is trying to create a new DefaultController and access a function that is private to the internals of the Controller (or something like that - don't sue me for correctness on this one as it is just an attempt to explain why using new $this is wrong.)
Your solution is to remove the new in $this->createFormBuilder():
$form = $this->createFormBuilder($cvBase);

Class not found when trying to index documents using Solr with Symfony2

I am very new to Solr and I am probably missing something simple however, having followed Xavier Briand's presentation I have set up Symfony2, Solarium and Nelmio\SolariumBundle.
"nelmio/solarium-bundle": "2.0.*#dev",
"solarium/solarium": "3.0.*"
Having implemented a toSolrDocument method for my doctrine php object.
public function toSolrDocument(\Solarium_Document_ReadWrite $doc)
{
$doc->id = $this->getId();
$doc->description = $this->getTitle();
$doc->path = "path";
return $doc;
}
I am faced with the following error.
Catchable Fatal Error: Argument 1 passed to ::toSolrDocument() must be an instance of Solarium_Document_ReadWrite, instance of Solarium\QueryType\Update\Query\Document given, called in Controller.php
The controller calling this toSolrDocument method has the following function
public function indexItems(){
$client = $this->get('solarium.client');
// get an update query instance
$update = $client->createUpdate();
// create documents
$documents = array();
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('<Bundle>:<Object>');
$items = $repo->findAll();
foreach ($items as $item) {
$documents[] = $item->toSolrDocument($update->createDocument());
}
// add the documents and a commit command to the update query
$update->addDocuments($documents);
$update->addCommit();
// this executes the query and returns the result
$result = $client->update($update);
}
Most of this method again comes directly from Xavier's presentation and it is clear to see that the method $update->createDocument() does not return the correct type. In fact it returns an instance of the my php object. Does anyone know where I am going wrong here? Another thing that might be of help is that even when I try to pass a Solarium Document directly I get an exception.
foreach ($items as $item) {
$rw_item = new \Solarium_Document_ReadWrite();
$documents[] = $item->toSolrDocument($rw_item);
}
The exception is
FatalErrorException: Error: Class 'Solarium_Document_ReadWrite' not found in
I can't seem to find this class in any of the bundles and I am wondering if my setup might be causing the issues. Any help would be very much appreciated.
One additional point to note is that when I am running the solr jar I see the query requests come in from my symfony2 page it is only this indexing action that I can not work out so the config may be alright and I am miss understanding the use of the bundle.
You just need to use the correct class for the argument.
use Solarium\QueryType\Select\Result\AbstractDocument;
...
public function toSolrDocument(AbstractDocument $doc)
{
You could also not type hint it:
public function toSolrDocument($doc)
{

Resources