Summary
My problem is connected with the fact that the entity field marked with #Gedmo\UploadableFilePath annotation is ignored by Symfony3. I am using the Uploadable behavior extension for Doctrine2.
CODE
In my entity I have:
/**
* #ORM\Column
* #Gedmo\UploadableFileName
*/
private $name;
/*
* #ORM\Column
* #Gedmo\UploadableFilePath
*/
private $path;
SYMPTOMS
At first, I have noticed though that the path column is not generated in MySQL.
Then I found out that whenever I delete name field I get the following error:
[Gedmo\Exception\InvalidMappingException] Class
"AppBundle\Entity\plik" must have an UploadableFilePath or Uploadable
FileName field.
Documentation
In doctrine-uploadable documentation I see that:
#Gedmo\Mapping\Annotation\UploadableFilePath: This annotation is used
to set which field will receive the path to the file. The field MUST
be of type "string". Either this one or UploadableFileName annotation
is REQUIRED to be set.
so it seems that I should be able to set $path field only.
Help request
Please advice why is UploadableFilePath field not being generated and why can't I delete the $name field?
It seems that I have made a simple typo. There was lack of one * in the comment line.
/**
* #ORM\Column
* #Gedmo\UploadableFilePath
*/
private $path;
It seems that symfony ignores annotations in such a case.
Related
I just fixed some things in my code. I'm now trying to validate my schema
php bin/console doctrine:schema:validate
Doctrine tells me my mapping is correct but my database schema is not. So I'm doing a
schema:update --dump-sql
which results in the same ALTER again and again, that I already performed many times.
Here is the ALTER :
ALTER TABLE migration_versions CHANGE version version VARCHAR(14) NOT NULL;
I did it (with --force), the entity is reflecting the change already :
**
* MigrationVersions
*
* #ORM\Table(name="migration_versions")
* #ORM\Entity
*/
class MigrationVersions
{
/**
* #var string
*
* #ORM\Column(name="version", type="string", length=14, nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $version;
I am correct right ? its varchar, lenght 14...
And so is it in my database
I don't think Im making a mistake here but I may be missing something.
Have you already verified that the server_version in the doctrine config file is correct? (config/packages/doctrine.yaml in symfony5)
It happened to me that I was using MariaDB (version 10.4.11-MariaDB - Source distribution) and in the file doctrine.yaml the server_version parameter had the value 5.7.
After I corrected that, the error no longer occurred.
Also you can check this question
I want validate an entity doctrine differently when the entity is created, updated or deleted.
There is an entity constraint validator in my entity class.
// src/AppBundle/Entity/AcmeEntity.php
use AppBundle\Validator\Constraints as AcmeAssert;
/**
* #AcmeAssert\CustomConstraint
*/
class AcmeEntity
{
// ...
protected $name;
// ...
}
In my CustomConstraint I want determine if the Entity will be updated, created or delete for execute a specific validator.
Using unit of work is a solution ?
What is the best way to make this?
I think this problematic is common in lot of application ?
Thank's all ;)
You could either use validation groups based on the submitted data or handle itwhen you create the form by passing the validation group.
For example, in your controller when you create the form;
$form = $this->createForm(new AcmeType(), $acme, ['validation_groups' => ['create']]);
Then you entity would be something like;
/**
* Get name
*
* #Assert\Length(min=2, max=11, groups={"create", "update"})
* #AcmeAssert\ContainsAlphanumeric(groups={"create"}) // only applied when create group is passed
* #return string
*/
public function getName()
{
return $this->name;
}
This is what validation groups are made for.
Since Symfony Forms read validations from entity annotations and use internally the Validator component you'd have a look at these articles in the documentation:
http://symfony.com/doc/current/form/validation_groups.html
http://symfony.com/doc/current/validation/groups.html
http://symfony.com/doc/current/validation/sequence_provider.html
Sometimes, I just need to place the value with a config from the end user, just like the database prefix, uploaded File maxSize like below, and etc...
/**
* File
*
* #ORM\Table(name="{projectName}media_file")
* #ORM\Entity(repositoryClass="FileRepository")
* #ORM\HasLifecycleCallbacks()
*/
class File
{
/**
* #var File
*
* #Assert\File(maxSize="{configFromYML}")
*/
protected $file;
What can I do for it? thanks
As far as I know, this is not possible: the configuration parameters are only available in the container.
For the table prefix, maybe you can use the following solution:
How to setup table prefix in symfony2
For the validation, I think the only way to do this is to create a custom validation constraint and to configure it as a service: http://symfony.com/doc/master/validation/custom_constraint.html#constraint-validators-with-dependencies
Hope this will help!
I am using Doctrine 2 MongoDB ODM, all working fine except 1 specific relation. What's obscure is that I have seemingly identical relations in the project and they all work just fine
namespace Project\Entities\World; // same NS as class, but this is after
// splitting functionality from Entity to MappedSuperclass, didn't work either
/**
* #ReferenceOne(targetDocument="Project\Entities\World")
* #var IWorld
*/
protected $world;
used in Project\Entities\PlayerCharacter (extends Project\Entities\World\Object mentioned above)
=>
namespace Project\Entities;
/**
* #Document(collection="worlds")
* #HasLifecycleCallbacks
*/
class World {
/**
* #ReferenceMany(targetDocument="PlayerCharacter")
* #var ArrayCollection
*/
protected $playerCharacters;
}
When I set the $world variable, it's fine in the PHP script. Even $objectManager->refresh($character), $character->getWorld() === $world turns out fine. But it does never appear in the database itself (die right after setting it and flush to make sure it is never changed by accident somewhere) on the PlayerCharacter's end, only on the World side
On the other hand
class PlayerCharacter {
/**
* #ReferenceOne(targetDocument="User")
* #var User
*/
protected $user;
}
=>
/**
* #Document(collection="users")
* #HasLifecycleCallbacks
*/
class User {
/**
* #ReferenceMany(targetDocument="PlayerCharacter")
* #var ArrayCollection
*/
protected $characters;
}
works
In simplified version:
- PlayerCharacter::$user <==(1:N)==> User::$characters
(and all others) are fine, while only
- PlayerCharacter::$world <==(1:N)==> World::$playerCharacters
works only on the World side
Looking at it many days, can't find anything different.
Tried renaming property names, no change
Hydrator entry for character--->world looks identically to others
When I add the entry semi-manually (via RockMongo), it works fine
Creating the "world" field as NULL makes no difference, with {} it says "Undefined index: $id", but I guess that's an expected behaviour
Entities separately work very fine too, it really just is this one relation
Is there anything I am missing/overlooked or what can I do to discover why is it not getting persisted
(will edit the post if there's need for more info)
Thank you!
Ok, the thing was there were many entries in the UoW some containing World => null and some World => ...WorldProxy, so the latter probably got overwritten
Using flush() before the assignment solved this
I am trying to get the Stof\DoctrineExtensionsBundle to run to be able to use DoctrineExtensions easily. However, the PHP interpreter tells me:
No identifier/primary key specified for Entity 'Stof\DoctrineExtensionsBundle\Entity\Translation'. Every Entity must have an identifier/primary key.
Does anyone know how to circumvent this problem? I guess it is possible somewhere in the annotations of Doctrine2, but I do not understand it that much and there already is an "orm:index" value (renaming it by "orm:Id", which seems to be the required value, does not work).
That’s the code of Translation entity class shipped with DoctrineExtensions:
/**
* Stof\DoctrineExtensionsBundle\Entity\Translation
*
* #orm:Entity(repositoryClass="Gedmo\Translatable\Entity\Repository\TranslationRepository")
* #orm:Table(
* name="ext_translations",
* indexes={#orm:index(name="translations_lookup_idx", columns={
* "locale", "object_class", "foreign_key"
* })},
* uniqueConstraints={#orm:UniqueConstraint(name="lookup_unique_idx", columns={
* "locale", "object_class", "foreign_key", "field"
* })}
* )
*/
class Translation extends AbstractTranslation
{
}
By the way, that’s the git repository if this helps anything. But I was not able to find this point within the documentation: https://github.com/stof/DoctrineExtensionsBundle
You can try to disable the stofdoctrineextensions in your app/config/config.yml before generate your entities getters/setters like this:
mappings:
StofDoctrineExtensionsBundle: false
Looks like the entities generator doesn't support external mapping yet.