Symfony2 Add Column Doctrine for a query - php

I have add a column on my DATABASE and i would use this field for a query but i receive this error
[Semantical Error] line 0, col 7 near 'pr_refer FROM': Error:
'pr_refer' is not defined.
How can i solved this problem? What should I write in the command line?
**
* #ORM\ManyToOne(targetEntity="Dt\EcBundle\Entity\User")
* #ORM\JoinColumn(name="pr_refer", referencedColumnName="id")
* */
private $pr_refer;
And set e get method
/**
* Set pr_refer
*
* #param \Dt\EcBundle\Entity\User $PrRefer
* #return UserReferences
*/
public function setPrRefer(\Dt\EcBundle\Entity\User $PrRefer = null) {
$this->pr_refer = $PrRefer;
return $this;
}
/**
* Get pr_refer
*
* #return \Dt\EcBundle\Entity\User
*/
public function getPrRefer() {
return $this->pr_refer;
}
Where is the problem??

Just make a php app/console doctrine:schema:update --force for adding the new field to the database.

Related

Add extra option to LoggableListener

I use Loggable to backup changes in Entities.
The default AbstractLogEntry does not have enough columns for my needs.
Thats why i extended the class and added extra getters and setters.
See the code below
/**
* EmployeeBackup
*
* #ORM\Table(name="employee_backup")
* #ORM\Entity(repositoryClass="Gedmo\Loggable\Entity\Repository\LogEntryRepository")
*
*/
class EmployeeBackup extends AbstractLogEntry
{
/**
* #var int
*
* #ORM\Column(name="division_id", type="integer", unique=true)
*/
private $divisionId;
/**
* #return int
*/
public function getDivisionId(): int
{
return $this->divisionId;
}
/**
* #param string $divisionId
*/
public function setDivisionId(string $divisionId): void
{
$this->divisionId = $divisionId;
}
}
The extension is using the class above. So it works.
But now i need to set the divisionId when a new version is stored.
I tried the code below
$loggable = new LoggableListener();
$loggable->setDivision($division);
$evm->addEventSubscriber($loggable);
And this is what i get:
Attempted to call an undefined method named "setDivision" of class "Gedmo\Loggable\LoggableListener".
And thats true because LoggableListener does not have a setDivision function. My question is: Do i need to override the listener and if so, how do i do that?
Thanks ;)

Failed to set up a ManyToMany

I want to be able to select a school (that has its own entity) while creating a mission (also has its entity)
Since a school can have several missions, and you can select several schools at the mission's creation, I used a ManyToMany.
The problem is that after creating this "ManyToMany", generating the entities and updating my schema, Symfony created a table, but left it totally empty, without the two columns that I asked for. I'm not really used to Symfony nor to the ManyToMany system, so I might have done some mistake without noticing it, still I find this weird.
Here's the interesting part of my ecole (school) entity:
class Ecole{
// ...
/**
* #ORM\ManyToMany(targetEntity="MissionBundle\Entity\Mission", mappedBy="ecolesDispo")
*/
protected $missionsDispos;
// ...
/**
* Add missionsDispo
*
* #param \MissionBundle\Entity\Mission $missionsDispo
*
* #return Ecole
*/
public function addMissionsDispo(\MissionBundle\Entity\Mission $missionsDispo)
{
$this->missionsDispos[] = $missionsDispo;
return $this;
}
/**
* Remove missionsDispo
*
* #param \MissionBundle\Entity\Mission $missionsDispo
*/
public function removeMissionsDispo(\MissionBundle\Entity\Mission $missionsDispo)
{
$this->missionsDispos->removeElement($missionsDispo);
}
/**
* Get missionsDispos
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMissionsDispos()
{
return $this->missionsDispos;
}
And here is the interesting part of my mission entity:
/**
* #ORM\ManyToMany(targetEntity="EcoleBundle\Entity\Ecole", inversedBy="missionsDispo")
* #ORM\JoinTable(name="Mission2Ecole",
* joinColumns={#ORM\JoinColumn(name="em_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="me_id", referencedColumnName="id")}
* )
*/
protected $ecolesDispo;
// ...
/**
* Constructor
*/
public function __construct()
{
$this->ecolesDispo = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add ecolesDispo
*
* #param \EcoleBundle\Entity\Ecole $ecolesDispo
*
* #return Mission
*/
public function addEcolesDispo(\EcoleBundle\Entity\Ecole $ecolesDispo)
{
$this->ecolesDispo[] = $ecolesDispo;
return $this;
}
/**
* Remove ecolesDispo
*
* #param \EcoleBundle\Entity\Ecole $ecolesDispo
*/
public function removeEcolesDispo(\EcoleBundle\Entity\Ecole $ecolesDispo)
{
$this->ecolesDispo->removeElement($ecolesDispo);
}
/**
* Get ecolesDispo
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getEcolesDispo()
{
return $this->ecolesDispo;
}
After all this was created, I was supposed to get a multi selector with the list of all the schools saved in the database (I already added it to the missionType file), but I get absolutely nothing.
I don't really know if I inverted the annotations, or if the "joinTable" part is correct, but I'm completely lost here.
Does anyone have an idea?
Thank you in advance
Just wrong typo "s"? inversedBy="missionsDispo" >>> inversedBy="missionsDispos"
PS. Official doc here
http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional

Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Semantical Error] line?

I am trying to create a search field for the rejected transactions page in our web application project, I'm using symfony2 framework, I'm stuck coz there was an error saying,
'[Semantical Error] line 0, col 235 near 'b JOIN b.ediAk403ErrorCodes': Error: Class Matrix\MatrixEdiBundle\Entity\EdiTransaction has no association named edi997Details'
and also
'CRITICAL - Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Semantical Error] line 0, col 235 near 'b JOIN b.ediAk403ErrorCodes': Error: Class Matrix\MatrixEdiBundle\Entity\EdiTransaction has no association named edi997Details" at /tmxpage/apache/htdocsEDI/Editracker/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php line 63 '
here is my code for that (in my repository) :
public function getDetails($gsNumber, $senderId, $receiverId, $page = 1, $limit = 5 ){
$em = $this->getEntityManager();
$query = $em->createQuery(
'SELECT partial a.{ediTransactionId, senderId, receiverId, gsNumber, isaNumber, fileName },
partial b.{errorCodeId, noOfTrans},
partial c.{errorCode, condition}
FROM MatrixEdiBundle:EdiTransaction a
JOIN a.edi997Details b
JOIN b.ediAk403ErrorCodes c
WHERE b.errorCodeId != 1
AND a.flag = 1
AND a.gsNumber LIKE :gsNumber
AND a.senderId LIKE :senderId
AND a.recieverId LIKE :receiverId')
->setParameter('gsNumber', "%gsNumber%")
->setParameter('senderId', "%senderId%")
->setParameter('receiverId'ssss, "%receiverId%")
->setFirstResult(($page-1)*$limit)
->setMaxResults($limit);
$paginator = new Paginator($query, $fetchJoinCollection = false );
$paginator->setUseOutputWalkers(false);
return $paginator;
}
and here is my entity code for the talble ediAk403ErrorCodes :
class EdiAk403ErrorCodes
{
/**
* #var string
*
* #ORM\Column(name="condition", type="string", length=50, nullable=false)
*/
private $condition;
/**
* #var integer
*
* #ORM\Column(name="error_code", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $errorCode;
/**
* Set condition
*
* #param string $condition
* #return EdiAk403ErrorCodes
*/
public function setCondition($condition)
{
$this->condition = $condition;
return $this;
}
/**
* Get condition
*
* #return string
*/
public function getCondition()
{
return $this->condition;
}
/**
* Get errorCode
*
* #return integer
*/
public function getErrorCode()
{
return $this->errorCode;
}
/**
* Get edi997Details
*
* #return \Matrix\MatrixEdiBundle\Entity\Edi997Details
*/
public function getEdi997Details()
{
return $this->edi997Details;
}
}
Seems your Entity Class EdiTransaction don't have an explicit doctrine relation with the table EdiAk403ErrorCodes. So try adding the field definition with the correct annotation in the EdiTransaction class as follow:
EdiTransaction
/**
* #ORM\ManyToOne(targetEntity="Matrix\MatrixEdiBundle\Entity\Edi997Details", inversedBy="editTransactions")
* #ORM\JoinColumn(name="edit_details_id", referencedColumnName="id")
*/
private $edi997Details;
Hope this help
The single letters are used as a short name for a table. In your first line, you associate MatrixEdiBundle:EdiTransaction with the letter a. So a.edi997Details only makes sense if it's a column of the a table. I believe all you need is this:
FROM MatrixEdiBundle:EdiTransaction a
JOIN edi997Details b
JOIN ediAk403ErrorCodes c

Doctrine and Symfony2 : Association refers to inverse side that doesn't exist error

I have a page address.html.twig , the user can add many addresses in the table UserAddress. when he added his address in the database , the address should be render in the same page that he added his address then he can choose which one he would like to use. Unfortunately the address is not render.
First i thought that i have a problem in my controller action or in my twig page. I even asked a question here about it => here
I verified all my tables in phpmyadmin and all of them are well link but if i'm doing this: php app/console doctrine:schema:validate
i have this error :
[Mapping] FAIL - The entity-class
'FLY\BookingsBundle\Entity\Commandes' mapping is invalid:
* The association FLY\BookingsBundle\Entity\Commandes#user refers to the inverse side field
Application\Sonata\UserBundle\Entity\User#commandes which does not
exist.
[Mapping] FAIL - The entity-class
'FLY\BookingsBundle\Entity\UserAddress' mapping is invalid:
* The association FLY\BookingsBundle\Entity\UserAddress#user refers to the inverse side field
Application\Sonata\UserBundle\Entity\User#address which does not
exist.
Have a look at this picture:
This is my UserAddress.php
/**
* #ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="address")
* #ORM\JoinColumn(nullable=true)
*/
private $user;
Commandes.php
/**
* #ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="commandes")
* #ORM\JoinColumn(nullable=true)
*/
private $user;
User.php
/**
* #ORM\Entity(repositoryClass="FLY\UserBundle\Repository\UserRepository")
* #ORM\Table(name="fos_user_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
$this->commandes = new \Doctrine\Common\Collections\ArrayCollection();
$this->address = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\Commandes", mappedBy="user", cascade={"remove"})
* #ORM\JoinColumn(nullable=true)
*/
private $commandes;
/**
* #ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\UserAddress", mappedBy="user", cascade={"remove"})
* #ORM\JoinColumn(nullable=true)
*/
private $address;
Here you can see my var dump:
User {#124 ▼
#id: 21
-commandes: null
-address: null
}
I've had an issue which has popped up 2-3 times in the last few years, where the mappings were incorrect but the schema update was successful. After the mappings were fixed this wasn't reflected in the schema and symfony assumed it was already up-to-date.
I recommend you try removing the relevent relationships manually from your user, commande and address tables and then run:
php app/console doctrine:schema:update --force
- it may fix your issue.
Heres an example from one of my apps - I've done this for your commandes entity.
You'll be able to piece together your UserAddress Entity from this example yourself!
Here goes:
User.php
/**
* #ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\Commandes", mappedBy="commandesUser")
*/
protected $commandes;
User.php - Getters and Setters
/**
* Add commandes
*
* #param FLY\BookingsBundle\Entity\Commandes $commandes
*/
public function addCommandes(\FLY\BookingsBundle\Entity\Commandes $commandes)
{
$this->commandes[] = $commandes;
}
/**
* Get commandes
*
* #return Doctrine\Common\Collections\Collection
*/
public function getCommandes()
{
return $this->commandes;
}
Commandes.php
/**
* #ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="commandes")
* #ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $commandesUser;
Commandes.php - Getters and Setters
/**
* Set commandesUser
*
* #param Application\Sonata\UserBundle\Entity\User $commandesUser
*/
public function setCommandesUser(\Application\Sonata\UserBundle\Entity\User $commandesUser = null)
{
$this->commandesUser = $commandesUser;
}
/**
* Get $commandesUser
*
* #return Application\Sonata\UserBundle\Entity\User
*/
public function getCommandesUser()
{
return $this->commandesUser;
}
It's quite likely this doesn't happen to anyone else, but there's a chance.
In my case, this error appeared because there was a duplicate. My entity had 2 fields, which are ManyToOne relationships. And they both had the same inversed names, which gave this error.
So this is the relevant bit of code:
class TaskIngredient
{
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Ingredient", inversedBy="taskIngredients")
* #ORM\JoinColumn(nullable=false)
*/
private $ingredient;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Task", inversedBy="taskIngredients")
* #ORM\JoinColumn(nullable=false)
*/
private $task;
}
The solution was relatively easy. I tried changing the inversedBy name, manually. However this didn't fix it (even after applying php app/console doctrine:schema:update --force and removing the var/cache folder).
So I just:
Removed one of the problematic entities (and it's setter/getter)
Ran the php bin/console make:entity tool and readded the field with a different name
Voilà! Issue fixed.

Can't call entity functions from a returned repository query

I am trying to fetch an object from my database in symfony2 and doctrine using one of my entity classes and a repository class.
Here is the function call:
$game = $em->getRepository('MLBPBeerBundle:TableGame')->findBygameId($gid);
$game.setZoneTableid('20');
Here is a snippet of my repository class:
class TableGameRepository extends EntityRepository
{
public function findBygameId($gid)
{
return $this->getEntityManager()
->createQuery('SELECT g FROM MLBPBeerBundle:TableGame g WHERE g.gameId = ' . $gid)
->getSingleResult();
}
And here is a snippet of my entity class:
/**
* MLBP\BeerBundle\Entity\TableGame
*
* #ORM\Table(name="table_game")
* #ORM\Entity(repositoryClass="MLBP\BeerBundle\Repository\TableGameRepository")
*
*/
class TableGame
{
/**
* #var TableTable
*
* #ORM\ManyToOne(targetEntity="TableTable")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="ZONE_TABLEID", referencedColumnName="TABLE_ID")
* })
*/
protected $zoneTableid;
/**
* Set zoneTableid
*
* #param MLBP\BeerBundle\Entity\TableTable $zoneTableid
*/
public function setZoneTableid(\MLBP\BeerBundle\Entity\TableTable $zoneTableid)
{
$this->zoneTableid = $zoneTableid;
}
And here is my error message:
Call to undefined function MLBP\BeerBundle\Controller\setZoneTableid()
Thanks!
Be sure to use the -> operator instead of the . operator. Also, for this specific example you cannot set '20' as the zoneId as it is not the Entity representing a TableTable.
$game->setZoneTableid(<Some Table Entity>);

Categories