get entity from user name FosuserBundle Symfony - symfony

i have entity Collab that extend from Fos\UserBundle\Model\User, i want to get the entity from the username that i login .
i my controller :
echo $this->getUser();
$collab->get('collab.collaborateurservice')->findCollaborateurByUserName($this->getUser());
echo $this->getEmailCollaborateur();
in my manager i defined i method :
public function findCollaborateurByUserName ($username){
return $this->getRepository()->findOneBy(array('username'=>$username));
}
i get this exception :
FatalErrorException: Error: Call to a member function get() on a non-object in
how can get this entity??

Your problem is in this lines:
return $this->getRepository()->findOneBy(array('username'=>$username));
$collab->get('collab.collaborateurservice')->findCollaborateurByUserName($this->getUser());
It should be:
return $this->getDoctrine()->getRepository('YourBundleNamespace:Collab')->findOneBy(array('username'=>$username));
$collab = $this->get('collab.collaborateurservice')->findCollaborateurByUserName($this->getUser()->getUsername());

What is $collab? You're getting Fatal Error trying to call get method on variable that was not properly initialized (non-object).

Related

Dependency component issue

I'm new in Symfony and after I finished course "Create your own PHP Framework" (which is based on symfony components), I wanted to expand my framework by inject constructor of controller by model, but I got stuck. I got following error.
Error:
Catchable fatal error: Argument 1 passed to Controller\HelloController::__construct() must be an instance of Model\TestModel, none given, called in /var/www/html/vendor/symfony/http-kernel/Controller/ControllerResolver.php on line 195 and defined in /var/www/html/src/Controller/HelloController.php on line 21
HelloController constructor
public function __construct(TestModel $testModel)
{
$this->testModel = $testModel;
}
In container
....
$containerBuilder->register('model', \Model\TestModel::class);
$containerBuilder->register('hello', \Controller\HelloController::class)
->addArgument(new Reference('model'));
....
Routes
$routes->add('index', new Routing\Route('/hello/{name}',array(
'name' => 'World',
'_controller' => 'Controller\HelloController::index'
)));
I will be grateful for every advice.
Thanks

symfony get container not in controller and without service

i have problem with get my class function to which i send files from terminal. It's see that:
terminal: // php src/AppBundle/Provider/CommissionCost.php test.csv
in CommissionCost i want only put data to my function in Parser/CommissionDataParser.php
global $kernel;
$data = $kernel->getContainer()->get('data.parser')->getData($argv[1]);
var_dump($data);
//Uncaught Error: Call to a member function getContainer() on null
// this example i see in stackoverflow in topic about container : )
service:
services:
data.parser:
class: AppBundle\Parser\CommissionDataParser
arguments: ["#doctrine.orm.entity_manager"]
You need to add this in the configure method:
$this->addArgument('arg1', InputArgument::REQUIRED, 'Your first arg');
Then, you can access to it in the execute method by:
$input->getArgument('arg1');

Symfony3 Hello World: Attempted to call an undefined method named "redirect"

I am getting started with Symfony 3 following the downloadable book. However I can't get past the basic Hello World function when using the Controller class to extend my index Action.
The error I receive is "Attempted to call an undefined method named "redirect" . I have followed the documentation successfully up to this point. It looks like my Hello Wrold app cannot reference the Symfony Controller Class.
The below works fine:
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class HelloController
{
/**
* #Route("/hello/{name}", name="hello")
*/
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
But when I extend the index Controller by adding the below two lines as per the documentation I get the error "Attempted to call an undefined method named "redirect" of class "AppBundle\Controller\HelloController"."
I added these two lines, including the Symfnoy controller class and returning with a redirect.
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
return $this->redirect('http://symfony.com/doc');

Symfony 2.5.6 error InvalidArgumentException: The service definition "event_dispatcher" does not exist

I'm trying to build my first Compiler Pass in Symfony 2. For now, I'm just trying to get the core event_dispatcher service from FrameWorkBundle inside a SampleBundle, but I get this error :
error InvalidArgumentException: The service definition "event_dispatcher" does not exist.
Here is the code for my compiler :
<?php
namespace Me\SampleBunlde\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class RegisterListenersPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition('event_dispatcher');
}
}
?>
I'm a bit surprised since I'm following step by step a professionnal Symfony book who assures me that I will find this service with that id.
I've done some researches about that, and I discovered that only the debug.event_dispatcher service was avaible. Then I checked for aliases and saw that there was a private Alias named 'event_dispatcher' pointing to debug.event_dispatcher. So I'm really confused about all that. And I'm wondering :
Why is the Alias private ? Do I need to set him Public or is it the wrong way ?
Why Symfony does not automatically interprets my event_dispatcher call ?
Thank you for your help !
Use findDefinition() instead of getDefinition(). findDefinition also looks for aliases.

Error "Call to a member function on a non-object" when loading Symfony2 fixtures

I'm implementing the classic Blog app with Symfony2, and the "app/console doctrine:fixtures:load" returns an error. My BlogFixtures.php file is like this:
<?php
namespace MGF\Bundles\WebBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use MGF\Bundles\WebBundle\Entity\Blog;
use MGF\Bundles\CRMBundle\Util\Util;
class BlogFixtures extends AbstractFixture implements FixtureInterface
{
public function load(ObjectManager $em)
{
$blog1 = new Blog();
$title = 'First post';
$blog1->setTitle($title);
$slug1 = Util::getSlug($title);
$blog1->setSlug($slug1);
$blog1->setImage('beach.jpg');
$blog1->setTags('symfony2, php, paradise, symblog');
$blog1->setCreated(new \DateTime('now'));
$blog1->setUpdated($blog1->getCreated());
$em->persist($blog1);
$author1 = $em->getRepository('MGFBCBundle:User')->findOneByUser('sarah');
$author1->addBlog($blog1);
$em->persist($author1);
$em->flush();
}
}
And the error:
app/console doctrine:fixtures:load
Careful, database will be purged. Do you want to continue Y/N ?Y
> purging database
> loading MGF\Bundles\WebBundle\DataFixtures\ORM\BlogFixtures
PHP Fatal error: Call to a member function addBlog() on a non-object in /var/www/METRO/src/MGF/Bundles/WebBundle/DataFixtures/ORM/BlogFixtures.php on line 33
Fatal error: Call to a member function addBlog() on a non-object in /var/www/METRO/src/MGF/Bundles/WebBundle/DataFixtures/ORM/BlogFixtures.php on line 33
I don't see where I go wrong. Any hints?
Thanks in advance.
The problem was that even though the user 'sarah' exists in the db from the fixtures, when trying to load fixtures again, db gets purged. So I needed to reference my users when created from the fixtures, and retrieve them by their reference, as explained here:
http://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures
Fixtures loading is working again.

Resources