So I use my fork of CalendarBundle (http://github.com/permanaj/CalendarBundle), I install it, configure it, and extend it, because it usually the way to use a bundle. But when I try to run command it return error:
[Doctrine\ODM\MongoDB\Mapping\MappingException]
Class Rizza\CalendarBundle\Document\Event is not a valid document or mapped
super class.
I already create a Document that extends the parent CalendarBundle Document, but it still return the same error. Should the parent document also use #MongoDB annotation? or just the child document? How to fix this problem?
Related
I just want to ask if there's a way to retrieve the root directory of a Symfony Application ANYHWERE?
What I mean by anywhere is, in any file of my App.
I've searched everywhere and all I get is this:
$this->get('kernel')->getRootDir();
Which of course works! But I can't use it in my custom classes. I need to get the root directory in one of my custom classes.
I've already read answers about DependencyInjection/Service and other stuff, but I think it's too complex/overkill to implement those just to solve my current problem.
I just want the root directory of my app, period. Is there any other way?
The simplest way I can think of is to define a constant in your app.php file, like this:
define("ROOTDIR", $kernel->getRootDir());
so you can then use this constant anywhere. Compared to this, a static method is overkill, too.
I reviewed my answer. Indeed, it will not fit your need. Anyway, if you don't want to use dependency injection to achieve this goal because you have static methods, where do you call these static methods? In a controller? In a command? In another service? If you don't want to instanciate your class because you don't want objects with their own data, you have 2 options:
Get the root directory outside your class, and use it as a parameter for your static methods.
If your class uses static methods that means your class behave as a helper class, it is just a tool (converter, exporter, renderer...etc). So I assume that you placed all your helper classes in one directory. In this case you can create a enum class which defines constant like root dir, web dir giving the absolute paths.
I use this at the top of my class, that I run a test against:
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
require_once ExtensionManagementUtility::siteRelPath('my_ext') . 'Lib/ServiceLoader.php';
class Tx_MyOtherExt_Domain_Service_SearchService implements SingletonInterface
Then in my test I get an instance of that class, like this:
/** #var \Tx_MyOtherExt_Domain_Service_SearchService $service */
$service = GeneralUtility::makeInstance('\Tx_MyOtherExt_Domain_Service_SearchService');
However, the test throws an error:
Fatal error: main(): Failed opening required 'typo3conf/ext/my_ext/Lib/ServiceLoader.php' (include_path='C:/xampp/htdocs/my_proj/site/typo3/contrib/pear/;.;C:\xampp\php\PEAR')
How can I add the site root to the include path? The PHPSTORM settings (Settings->PHP) don't seem to make a difference.
NOTE: adding the site root to the include_path setting in php.ini works, of course. But as this path changes per project, setting this via PHPSTORM would be best.
How about using the constant PATH_site to get an absolute path?
https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/GlobalValues/Constants/Index.html
There are several things you should improve writing your code. First of all, it is also about changing the running code.
Use injection
If you need a separate class in your code, use e.g. constructor injection to inject the object into a property. By calling $this->getAccessibleMock with the 5th argument to false the original constructor won't call and you can fake the object by using `->_set('name', $obj)``
GeneralUtility::makeInstance
Don't use that in the tests but new(). If the tests breaks, your code is wrong and should be changed instead of building the test's code around it!
I am creating a personal package with some S4 classes. When I run the Build and Reload button in the Rstudio IDE I get the next message.
in method for ‘checkNewItems’ with signature ‘"webSource"’: no definition for class "webSource"
The Class declaration webSource is in a different file where the checkNewItems method is and I am guessing that is the reason why I am getting that message. In the source code that I have makes more sense to have the class declaration in other place rather than next to the methods checkNewItems.
Which is the idiomatic workaround that R programmers use for this?
Also, from the point of view of the Lazyloading that R uses I assumed that this should not happen.
You should export your class. In your namespace file you add this:
exportClasses(webSource)
I'm relatively new to Symfony2, so I'm learning by doing. My controller classes are getting pretty big. I'd like to break it up with functions() or objects->method(). Unfortunately I can't figure out where to put the code. (actually its really simple functions... but I can wrap that in an object...)
--I can't add it to the bottom of my DefaultController.php file. It errors out, and not pretty code to boot, either inside or outside of the { }.
--I can't simply add a new NewObject.php file to the controller directory. That errors out. The error: FatalErrorException: ...NewObject not found.
--I've toyed with manual mods to ../app/autoload.php but that doesn't really make sense for a simple class add to my ./SRC/ bundle. Perhaps I should build a ./src/autoload.php file (similiar to ./vender/autoload.php) but the contents of that file don't make sense to me at all. I simply can't figure out how the AnnotationRegistry Loader works.
Am I missing something? This seems way too hard.. what I want is a wrapped up 'include' so I can use the class in dev and after deployment.
How do I include NewObject.php (and the accompanying $newObject->function() ) in my code?
I'm told I can add a service, yet that seems like outrageous overhead for such a seemingly simple task (again, all I'm trying to do is clean up my very long controller php code...)
thanks in advance for your advice.
So you've got a project structure that looks something like this, right?
project
-- app
-- bin
-- src
-- SomeName
-- SomeBundle
-- Controller
-- Entity
-- Resources
-- ...
-- vendor
-- web
And you're just looking to have some kind of "helper" class that's used throughout your bundle. Is that correct?
If so, then you can really put it wherever you want to inside your src/ directory... Just make sure that the class name matches the file name, and that the path to the file matches the namespace you define at the top of your PHP code.
Sometimes when I do this, I'll create a simple directory under my bundle called "Helper/". Other times, when the application is more complex, I might be a little bit more explicit. But here's what the first case would look like...
First, add your /Helper directory under your bundle, and create the class file:
project
-- app
-- bin
-- src
-- SomeName
-- SomeBundle
-- Controller
-- Entity
-- Helper
-- SomeHelper.php
-- Resources
-- ...
-- vendor
-- web
The contents of SomeHelper.php might look like this:
<?php
namespace SomeName\SomeBundle\Helper;
class SomeHelper
{
public function doSomething()
{
...
}
}
Because your namespace matches the file path, it gets autoloaded, so you don't need to worry about include statements. You can instantiate that class anywhere in your bundle, as long as you include a use statement:
<?php
namespace SomeName\SomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use SomeName\SomeBundle\Helper\SomeHelper;
class DefaultController extends Controller
{
public function indexAction()
{
...
$helper = new SomeHelper();
$helper->doSomething();
...
}
}
Regarding the usage of services... Yes, that might be overkill, depending on what you're using it for. It's helpful to create services when the class needs to be aware of the application around it. For example, if you're creating a service that emails a User, it might want to access your database through the Doctrine service, or it might want to log the email activity through the Monolog service.
However, if your class doesn't need to know about the application (referred to as the "service container"), for example if it's just used to transfer data, then a helper class is probably more appropriate.
I have a trivially small PHPUnit test that looks like this:
<?php
namespace VNN\PressboxBundle\Tests\Entity;
namespace VNN\PressboxBundle\Entity;
use VNN\PressboxBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Phactory\Sql\Phactory;
class UserTest extends EntityTest
{
public function testCreate()
{
Phactory::reset();
}
}
When I try to run it, I get this:
There was 1 error:
1) VNN\PressboxBundle\Entity\UserTest::testCreate
ErrorException: Runtime Notice: Non-static method Phactory\Sql\Phactory::reset() should not be called statically, assuming $this from incompatible context in /Users/jason/Web/pressbox/src/VNN/PressboxBundle/Tests/Entity/UserTest.php line 13
What's up with that? All the docs call it statically.
I'm doing this on Symfony 2.0, if that makes a difference.
The documentation says you should be using the top-level Phactory class directly under lib/--not the individual implementations such as Phactory/Sql/Phactory which get instantiated based on the PDO object you pass to setConnection. Change
use Phactory\Sql\Phactory;
to
require_once 'Phactory/lib/Phactory.php';
The main class is in the global namespace and doesn't require a use statement.
https://github.com/chriskite/phactory/issues/30
From the code, setConnection, define and create are not static functions but the README and website guide do not reflect that.
e.g. test code
https://github.com/chriskite/phactory/blob/next/tests/Phactory/Sql/PhactoryTest.php
use Phactory\Sql\Phactory;
...
$this->pdo = new \PDO("sqlite:test.db");
$this->phactory = new Phactory($this->pdo);
$this->phactory->define('user');
$this->phactory->reset();
I don't know when it has been changed.
Too late anyways...
The current version, 0.3.2, is not backward compatible with the static method structure that is documented.
Here is the breaking commit: https://github.com/chriskite/phactory/commit/d3b60eeedea955ab7b5803ec29446d19888d3849
Unfortunately, the documentation has not been updated on http://phactory.org and the pearhub repo is no longer available.
I would look to the tests for examples: https://github.com/chriskite/phactory/blob/next/tests/Phactory/Sql/PhactoryTest.php