I have a sql error deploying my symfony project on Unix. Indeed, the query made uses uppercase for table names which was not the case before. In the database, table names are lowercases.
Does anybody know where you configure how you want the queries generation be made (uppercase or lowercase)
Thank you.
Maybe something in Doctrine configuration??
NOTE : I have some new information.
I asked to rebuild my database from the save at a time I know it was working.
I have some errors because, for the new code, the database must be a little different than what it was at this time but I can see that in queries the names of the tables are in lower case.
I pass my new sql (the same as before plus some little changes) in command line.
\. path/to/my/sql
I launch the site and queries are made with table names in uppercase.
Got any idea ?
example of entity :
<?php
namespace MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* MyTable
*
* #ORM\Table(name="mytable")
* #ORM\Entity(repositoryClass="MyBundle\Repository\MyClassRepository")
*/
class MyTable
{
/**
* #var string
*
* #ORM\Column(name="FIELD1, type="string", length=120, nullable=false)
*/
private $field1;
/**
* #var string
*
* #ORM\Column(name="FIELD2", type="string", length=50, nullable=true)
*/
private $field2
/**
* Set field1
*
* #param string $field1
*
* #return Tretb
*/
public function setField1($field1)
{
$this->field1 = $field1;
return $this;
}
/**
* Get field1
*
* #return string
*/
public function getField1()
{
return $this->field1;
}
/**
* Set field2
*
* #param string $field2
*
* #return Tretb
*/
public function setField2($field2)
{
$this->field2 = $field2;
return $this;
}
/**
* Get field2
*
* #return string
*/
public function getField2()
{
return $this->field2;
}
}
Dont know if you can configure it more generally, but you can define table table for each entity with doctrine
/**
* AppBundle\Entity\MyEntity
*
* #ORM\Table()
* #ORM\Entity
* #ORM\Table(name="mytable")
*/
class MyEntity
{
Does your MySQL instance support lowercase tablenames ?
To check the settings use:
mysql> show variables like "lower_case%";
To change the setting you need to change the mysql setting
lower_case_table_names = 1 or lower_case_table_names = 2
Source from Stackoverflow
Related
I am using Symfony3 with doctrine and my question is:
Is possible after getQuery in query builder get results with attribute which has formatted date?
I have attr in entity:
/**
* #var \DateTime
*
* #ORM\Column(name="terminFrom", type="date", nullable=true)
*/
private $terminFrom;
My try - set format in getter:
/**
* #return string
*/
public function getTerminFrom()
{
return $this->terminFrom->format('d.m.Y');
}
But still after getQuery i have \DateTime object in this attribute.
I don't understand - attributes in entity are private so getters must be called... or?
I got three classes:
ProjectType
Phase
ProjectTypePhase (This is to create a seperate join table to make sure ProjectType and Phase gets linked with an id for ordering)
ProjectType
/**
* #OneToMany(targetEntity="ProjectTypePhase", mappedBy="project_type")
*/
private $projectTypePhases;
public function __construct()
{
$this->projectTypePhases = new ArrayCollection();
}
/**
* #return mixed
*/
public function getProjectTypePhases()
{
return $this->projectTypePhases;
}
Phase
/**
* #OneToMany(targetEntity="ProjectTypePhase", mappedBy="phase")
*/
private $projectTypePhases;
public function __construct()
{
$this->projectTypePhases = new ArrayCollection();
}
/**
* #return mixed
*/
public function getProjectTypePhases()
{
return $this->projectTypePhases;
}
ProjectTypePhase
/**
* #ManyToOne(targetEntity="ProjectType", inversedBy="project_type_phase")
* #JoinColumn(name="project_type_id", referencedColumnName="id")
*/
private $projectType;
/**
* #ManyToOne(targetEntity="Phase", inversedBy="project_type_phase")
* #JoinColumn(name="phase_id", referencedColumnName="id")
*/
private $phase;
public function __construct($projectType, $phase)
{
$this->projectType = $projectType;
$this->phase = $phase;
}
I filled the database through MySQL workbench since they are only id entries (correct?). Anyway, whenever I try to do $projectType->getProjectTypePhases();` it returns an empty collection. Also when I try this:
$repository = $this->getDoctrine()->getRepository('AppBundle:ProjectTypePhase');
$projectPhases = $repository->findAll();
I get all the entries, but somehow the variable name for instance of the projecttype and phase entity is null even though they are filled in the database. The corresponding keys are correct and names are filled. What goes wrong? And is there some approach that needs to be done what I am missing? The thing I am trying to accomplish is:
I had a Many to Many relationship which worked fine between ProjectType and Phase. However since there is no id for that table things didn't get in the order I intended to. I had searched for a solution and that was a seperate entity which could handle the relationship between ProjectType and Phase and add additional columns. Is this the correct way?
Modify ProjectTypePhase as follows
/**
* #ManyToOne(targetEntity="ProjectType", inversedBy="projectTypePhases")
* #JoinColumn(name="project_type_id", referencedColumnName="id")
*/
private $project_type;
/**
* #ManyToOne(targetEntity="Phase", inversedBy="projectTypePhases")
* #JoinColumn(name="phase_id", referencedColumnName="id")
*/
private $phase;
You must make names match in order to make the things works. However it's pretty strange that doctrine does not warn you
I am using an existing MSSQL-database (Dynamics) and to fetch data out of it I've cloned the database layout in a Doctrine entity so I can fetch the data out of the database. This works well for a sole entity, but when I have OneToMany relationships, something goes wrong. It's returning the correct amount of children, but they're all the same.
When I copy paste the runnable query into SQL Server I get the correct results (with different children). So the SQL query is good.
So it seems like Doctrine doesn't hydrate the result correctly. I am using the pdo_sqlsrv driver and Doctrine2 with Symfony 2.8.
Dynamics table layout:
PurchTable
recid = unique integer
purchid = unique string
PurchLine
recid = unique integer
purchid = referring to purchid from PurchTable
itemid = string
etc...
PurchTable Entity:
/**
* #var integer
*
* #ORM\Column(name="recid", type="integer")
*/
private $recid;
/**
* #var string
*
* #ORM\Column(name="purchid", type="string", length=255)
* #ORM\Id
*/
private $orderid;
/**
* #ORM\OneToMany(targetEntity="PurchLine", mappedBy="order")
*/
protected $lines;
public function __construct()
{
$this->lines = new ArrayCollection();
}
PurchLine:
/**
* #var integer
*
* #ORM\Column(name="recid", type="integer")
* #ORM\Id
*/
private $recid;
/**
* #var integer
*
* #ORM\Column(name="linenum", type="integer")
*/
private $linenum;
/**
* #var string
*
* #ORM\Column(name="itemid", type="string", length=255)
*/
private $itemid;
/**
* #ORM\ManyToOne(targetEntity="PurchTable", inversedBy="lines")
* #ORM\JoinColumn(name="purchid", referencedColumnName="purchid")
*/
protected $order;
As you can see I changed the property name to orderid in PurchTable to avoid using the same name, but that didn't help.
So what am I doing wrong? :)
I cannot change the driver at a short time so I am wondering if this is a driver issue or not (to change the priority of changing the driver). After reading this:
http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/configuration.html#driver
A Microsoft SQL Server driver that uses pdo_sqlsrv PDO Note that this driver caused problems in our tests. Prefer the sqlsrv driver if possible.
I am wondering if the pdo_sqlsrv driver can cause these issues or not.
edit
I tested the sqlsrv driver (be sure to use an empty string as username instead of null (for pdo_sqlsrv) to use Windows Authentication) and I encounter the same problem. So it must be something in Doctrine or in the relationship.
edit 2
I recreated the entities to avoid any spelling mistakes but to no avail.
The problem lies in the wrong definition of the columns. The RecId column in the MSSQL database was a bigint instead of integer.
So, changing
/**
* #var integer
*
* #ORM\Column(name="recid", type="integer")
* #ORM\Id
*/
private $recid;
to
/**
* #var bigint
*
* #ORM\Column(name="recid", type="bigint")
* #ORM\Id
*/
private $recid;
is the answer.
I am making the lesson administration system on symfony2 and doctrine
I am confused to use foreign key in doctrine.
/Entity/User.php
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*#ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\Lesson", inversedBy("teacher"))
*/
protected $id;
.
.
}
/Entity/Lesson.php
class Lesson
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy("id"))
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $teacher;
.
.
}
Each 'Lesson' has one teacher registered in User.php.
How can I write annotation for this purpose?
I am also planning that each Lesson has multiple students from /Entity/User.
How can I write annotation for this purpose? (ManyToMany?)
I have researched ,but I couldn't find good documents for doctrine annotation.
Here some cheat sheets for doctrine annotations : link
For your problem, you need to define your variables in each side of your associations.
In Lesson.php :
/**
* #ORM\OneToOne(
* targetEntity="Acme\UserBundle\Entity\User",
* inversedBy="lessons*removethis : name of the variable in user.php*"
* )
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $teacher;
In User.php :
/**
* #ORM\OneToOne(
* targetEntity="Acme\UserBundle\Entity\Lesson",
* mappedBy="teacher*removethis : name of the variable in lesson.php*"
* )
*/
private $lessons;
And yes, ManyToMany is good for the purpose your are looking for :)
Doctrine has got some nice documentation, but at some point I have the feeling for someone who wants to get in to doctrine it is sort of a small battle to get used to the mapping stuff. I am one of those guys who belongs to this section. I have gone through most of the mapping stuff documentation for example like this and other links in doctrine official site, but the documentation for me looks like bits and pieces to follow. I am saying this for my case.
Is there somewhere an example which shows how can I join two tables with a third join table, I wanted to know the basic mapping for this schema.
Let me say I have two tables: Fruits and Country.
The relationship is that one country produces many varieties of fruits, so as to say that is a onetomany and manytoone relationship. Apart from that I wanted to do association using a third table say countryFruits.
Fruits Table
-- fruitsId (PK, AI)
-- fruitName
Country Table
-- countryId (PK, AI)
-- countryName
countryFruits Table
-- fruitsId (PK, FK)
-- countryId (PK, FK)
That is how the tables in MySQL look like and it has already been designed. Now I can fill the fruits table with doctrine and when it comes to filling the country table, I get a messed up mapping problem.
/**
* #ORM\Entity
* #ORM\Table(name="fruits")
* #property string $fruitName
* #property int $fruitId
*/
class Fruits
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="fruitId", unique=true);
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $fruitId;
/**
* #ORM\Column(type="string")
*/
protected $fruitName;
/**
* #ORM\OneToMany(targetEntity="Country", mappedBy="fruits", cascade={"persist"})
*/
protected $country;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
/**
* #ORM\Entity
* #ORM\Table(name="country")
* #property string $countryName
* #property int $countryId
*/
class Country
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="countryId", unique=true);
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $countryId;
/**
* #ORM\Column(type="string")
*/
protected $countryName;
/**
* #ORM\OneToMany(targetEntity="Fruits", mappedBy="country", cascade={"persist"})
*/
protected $countries;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
/**
* #ORM\Entity
* #ORM\Table(name="countryFruits ")
* #property int $fruitId
* #property int $countryId
*/
class countryFruits
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="fruitId", nullable=false)
* #ORM\GeneratedValue(strategy="NONE")
*/
protected $fruitId;
/**
* #ORM\Id
* #ORM\Column(type="integer", name="countryId", nullable=false)
* #ORM\GeneratedValue(strategy="NONE")
*/
protected $countryId;
/**
* #ORM\ManyToOne(targetEntity="Country", inversedBy="fruits", cascade={"persist"})
* #ORM\JoinColumn(name="countryId", referencedColumnName="countryId")
*/
protected $country;
/**
* #ORM\ManyToOne(targetEntity="Fruits", inversedBy="country", cascade={"persist"})
* #ORM\JoinColumn(name="fruitId", referencedColumnName="fruitId")
*/
protected $fruits;
/**
* Set fruits
*
* #param Fruits $fruits
*/
public function setFruits($fruits)
{
$this->fruits = $fruits;
}
/**
* ´Get fruits
*
* #param Fruits $fruits
*/
public function getFruits()
{
return $this->fruits;
}
/**
* Set country
*
* #param Country $country
*/
public function setCountry($country)
{
$this->country = $country;
}
/**
* Get country
*
* #param Country $country
*/
public function getCountry($country)
{
$this->country = $country;
}
}
Can somebody cross check this and let me know if my mappings are done in the proper way. In case, is it possible to get a small piece of code how to persist the entities into my database.
you don't need the countryFruits class. what you're searching for is the ManyToMany relation! also you don't want to name entities in plural, as an entity always represends a single object/row in the table.
Fruit entity
/**
* #ORM\Entity
* #ORM\Table(name="fruits")
* #property string $fruitName
* #property int $fruitId
*/
class Fruit
{
/**
* #ORM\ManyToMany(targetEntity="Country")
* #ORM\JoinTable(name="country_fruits",
* joinColumns={#ORM\JoinColumn(name="country_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="fruit_id", referencedColumnName="id")}
* )
* #var Country[]
*/
protected $countries;
}
Country entity
/**
* #ORM\Entity
* #ORM\Table(name="country")
* #property string $countryName
* #property int $countryId
*/
class Country
{
/**
* #ORM\ManyToMany(targetEntity="Fruit")
* #ORM\JoinTable(name="country_fruits",
* joinColumns={#ORM\JoinColumn(name="fruit_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="country_id", referencedColumnName="id")}
* )
* #var Fruit[]
*/
protected $fruits;
}
note that you don't need the countryFruits class, but the table country_fruits is required!