How to rename column name in database using Zend Framework 2 doctrine? - doctrine-orm

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"

Related

Doctrine2 - Annotations: eager load (join) vs. nullable column (ManyToOne)

I have this:
/**
* #ManyToOne(targetEntity="TblCity",fetch="EAGER",cascade={"persist"})
* #JoinColumn(name="tblCity",referencedColumnName="Id")
*/
and it creates the correct JOIN for table tblCity in sql and TblCity entity is plugged in my parent entity - aka "Eager-Load"
Pseudo result:
PersonEntity: {
Id: 1
...
CityEntity: {
Id: 1
...
}
}
but, this column NEEDS to be nullable
(if it runs into a "missing" foreign id it complains about missing proxy files for TblCity).
So it has to look like this:
/**
* #Column(nullable=true)
* #ManyToOne(targetEntity="TblCity",fetch="EAGER",cascade={"persist"})
* #JoinColumn(name="tblCity",referencedColumnName="Id")
*/
and poff there goes the "Eager-Load"
The generated sql is missing the JOIN of table tblCity and the column contains only the id and not the entity for TblCity
Pseudo result:
PersonEntity: {
Id: 1
...
CityEntity: 1 (as integer)
}
What am I doing wrong?
PS: I CAN'T use createQuery or such things, so please no solutions involving that
The doctrine #JoinColumn annotation has an optiobal attribute nullable which defaults to true. Read more on this here in the documentation: 21.2.15. #JoinColumn
So the proper way to declare nullable for the join column is:
#JoinColumn(name="tblCity",referencedColumnName="Id", nullable=true)
But nullable is true by default so you don't really need it...
My guess would be that your #Column annotation is overruling the whole #ManyToOne annotation in your case. That is why you get only an id and no TblCity entity.

Doctrine2 migrations rename index for foreign keys

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",

Meaning of "id" in yesod persistent field attributes

What is the meaning of "id=name" in the definition of the User entity?
Any resources about the persist field attributes?
User
name Text Asc id=name
The id=... attribute sets the column name for the tables primary key.
For Example:
Product id=product_id
name Text
quantityInStock Int
... will create a table with an integer primary key called "product_id"

Doctrine 2 query missing part of inner join

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".

How to get names of the table attributes?

I am working Sql Server 2005
My table name is Customer, the attributes are
Orderid
Customerid
Customeraddress
Wherein Customerid is the primary key and Orderid is the foreign key.
I want to know the
primary key name,
foreign key name and
the table name where the foreign key exists as a primary key
Is there any query to retrieve those names using a table name?
Kindly please provide me the queries...Thanks in advance
--list all columns in BLAH table
select c.name
from sys.columns c, sys.tables t
where c.object_id=t.object_id
and t.name='BLAH'