Semantical Error Symfony2 - symfony

Hi I have an Entity name: Template
it containing 5-6 field with respective their getter/setter, defined.
But I am getting an semantical error as follows while fething data from this entity:
/**
* #var integer
*
* #ORM\Column(name="message_type", type="integer", nullable=false)
*/
private $messageType;
[Semantical Error] line 0, col 20 near 'message_type,': Error: Class MyuBundle\Entity\MyTemplate. has no field or association named message_type.
even the field "message_type" already defined in my entity and table in db.
any suggestion what could be the issue.
Thanks

we need to see the formType thats throwing the error to be 100%.
but looking at the snippet, you don't actually have a property message_type on that class. (the column name itself isn't used in mapping).
The likely error is caused by the fact that you've mapped a form field against a fieldname of message_type when it should be messageType.
provided you have a getter and setter for it you should be fine.
public function getMessageType()
public function setMessageType()
even the field "message_type" already defined in my entity and table in db.
this is not actually the case.

Related

Doctrine 2 - ManyToOne Nullable relation not work

I have a strange error, on my Symfony4 project.
One of my relation is a ManyToOne with nullable true, like this:
class UserComic
{
...
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Series")
* #ORM\JoinColumn(name="id_series", referencedColumnName="id", nullable=true)
*/
private $series;
...
}
then I try to create a new UserComic with the $series attribute set to NULL, but i receive this error:
"Entity of type App\Entity\UserComic is missing an assigned ID for
field 'series'. The identifier generation strategy for this entity
requires the ID field to be populated before EntityManager#persist()
is called. If you want automatically generated identifiers instead you
need to adjust the metadata mapping accordingly."
As the field is not setted to nullable. Any advice?
ID means UNIQ and NOT NULLABLE.
You are on a ManyToOne relatsionship (on the many side) that break the UNIQ property of ID
You set the joinColumn as nullable, that breaks the NOT NULLABLE PROPERTY

How to pass a getter or a class attribute to symfony custom constraint?

I have a custom constraint to validate if given coordinates are inside a given region, but i'm not able to pass this regionId to the constraint cause it's not a constant, it's a class variable.
I've tried this
/**
* #var array
* #CoordinatesInsideRegion(regionId= getRegionId())
*/
protected $coordinates;
But it's returning this
{"code":500,"message":"[Semantical Error] Couldn't find constant getRegionId, property CustomerBundle\DTO\Address\Request\CreateAddressRequestDTO::$coordinates."}
I've also tried regionId, this.regionId, this.getRegionId()
And none worked

Symfony2 misconfigured entity

I am getting two warnings about misconfigured entities in my Symfony 2 project. It runs fine in the development environment, but the production environment will not start and I suspect these misconfigured entities might be the reason.
It is the same error on both entities so I am only including one of them as an example.
BizTV\MediaManagementBundle\Entity\QrImage:
The field BizTV\MediaManagementBundle\Entity\QrImage#visits is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity BizTV\MediaManagementBundle\Entity\QrVisit#QrImage does not contain the required 'inversedBy="visits"' attribute.
QrVisit entity:
class QrVisit
{
...
/**
* #var object BizTV\MediaManagementBundle\Entity\QrImage
*
* #ORM\ManyToOne(targetEntity="BizTV\MediaManagementBundle\Entity\QrImage")
* #ORM\JoinColumn(name="QrImage", referencedColumnName="id")
*/
protected $QrImage;
QrImage entity:
class QrImage
{
...
/**
* #ORM\OneToMany(targetEntity="BizTV\MediaManagementBundle\Entity\QrVisit", mappedBy="QrImage")
*/
private $visits;
I changed QrImage to include the inversedBy as below, but I probably did it wrong because I still get an error message, although a new one.
/**
* #ORM\OneToMany(targetEntity="BizTV\MediaManagementBundle\Entity\QrVisit", mappedBy="QrImage", inversedBy="visits")
*/
private $visits;
But this generates the error:
[Creation Error] The annotation #ORM\JoinColumn declared on property BizTV\UserBundle\Entity\UserGroup::$company does not have a property named "inversedBy". Available properties: name, referencedColumnName, unique, nullable, onDelete, columnDefinition, fieldName
If you want to establish a bi-directional ManyToOne / OneToMany relationship you'll have to put the mappedBy attribute on the OneToMany side like:
#ORM\OneToMany(targetEntity="BizTV\MediaManagementBundle\Entity\QrVisit", mappedBy="QrImage")
and the inversedBy on the ManyToOne side like:
#ORM\ManyToOne(targetEntity="BizTV\MediaManagementBundle\Entity\QrImage", inversedBy="visits")
that's all you need here. For your reference please check Doctrine doc
The error that you're getting refers to a different entity (UserGroup) but you can check them in the same fashion.

Symfony2 & FOSRestBundle: Getting UUID packed in a BINARY(16) field from MySQL

I am facing a weird problem relating to UUIDs.
I have developed a REST API using Symfony2+FOSRestBundle+JMSSerializer. As I need to update some tables from two sources I thought of using UUID as primary key for one entity.
I did a doctrine:mapping:import to generate entities in my Symfony project. Everything correct. I ended up with the following entity (only exposing the key field and generated getter for simplicity):
<?php
namespace Stardigita\TgaAPIBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TgaBookings
*
* #ORM\Table(name="tga_bookings", indexes={[...]})
* #ORM\Entity
*/
class TgaBookings
{
/**
* #var string
*
* #ORM\Column(name="book_cd_booking_UUID", type="blob", length=16, nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $bookCdBookingUuid;
/**
* Get bookCdBookingUuid
*
* #return string
*/
public function getBookCdBookingUuid()
{
return $this->bookCdBookingUuid;
}
...
No setter was generated. I can still do it myself and I will, as I will need to know the key beforehand.
The data for this field is correctly stored in the table as a BINARY(16). When I recover the data calling the REST GET method, I get this:
[
{
"book_cd_booking_uuid": "Resource id #1244",
"book_cd_booking": 8,
....
My question is: how can I get the actual data from the field?
I suppose something has to be done in the field getter, but I tried some solutions without any success.
Thanks.
UPDATE:
I've managed to get the actual data logged, modifying the getBookCdBookingUuid method this way:
/**
* Get bookCdBookingUuid
*
* #return string
*/
public function getBookCdBookingUuid()
{
return bin2hex($this->bookCdBookingUuid);
}
and changed the type to "guid" in the property annotation:
/**
* #var string
*
* #ORM\Column(name="book_cd_booking_UUID", type="guid", length=16, nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $bookCdBookingUuid;
I have represented the hex UUID correctly in the log before returning the results in the controller:
[2014-11-03 19:52:07] app.ERROR: 1046684e5f6711e4a09f00089bce936a [] []
But still getting an exception relating UTF invalid characters:
request.CRITICAL: Uncaught PHP Exception RuntimeException: "Your data could not be encoded because it contains invalid UTF8 characters." at /var/www/tga_api/vendor/jms/serializer/src/JMS/Serializer/JsonSerializationVisitor.php line 36 {"exception":"[object] (RuntimeException: Your data could not be encoded because it contains invalid UTF8 characters. at /var/www/tga_api/vendor/jms/serializer/src/JMS/Serializer/JsonSerializationVisitor.php:36)"} []
Also I got no response from the service. A 500 error is returned.
Please, I need to solve this issue. Any ideas are welcome.
Thanks.
GeneratedValue
I notice you're using the annotation #ORM\GeneratedValue(strategy="IDENTITY") for the UUID property. IDENTITY means the database should/will use auto-increments, which shouldn't be done when using UUIDs. Please change it to #ORM\GeneratedValue(strategy="NONE") or remove it completely.
Conversion
The string form of a UUID (like 01234567-89ab-cdef-0123-456789abcdef) should be converted to binary when it's persisted in the database, and converted back when fetched from the database.
The easiest way to do this is to introduce a custom type. See here for an example.
Bugs
Doctrine (even master/2.5) has some issues with using UUIDs in associations. I'm attempting to fix these issues in PR #1178.
If you need UUIDs in associations and can't wait till it's fixed, then use regular integer ids and have the UUID is a separate column.

Getting multiple rows using ParamConverter

Hi Im trying to use ParamConverter to get multiple rows from DB but profiler show query with limi 1. Is it possible to get it like that
/**
* #Route("/localization/{code}", name="pkt-index")
* #ParamConverter("localizations", class="PriceBundle:Localization")
*/
after entering localization/0003 I should get more than 100 rows.
EDIT:
I have used repository_method option
and
/*
* #Route("/localization/{id}", name="pkt-index")
* #ParamConverter("localizations", class="PriceBundle:Localization", options={
* "repository_method": "getByLocalizationCode"
* })
*/
but funny thing is that when I change {id} in route it does not work it throws and exception
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
even if variable exists in entity class, if variable dont exist it throws
Unable to guess how to get a Doctrine instance from the request information.
EXPLANATION
when I change {id} in route it does not work it throws and exception
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
Here I think symfony treads id like primary key and as parameter to repository method it pass string when I changed this id to something else it pass array
Example
/**
* #Route("/localization/{id}", name="pkt-index")
*/
pass string to method
/**
* #Route("/localization/{code}/{name}", name="pkt-index")
*/
pass array to method
array(
'code' => 003
'name' => localization_name
)
and last
/**
* #Route("/localization/{id}/{name}", name="pkt-index")
*/
will pass string id omit the name
Hope this sounds reasonable.
forgottenbas's answer isn't completely right. #ParamConverter will first try to find one entity by id ...
... then try to match the route variables against db columns to find an entity ...
but essentially it will only convert one entity at a time.
If you would still like to use a paramconverter you would need to write a custom one.
or just use a one-liner inside your controller action:
/**
* #Route("/localization/{code}", name="pkt-index")
*/
public function yourAction($code)
{
$localizations = $this->getDoctrine()->getRepository("YourBundle:Localization")->findBy(array("code" => $code));
// ...
ParamConverter currently can only extract id from request and find one entity from db. Look at
DoctrineParamConverter code. But you can specify your own param converter with some extra logic.

Resources