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.
Related
I'm using Symfony 3.4 and knp doctrine behaviors for translation.
My entity Article looks like:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* Article
*
* #ORM\Table(name="article")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository")
*/
class Article
{
use ORMBehaviors\Translatable\Translatable;
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
//...
}
Then I have entity ArticleTranslation
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* #ORM\Entity
*/
class ArticleTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* #ORM\Column(type="string", length=128)
*/
protected $headline;
//...
}
Now my app throws me an error:
Unable to find the association target class of "headline" in AppBundle\Entity\Article.
It expects a relation between Article and ArticleTranslation. There is a sentence in the documentation:
The default naming convention (or its customization via trait methods) avoids you to manually handle entity associations. It is handled automatically by the TranslationSubscriber.
Why does this happen? What am I missing?
edit
bin/console doctrine:schema:update
[OK] Nothing to update - your database is already in sync with the current entity metadata.
bin/console debug:config knp_doctrine_behaviors
knp_doctrine_behaviors:
translatable: true
blameable: false
geocodable: false
loggable: false
sluggable: false
soft_deletable: false
sortable: false
timestampable: false
tree: false
I'm using this in sonata admin, with a2lix translatable.
ArticleAdmin.php:
<?php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
final class ArticleAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('headline', TranslationsType::class);
}
//...
}
Try to use
$formMapper->add('translations', TranslationsType::class);
I had the same problem. Probably the field name needs to be exactly translations.
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
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;
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?
I have a custom validator in src/VNN/PressboxBundle/Component/Validator/CurrentPasswordValidator.php:
<?php
namespace VNN\PressboxBundle\Component\Validator\Constraints;
use Symfony\Component\Validator\ConstraintValidator,
Symfony\Component\Validator\Constraint,
Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface,
Symfony\Component\Security\Core\SecurityContextInterface,
JMS\DiExtraBundle\Annotation\Validator,
JMS\DiExtraBundle\Annotation\InjectParams,
JMS\DiExtraBundle\Annotation\Inject;
/**
* #Validator("user.validator.current_password")
*/
class CurrentPasswordValidator extends ConstraintValidator
{
// ...
}
And then I have this at src/VNN/PressboxBundle/Component/Validator/Contraints/CurrentPassword.php:
<?php
namespace VNN\PressboxBundle\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class CurrentPassword extends Constraint
{
public $message = "Your current password is not valid";
/**
* #return string
*/
public function validatedBy()
{
return 'user.validator.current_password';
}
}
For some reason, when I try to add an annotation that uses this validator, I get an error. Here's my entity/annotation:
<?php
namespace VNN\PressboxBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinTable as JoinTable;
use Doctrine\ORM\Mapping\JoinColumn as JoinColumn;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContext;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\MaxLength;
use Symfony\Component\Validator\Constraints\Email;
use VNN\PressboxBundle\Component\Validator\Constraints\CurrentPassword;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* VNN\PressboxBundle\Entity\User
*
* #ORM\Table(name="user")
* #ORM\Entity
*/
class User implements UserInterface, \Serializable
{
/**
* #var string $password
* #Assert\NotBlank()
* #CurrentPassword()
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
}
The error I'm getting is:
AnnotationException: [Semantical Error] The annotation
"#VNN\PressboxBundle\Component\Validator\Constraints\CurrentPassword"
in property VNN\PressboxBundle\Entity\User::$password does not exist,
or could not be auto-loaded.
What am I doing wrong?
Check that VNN\PressboxBundle\VNNPressboxBundle() is correctly registered with the kernel in AppKernel.php.
Update: If it is correctly registered, and src/ is registered as a fallback with the autoloader (assuming VNN/ is under src/), you may want to try and load the service yourself manually and check the stack trace if/when it fails:
$validator = $container->get('user.validator.current_password');
// Pretending this is a unit test:
// $this->assertInstanceOf('VNN\PressboxBundle\Component\Validator\Constraints\CurrentPasswordValidator', $validator);
Also, make sure you have cleared the cache (if not testing in dev environment).
Side note (but unrelated to your problem) - if you start to add more validators, it will be more useful to use this syntax:
<?php
namespace VNN\PressboxBundle\Entity;
use VNN\PressboxBundle\Component\Validator\Constraints as VNN;
class User implements UserInterface, \Serializable
{
/**
* #VNN\CurrentPassword
*/
private $password;
}