0
I have two tables, one States and another Customers. When entering the records in the two tables, an error is occurring, instead of entering the state reference in the foreign key of the table Customers, is inserting the new state record into the table States, and in the Customers table, this new state is inserted in place of the foreign key.
What I need is just insert into the table Customers in the field "estado_fk" the ID I got from the State without changing anything in the State table.
The Controller
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$estado = new \Clientes\Entity\Estados();
$estado->setId('2');
$estado->setEstado('Minas Gerais');
$cliente = new \Clientes\Entity\Clientes();
$cliente->setNome('MEU NOME É');
$cliente->setEstadoFk($estado);
$em->persist($estado);
$em->persist($cliente);
$em->flush();
die;
The entity Customers
namespace Clientes\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Clientes
*
* @ORM\Table(name="clientes", indexes={@ORM\Index(name="estado_fk", columns={"estado_fk"})})
* @ORM\Entity
*/
class Clientes
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nome", type="string", length=100, nullable=true)
*/
private $nome;
/**
* @var \Estados
*
* @ORM\ManyToOne(targetEntity="Estados")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="estado_fk", referencedColumnName="id")
* })
*/
private $estadoFk;
function getId() {
return $this->id;
}
function getNome() {
return $this->nome;
}
function getEstadoFk() {
return $this->estadoFk;
}
function setId($id) {
$this->id = $id;
}
function setNome($nome) {
$this->nome = $nome;
}
function setEstadoFk(\Clientes\Entity\Estados $estadoFk) {
$this->estadoFk = $estadoFk;
}
}
The entity States
namespace Clientes\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Estados
*
* @ORM\Table(name="estados")
* @ORM\Entity
*/
class Estados
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="uf", type="string", length=2, nullable=true)
*/
private $uf;
/**
* @var string
*
* @ORM\Column(name="estado", type="string", length=30, nullable=true)
*/
private $estado;
function getId() {
return $this->id;
}
function getUf() {
return $this->uf;
}
function getEstado() {
return $this->estado;
}
function setId($id) {
$this->id = $id;
}
function setUf($uf) {
$this->uf = $uf;
}
function setEstado($estado) {
$this->estado = $estado;
}
}
Probably your bank is assigned the keys, see if there are any restrictions or rules mapped inside the bank.
– Ivan Ferrer
Note that the customer entity is also mapping the state:

/**
 * Clientes
 *
 * @ORM\Table(name="clientes", indexes={@ORM\Index(name="estado_fk", columns={"estado_fk"})})
 * @ORM\Entity
 */
. I believe that when you enter the two records into the database, the mapped Doctrine checks the state’s foreign key and passes to the user you are registering on$cliente->setEstadoFk($estado);
with user registration.– Ivan Ferrer
All right. Ivan, how do I check for keys? I created my table with the following code.
– Danilo Miguel
create table clients(id int Primary key auto_increment, name varchar(100), state_fk int);
– Danilo Miguel
create table states(id int Primary key auto_increment, Uf char(2), state varchar(30) );
– Danilo Miguel
alter table clients add Foreign key (estado_fk) REFERENCES states(id);
– Danilo Miguel
But what is the problem you want to fix? If you do not want to enter the status, boot the reference: this reference:
alter table clientes add foreign key (estado_fk) REFERENCES estados(id);
– Ivan Ferrer
alter table clientes drop constraint RegraDoEstado
– Ivan Ferrer
to check the constraints of your bank:
SELECT DISTINCT(constraint_name) 
FROM information_schema.table_constraints 
WHERE constraint_schema = 'seubanco' 
ORDER BY constraint_name ASC
;– Ivan Ferrer
The id of the state I have "selected", I want to save in the Clients table (estado_fk) without changing anything in the Status table. Only he’s not "setting" the id I picked up, he’s bringing me a new id (like it’s an auto increment)
– Danilo Miguel
Ahhh, now I understand the problem, you better edit your question because it was not clear. Because you said "inserted a new record in the State table".
– Ivan Ferrer
That’s the problem kkkk but I’ll better formulate the title
– Danilo Miguel
Just a detail, why are you passing the name of the state, if you just want to get the reference?
– Ivan Ferrer
Actually, Ivan, I’m doing a lot of tests because I’m racking my brain with this.
– Danilo Miguel
because you don’t just pass the id 2 inside the setState
– Ivan Ferrer
It is that it only accepts being an instance of States
– Danilo Miguel
You’re telling me that in setEstadoFk, he needs to receive the entity?
– Ivan Ferrer
In your controller, you can put the path of namespaces used?
– Ivan Ferrer