Doctrine\Common\Annotations\DocLexer::T_CLOSE_PARENTHESIS, got 'datetime' - doctrine

[Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_CLOSE_PARENTHESIS, got 'datetime' at position 52 in property Application\Entity\catalogTemplateVersion::$validUntil.
Here ist my Entity:
/**
*
* #var \DateTime #ORM\Column(name="valid_until, type="datetime")
*/
private $validUntil;

I found the BUG. I have not closed the annotation(name).
... name="valid_until ...

Expected Doctrine\Common\Annotations\DocLexer::T_CLOSE_PARENTHESIS, got 'name' at position 27 in property
Answer:
In this you have missed " string in your column
/**
*
* #var \DateTime
* #ORM\Column(name="valid_until, type="datetime")
*/
private $validUntil;
to
/**
*
* #var \DateTime
* #ORM\Column(name="valid_until", type="datetime")
*/
private $validUntil;
Note
This issue always appear when you missed "" or , in your annotation
I hope it will helpful

/**
* #var null|\DateTime
*
* #ORM\Column(name="valid_until", type="datetime")
*/
private $validUntil;

Related

Symfony 5 - Doctrine - Index not created in custom ManyToOne relation

I've created two Entity, and i try to create a custom relation between them, without using the default syntax.
See :
/**
* #ORM\Entity(repositoryClass=LandRepository::class)
* #ORM\HasLifecycleCallbacks()
*/
class Land
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $libelle;
/**
* #ORM\Column(type="integer")
*/
private $uid;
/**
* #ORM\OneToMany(targetEntity=Ride::class, mappedBy="uidparent")
*/
private $rides;
}
/**
* #ORM\Entity(repositoryClass=RideRepository::class)
* #ORM\HasLifecycleCallbacks()
*/
class Ride
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $libelle;
/**
* #ORM\ManyToOne(targetEntity=Land::class, inversedBy="rides")
* #ORM\JoinColumn(name="uidparent", referencedColumnName="uid")
*/
private $uidparent;
}
Tables are correctly created, but the last instruction have an error.
In MySQL, i made some test, and i need to create an index on "uid" column in "land" table.
So, i changed the header of my class "Land" to force the index
/**
* #ORM\Entity(repositoryClass=LandRepository::class)
* #ORM\Table(name="land",indexes={#ORM\Index(columns={"uid"})})
* #ORM\HasLifecycleCallbacks()
*/
class Land
{
/ ... /
}
I don't understand why i need to specify this index creation.
I hope to have something like this :
(PS : I know i can use the classic syntax (using in my entity Ride a column auto named "land_id") but I want to understand and master this alternative syntax to manage future complex entities and associations)
Thanks !
Need to manually specify in Entity header annotation :
#ORM\Table(name="land",indexes={#ORM\Index(columns={"uid"})})

Symfony3 Error manipulating time duration with doctrine

I want to use a variable time as the amount of time passed to do something:
/**
* #var \DateTime
*
* #ORM\Column(name="TempsPasse", type="time", nullable=true)
*/
public $tempsPasse;
Code:
$Information->setTempsPasse(new \DateTime($Log->getTempsPasse()->format('H:i:s')));
$Information->setTempsPasse(clone($Log->getTempsPasse()));//Same result
$Information->setTempsPasse(new \DateTime('01:00:00'));//Same result
Where echo($Log->getTempsPasse()->format('H:i:s')); show: 01:00:00
Then when I flush I get this error:
FatalErrorException in TimeType.php line 53: Error: Call to a member
function format() on a non-object
Where am I wrong and how can I handle time with doctrine ?
EDIT
/**
* #var \DateTime
*
* #ORM\Column(name="TempsPasse", type="time")
*/
public $tempsPasse;
/**
* Set tempsPasse
*
* #param \DateTime $tempsPasse
*
* #return Log
*/
public function setTempsPasse($tempsPasse)
{
$this->tempsPasse = $tempsPasse;
return $this;
}
/**
* Get tempsPasse
*
* #return \DateTime
*/
public function getTempsPasse()
{
return $this->tempsPasse;
}
That's the same code for both entities.
I wonder if this works:
$Information->setTempsPasse( new \DateTime($Log->getTempsPasse()) );
The new \DateTime may be expecting a DateTime object...
Otherwise, can you edit your post and show how you create the $Log object first, and the code leading up to that.
The one thing I'm thinking is when you create the new \DateTime it is expecting line this:
new \DateTime( '01:00:00' )
Notice the quotes around the time parameter.

Symfony3 Edit Entity : Error Missing value for primary key

I try to create a tree structure for a catalog of products.
A catalog can have multiple levels and levels can contain multiple products.
I manage to save my structure in database but when I want to edit it, I have this error :
Error : Missing value for primary key catalogCode on AppBundle\Entity\CatalogLevel
at OutOfBoundsException ::missingPrimaryKeyValue ('AppBundle\Entity\CatalogLevel', 'catalogCode')
in vendor\doctrine\common\lib\Doctrine\Common\Proxy\AbstractProxyFactory.php at line 125
when I do this in my CatalogController :
$form = $this->createForm(CatalogTreeType::class, $catalog);
But, just before that line, I verify if I get my levels correctly and it's looking like that's the case :
// Create an ArrayCollection of the current levels
$originalLevels = new ArrayCollection();
foreach ($catalog->getLevels() as $level) {
var_dump($level->getCatalogCode());
$originalLevels->add($level);
}
// returns
AppBundle\Controller\CatalogController.php:337:string 'TT-FTEST' (length=8)
AppBundle\Controller\CatalogController.php:337:string 'TT-FTEST' (length=8)
CatalogLevel entity has a composite key : levelId + catalogCode.
Considering the primary key catalogCode isn't empty, I don't understand this error...
Catalog Entity
/**
* #ORM\Table(name="catalogue")
* #ORM\Entity(repositoryClass="AppBundle\Entity\CatalogRepository")
* #UniqueEntity(fields="code", message="Catalog code already exists")
*/
class Catalog
{
/**
* #ORM\Column(name="Catalogue_Code", type="string", length=15)
* #ORM\Id
* #Assert\NotBlank()
* #Assert\Length(max=15, maxMessage="The code is too long ({{ limit }} characters max)")
*/
private $code;
/**
* #ORM\OneToMany(targetEntity="CatalogLevel", mappedBy="catalog", cascade={"persist", "remove"})
* #Assert\Valid
*/
private $levels;
/**
* Constructor
*/
public function __construct()
{
$this->levels = new ArrayCollection();
}
/**
* Get levels
*
* #return ArrayCollection
*/
public function getLevels()
{
return $this->levels;
}
/**
* Add level
*
* #param \AppBundle\Entity\CatalogLevel $level
*
* #return Catalog
*/
public function addLevel(\AppBundle\Entity\CatalogLevel $level)
{
$level->setCatalogCode($this->getCode());
$level->setCatalog($this);
if (!$this->getLevels()->contains($level)) {
$this->levels->add($level);
}
return $this;
}
/**
* Remove level
*
* #param \AppBundle\Entity\CatalogLevel $level
*/
public function removeLevel(\AppBundle\Entity\CatalogLevel $level)
{
$this->levels->removeElement($level);
}
}
CatalogLevel Entity
/**
* #ORM\Table(name="catalogue_niveau")
* #ORM\Entity(repositoryClass="AppBundle\Entity\CatalogLevelRepository")
*/
class CatalogLevel
{
/**
* #ORM\Column(name="Niveau_ID", type="string", length=15)
* #ORM\Id
*/
private $id;
/**
* #ORM\Column(name="Catalogue_Code", type="string", length=15)
* #ORM\Id
*/
private $catalogCode;
/**
* #ORM\ManyToOne(targetEntity="Catalog", inversedBy="levels")
* #ORM\JoinColumn(name="Catalogue_Code", referencedColumnName="Catalogue_Code")
*/
private $catalog;
/**
* Set id
*
* #param string $id
*
* #return CatalogLevel
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* #return string
*/
public function getId()
{
return $this->id;
}
/**
* Set catalogCode
*
* #param string $catalogCode
*
* #return CatalogLevel
*/
public function setCatalogCode($catalogCode)
{
$this->catalogCode = $catalogCode;
return $this;
}
/**
* Get catalogCode
*
* #return string
*/
public function getCatalogCode()
{
return $this->catalogCode;
}
}
I would like to remind you that this error occured on the editAction (it works very well on the addAction) when I display the pre-filled form.
Thanks for your help !
I think that is because you haven't autoincrement id in entity CatalogLevel. Try add to id this code:
#ORM\GeneratedValue(strategy="AUTO")
You have some problems in the way you've created you Entities. You should use auto generate strategy. Also the "#ORM\Id" annotation is the unique identifier.
Also, your "JoinColumn" is incorrect. You need to refer back to the "Catalog" Entity, and it's id (identifier). There is no need for 2 "#ORM\Id" entries in the class CatalogLevel.
So make these changes:
/**
* #ORM\Table(name="catalog")
* #ORM\Entity(repositoryClass="AppBundle\Entity\CatalogRepository")
*/
class Catalog
{
/**
* #ORM\Column(name="cat_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $cat_id;
/**
* #ORM\OneToMany(targetEntity="CatalogLevel", mappedBy="catalog", cascade={"persist", "remove"})
* #Assert\Valid
*/
private $levels;
...
/**
* #ORM\Table(name="catalog_level")
* #ORM\Entity(repositoryClass="AppBundle\Entity\CatalogLevelRepository")
*/
class CatalogLevel
{
/**
* #ORM\Column(name="cat_level_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $cat_level_id;
/**
* #ORM\ManyToOne(targetEntity="Catalog", inversedBy="levels")
* #ORM\JoinColumn(name="local_cat_id", referencedColumnName="cat_id")
*/
private $catalog;
...

How can i use multiples values for a#Assert\EqualTo Validation Field Entity

I'm using Symfony\Component\Validator\Constraints as Assert For validate the value of a field.
Problem is I need to accept two o more values for this field of my entity.
/**
* #var integer
* #Assert\EqualTo(
* value=1,
* message="Invalid State",
* groups={"Update"}
* )
* #Column(name="pgsStatus", type="integer")
*/
private $status;
It works perfect, but i tried something like this
/**
* #var integer
* #Assert\EqualTo(
* value={1,11},
* message="Invalid State",
* groups={"Update"}
* )
* #Column(name="pgsStatus", type="integer")
*/
private $status;
or this
/**
* #var integer
* #Assert\EqualTo(
* value=1,
* message="Invalid State",
* groups={"Update"}
* )
* #Assert\EqualTo(
* value=11,
* message="Invalid State",
* groups={"Update"}
* )
* #Column(name="pgsStatus", type="integer")
*/
private $status;
And doesn't work.
I've looked in documentation and I don't find another validation or another way for doing this.
Thanks!!
The Choice Validator is the way to go here:
/**
* #var integer
* #Assert\Choice(
* choices={1,11},
* groups={"Update"}
* )
* #Column(name="pgsStatus", type="integer")
*/
probably use the Choice validator or use the Callback validator

FOSElasticaBundle to filter out entity with certain property

I have setup FOSElasticaBundle that indexes InstagramShopPictures, which has the following property:
class InstagramShopPicture
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #Exclude()
* #ORM\OneToMany(targetEntity="MyApp\MainBundle\Entity\InstagramPictureCategory", mappedBy="picture", cascade={"persist","remove"})
*/
protected $category;
}
How do I specify in elasticsearch (or FOSElasticaBundle) that I want to filter out the results such that only the one that has category of A, where A is a InstagramPictureCategory that the user specifies. Is this possible to filter this out in elastica?

Resources