I'm using Doctrine 2 to try and do a query with an inner join. I have a Site entity and a Page entity. Each Site can have many pages and each page can only belong to one site. I have a site_id foreign key in my pages table rows. In my Site entity, I've created a OneToMany assocatiion where the target entity is my Page entity and the mappedBy is set to Site.
/**
* #var \Doctrine\Common\Collections\ArrayCollection
* #OneToMany(targetEntity="Page", mappedBy="Site", cascade={"persist", "remove"})
*/
private $pages;
In my Page entity, I have a ManyToOne association where the target entity is set to Site.
/**
* #var App\Entity\Site
* #ManyToOne(targetEntity="Site")
*/
private $site;
Here is my query builder statement where I'm passing a specific Site id:
$qb = $this->_em->createQueryBuilder();
$qb->select('s')
->from('App\Entity\Site', 's')
->innerJoin('s.pages', 'p')
->where('s.id = :id')
->setParameter('id', $id);
and here is the actual SQL I get back:
SELECT s0_.id AS id0, s0_.domain AS domain1 FROM sites s0_ INNER JOIN WHERE s0_.id = ?
See how the INNER JOIN information is completely missing? Am I doing something wrong here or is this a problem with Doctrine 2?
You need to explicitly tell Doctrine to fetch those columns:
$qb->select('s', 'p')
->form(...)
->join(...)
Figured it out. My mappedBy statement was incorrect in Site entity. I was pointing at "Site" when it should have been "site".
Related
Is it possible to make #ORM\OneToOne join where parent is null?
Currently im using #ORM\OneToMany to get all rows and filtering to find only one parnent (where parent is NULL) entity
Yes it is possible with the nullable=true annotation :
<?php
/**
* #ORM\JoinColumn(name="commentary_id", nullable=true)
* #ORM\OneToOne(targetEntity="App\Entity\Commentary")
*/
private $commentary;
All the documentation is here : https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/annotations-reference.html#joincolumn
Trying to build a query with doctrine, getting error:
Entity has no field or association named some_id
code (t is test table, it has some_id and i join it with Example ON example.id = test.some_id):
->from('AppBundle:Test', 't')
->leftJoin('AppBundle:Example', 'e', 'WITH', 'e.id = t.some_id')
Test Entity:
/**
* #var integer
*
* #ORM\Column(name="some_id", type="integer", nullable=true)
*/
private $someId;
Column some_id exists in my database, and a simple SQL query works fine, im just getting this error when trying to build it with query manager. Im guessing im missing something in my entity, but not sure what exactly.
Found the answer..
it has to be:
->leftJoin('AppBundle:Example', 'e', 'WITH', 'e.id = t.someId')
someId instead of some_id
I have a table named Products in which there is a column id. It has foreign key relationship with prtyID column in ProductTypes Table. I just want to change the column id of Products table to prtyID. How is it possible? Is there any doctrine command is available for that? Please help me to fix this..
I'm not sure what you are trying to do here... id should be the primary id of Products, not a foreign key for another table. Please add your code if you ask a question.
Usually, Doctrine will use the property name to create the table column. So changing the property name to $prtyId would change your table column as well.
You can give every column the attribute "name" in the annotation of your entity, which will change the column name in your database:
/**
* #ORM\Column(type="integer", name="prtyID")
**/
protected $id;
Or, because you don't define the foreign keys yourself in Doctrine but the association, you can define the association like this:
/**
* #ORM\ManyToOne(targetEntity="ProductTypes")
* #ORM\JoinColumn(name="prtyID", referencedColumnName="id")
*/
protected $type;
This will create the column "prtyID" in your products table which is defined as the foreign key to productTypes primary colum "id"
I have problem with renaming indexes in doctrine:migrations:diff. Everytime when i ran this command, doctrine created sql queries to rename existing index in database. Is there any way to prevent this behaviour?
Full description:
we have big(about 2 hundres tables) old database on Oracle. There are naming conventions for constraint and indexes, for example:
we have table RANDOM_KEY and USER, foreign key (many to one) from RANDOM_KEY to USER, so coinstraint has name FK_RANDOM_KEY_USER and relevant index is FK_RANDOM_KEY_USER_I.
Entity declaration:
/**
* #ORM\Table(name="RANDOM_KEY")
* #ORM\Entity
*/
class RandomKey {
...
Relations declaration:
/**
* #var \User
*
* #ORM\ManyToOne(targetEntity="\User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="USER_ID", referencedColumnName="USER_ID")
* })
*/
private $user;
Result of migrations:diff commad is:
$this->addSql("ALTER INDEX FK_RANDOM_KEY_USER_I RENAME TO IDX_C54F7889A0666B6F");
I looked into depth of doctrine, tried some debugging. Between indexes are no other differences, only name. Also I tried add indexes definition to #ORM/Table. Is there way to prevent index renaming?
Relevant composer data:
"symfony/symfony": "2.4.*",
"doctrine/orm": "2.5.*",
"doctrine/dbal": "2.5.*",
"doctrine/migrations": "1.0.*#dev",
"doctrine/doctrine-bundle": "1.2.*",
"doctrine/doctrine-migrations-bundle": "2.1.*#dev",
on a very simple entity :
class Users {
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
protected $userid;
/** #ORM\Column(type="string") */
protected $username;
}[...]
while trying to do a native query
$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
$rsm->addEntityResult('Application\Entity\Users', 'u');
$rsm->addFieldResult('u', 'test', 'username');
$rsm->addFieldResult('u', 'userid', 'userid');
$q = $objectManager->createNativeQuery('SELECT u.username as test, u.userid from users u where u.userid=17',$rsm);
$result = $result->getResult();
$result returns an empty array with oracle (oci8 driver and pdo).
With MySQL, all is ok. The databases are exactly the same between Oracle and MySQL, same tables, same columns.
The table 'users' is not empty, because when using DQL, it works. Works too when using addRootEntityFromClassMetadata() whith a native query.
It seems that the problem occurs only with oci8+addEntityResult().
Any idea ?
Thanks by advance.
Found it in the manual.
ResultSetMapping#addFieldResult();
The first parameter is the alias of the entity result to which the field result will belong. The second parameter is the name of the column in the SQL result set. Note that this name is case sensitive, i.e. if you use a native query against Oracle it must be all uppercase. The third parameter is the name of the field on the entity result identified by $alias into which the value of the column should be set.
$rsm->addFieldResult('u', 'TEST', 'username');
$rsm->addFieldResult('u', 'USERID', 'userid');