When I use:
php app/console doctrine:fixtures:load --fixtures=/var/www/Symfony/src/BISSAP/ForumBundle/DataFixtures/ORM**
I get the following error:
PHP Catchable fatal error: Argument 1 passed to
BISSAP\ForumBundle\Entity\Forum::setCategory() must be an instance of
BISSAP\ForumBundle\Entity\Category, null given, called in
/var/www/Symfony/src/BISSAP/ForumBundle/DataFixtures/ORM/LoadForum.php
on line 40 and defined in
/var/www/Symfony/src/BISSAP/ForumBundle/Entity/Forum.php on line 184
My Fixture - LoadForum.php:
<?php
namespace BISSAP\ForumBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BISSAP\ForumBundle\Entity\Forum;
use BISSAP\ForumBundle\Entity\Category;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LoadForum extends Controller implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$data=array(array('NAME','DESCRTIPTION','60',$manager->getRepository('BISSAPForumBundle:Category')->find('1')),
array('NAME2','DESCRTIPTION2','60',$manager->getRepository('BISSAPForumBundle:Category')->find('2')));
foreach ($data as $for) {
$forum = new Forum();
$forum->setName($for[0]);
$forum->setDescription($for[1]);
$forum->setOrdre($for[2]);
$forum->setCategory($for[3]);
$manager->persist($forum);
}
$manager->flush();
}
}
doctrine:fixtures:load erases all data from DB and loads new set of fixtures
I believe your problem is
$manager->getRepository('BISSAPForumBundle:Category')->find('1')
that return empty result instead of Category object
And it looks like either you loads Forum fixtures before Category or you didn't considered that DB was erased and you don't have any records for Category.
for case 1 you should change order of fixtures loading - change the function "getOrder" for Category fixtures and set returned number lower then number at Forum
for case 2 you should also create fixtures for some categories
BTW you should use a reference to the object, instead of picking up from the repository, so common way is:
Create new reference for the Category fixture
$category = new \MyApp\CategoryBundle\Entity\Category();
$category->setName();
$this->addReference('MyApp\CategoryBundle\Entity\Category-1',$category);
Call created reference to fill Forum
$forum->setCategory($this->getReference('MyApp\CategoryBundle\Entity\Category-1'));
Related
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);
I wanna throw an HTTP_NO_CONTENT, but symfony tells me that ClassNotFoundException: Attempted to load class "Codes" from namespace "FOS\Rest\Util". Do you need to "use" it from another namespace?
I actually did use FOS\RestBundle\Util\Codes; in my class.
My call looks like this
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$player = $em->getRepository('xxx')->find($id);
$em->remove($player);
$em->flush();
return $this->view(null, new HTTP_NO_CONTENT);
}
Why is it not possible to find that class?
new HTTP_NO_CONTENT doesn't make sens here! You've to use FOS\RestBundle\Util\Codes class,
use FOS\RestBundle\Util\Codes;
and point to the right status code as follow,
Codes::HTTP_NO_CONTENT // As HTTP_NO_CONTENT is defined as a constant
How do I access my database via doctrine in a BeforeFeature? I am unable to get my entity manager because my kernel is null...this is what I am trying:
/**
* #BeforeFeature
*/
public static function cleanDatabase(FeatureEvent $event)
{
$context = new FeatureContext(array());
$context->thereAreNoUsersInTheDatabase();
}
It tells me that I cannot get container from a non-object (the kernel). Why isn't KernelAwareInterface assigning the kernel when I manually create that FeatureContext?
KernelAwareInterface just provides a method "setKernel" method, you still have to call manually if you instantiate a new FeatureContext object
You can use FriendlyContext that provide you a "#reset-schema" annotation. See https://github.com/KnpLabs/FriendlyContexts/blob/master/doc/context-entity.md#reset-schema .
Do not hesitate to read the full documentation and the code that doesn't uses the SymfonyExtension.
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)
{
You know that in Symfony2 a new entity can be defined as in the following example:
use Acme\StoreBundle\Entity\Product;
public function defaultController() {
$product = new Product();
$product->setName('Pippo');
$product->setPrice(19.99);
....
// Use Doctrine EntityManager to store the Product object
}
Suppose that you know that the Product class has the following namespace: "AcmeHomeBundle:Product". It would by nice to create the $product object by using the namespace (e.g. by using the EntityManager or something similar).
public function defaultController() {
$item = createObjectFromNamespace("AcmeHomeBundle:Product");
$item->setName('Pippo');
$item->setPrice(19.99);
....
// Use Doctrine EntityManager to store the Item object
}
Do you know if this is possible?
Suppose that you have a string that provides the entity type
You should do this...
$entityInfo = $this->em->getClassMetadata("entityNameSpace:entityName");
$entityMember = $entityInfo->newInstance();
If you wanna use a setter method by string:
$entitySetMethod = "set".\ucfirst("entityDataMemberName");
\call_user_func(array($entityMember, $entitySetMethod), $parameter);
If you really want to, you can do this:
$product = new Acme\JournalBundle\Entity\Product();
$article = new Acme\JournalBundle\Entity\Article();
But you'd have to type it out every time you wanted to create a new entity in that namespace. If you simply used a use statement at the top of you class:
use Acme\JournalBundle\Entity\Product,
Acme\JournalBundle\Entity\Article;
You could then create new articles and products with a simple:
$product = new Product();
$article = new Article();
They do the same thing.
Acme\StoreBundle\Entity\Product IS the namespace of your entity. AcmeStoreBundle:Product is just an alias for the namespace to be used in DQL as a shorter alternative to the real namespace.
Why would you want to create objects with aliased namespace? I suppose you could create some kind of a factory using alias to map it to a real namespace, create an object and return it. But what's the point?
Entity aliases are defined via Configuration: http://www.doctrine-project.org/api/orm/2.2/source-class-Doctrine.ORM.Configuration.html#153
you can not only set them but also retrieve, so if you really need this functionality you should be able to do this with Configuration instance.
It's hard to find anything about entity aliases in Doctrine docs. Symfony docs explain the purpose of it a little:
alias - Doctrine offers a way to alias entity namespaces to simpler, shorter names to be used in DQL queries or for Repository access. When using a bundle the alias defaults to the bundle name.