Attempted to load class "EntityRepository" from namespace - symfony

I am a beginner in symfony 3. I currently have problems after installing FOSUserBundle. By creating my User entity I get this error: Error when access to app_dev.php
Here my entity User.php :
namespace SocialclockBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="SocialclockBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
And my UserRepository.php :
use Doctrine\ORM\EntityRepository;
namespace SocialclockBundle\Repository;
/**
* UserRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class UserRepository extends EntityRepository
{
}
Thank for your help !!!
florian

You need to put the use after the namespace
namespace SocialclockBundle\Repository;
use Doctrine\ORM\EntityRepository;

Related

Unable to find the object manager associated with an entity of class

I am developing an application with symfony 2.8 with the bundle "FOSUSERBUNDLE", my problem is when I try to register a user, I can see the registration form, but when registering I throw the following error:
Unable to find the object manager associated with an entity of class
"Alienigena\ViviendaBundle\Entity\User".
I follow step by step the official tutorial of "FOSUSERBUNDLE", my USER class is:
namespace Alienigena\ViviendaBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
My problem originated because at the moment of mapping the bd with reverse engineering I did with the mapping in xml, I should have mapped it with annotations.
the solution consisted in eliminating the files with extension xml that were generated with the inverse engineering.
Thanks

SonataUserBundle oneToMany Mapping Error

I dont know what else to try, this seems like it should be so straight forward, but the Symfony profiler continues to show this error:
Here are my User and Venue classes:
User:
namespace Application\Sonata\UserBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Venue", mappedBy="user")
*/
protected $venues;
public function __construct()
{
parent::__construct();
$this->venues = new ArrayCollection();
}
}
Venue:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Venue
*
* #ORM\Table()
* #ORM\Entity
*/
class Venue
{
/**
* #ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="venues")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
It seems to work fine in the application, I only get the error inside the Symfony Profiler. It keeps telling me venues doesnt exist on User but Im looking right at it! What am I not doing correctly here?

Some difficulties to understand a namesapace and his use

I haves some difficulties to understand this namespace's use :
namespace Utilisateurs\UtilisateursBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
I have found a namespace called FOS (in /Applications/MAMP/htdocs/ecommerce/vendor/friendsofsymfony/user-bundle) :
namespace FOS\UserBundle;
But I don't understand \Entity\User.
This is the code of my Entity :
<?php
// src/Acme/UserBundle/Entity/User.php
namespace Utilisateurs\UtilisateursBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="Utilisateurs")
*/
class Utilisateurs extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
When I will understand this namespace, I will be able to fix this problem :
Fatal error: Class 'FOS\UserBundle\Entity\User' not found in /Applications/MAMP/htdocs/ecommerce/src/Utilisateurs/UtilisateursBundle/Entity/Utilisateurs.php on line 14
[2015-08-16 17:19:57] php.CRITICAL: Fatal Error: Class 'FOS\UserBundle\Entity\User' not found {"type":1,"file":"/Applications/MAMP/htdocs/ecommerce/src/Utilisateurs/UtilisateursBundle/Entity/Utilisateurs.php","line":14,"level":-1,"stack":[]}
Fatal error: Class 'FOS\UserBundle\Propel\om\BaseUser' not found in
/Applications/MAMP/htdocs/ecommerce/vendor/friendsofsymfony/user-bundle/Propel/User.php
on line 18
I recommend you to watch PHP namespaces in 120 seconds screencast.

I give not a valid entity or mapped super class

i am trying to create entity via cli. When i try to cretate getter/setter via console it gives this error: Doctrin\ORM\Mapping\MappingException
Class SfTuts\JobeetBundle\Entity\Job is not valid entity or mapped super class
Here is my code:
<?php
namespace SfTuts\JobeetBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="job")
*/
class Job
{
/**
* #ORM\Id #Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
}
How can i solve this problem? Where is my fault? Thanks.
Check do you have this option in your config.yml file
doctrine:
orm:
auto_mapping: true
Also your column declaration is wrong. It should be #ORM\Column not #Column becouse you are using Doctrine\ORM\Mapping namespace for annotations, not SfTuts\JobeetBundle\Entity
namespace SfTuts\JobeetBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="job")
*/
class Job
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
}

Why __construct method don't work in User Entity in FOSUserBundle?

I am using the FOSUserBundle.
This is the User Entity:
namespace Shop\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
protected $shop;
public function __construct()
{
$this->shop = 'shop';
parent::__construct();
}
public function getShop()
{
return $this->shop;
}
}
When I call getShop in the controller:
$user = $this->getUser()->getShop()
A result is null
Why does not __construct work in User class?
I expected to have 'shop' string as default
You can put a callback to iniatilize your User. Just two annotations #ORM\HasLifecycleCallbacks for the entity and #ORM\PostLoad for the method. For example:
namespace Shop\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #ORM\HasLifecycleCallbacks
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
protected $shop;
/**
* #ORM\PostLoad
*/
public function init()
{
$this->shop = 'shop';
parent::init();
}
public function getShop()
{
return $this->shop;
}
}
Basically __construct in doctrine entity is called directly in end user code. Doctrine does not use construct when fetch entities from database - please check that question for more details. You can persist this in database by adding mapping if you want.

Resources