Here is the problem that I cannot solve:

I have two tables, a "devices" table, and a "device type" table. The device 
type table has two values, A or B with a primary key of 1 or 2.
The devices table can be hundreds of records, and has a foreign key to the 
device type table. Each record in the device table will either have an ID 
or 1 or 2 from the device type table,

First, I assume this is a Many to One Relationship, because each device can 
only have one device type, but these can be repeated over and over again in 
the devices table.

The problem is, when I insert data from an array, I get duplicate inserts 
into the device type table, and the program fails. 

For example. I am trying to insert 5 devices, all with a device type of A. 
When I run the below code, it tries to insert another record in device type 
for A, even though it already exists, and I get an Integrity constraint 
violation



foreach ($device_info as $info) {
            
           
            $device = new entityDevice();
            $device_type = new entityDeviceType();
        
            $device_type->setDeviceTypeName($device_type_name);
            $this->_entity_manager->persist($device_type);
        
            $device->setDeviceHostName($info[1]);
            $device->setDeviceSerialNumber($info[5]);
            $timestamp = strtotime($info[4]);
            $device->setExpirationDateDevice(date("Y-m-d", $timestamp));
            $this->_entity_manager->persist($device);
            
            $device->setDeviceType($device_type);
            
            // write changes to database
            $this->_entity_manager->flush();
}



This code tries to insert the same value, and fails. How can I tell 
Doctrine to just use the existing primary key since the data for the device 
type already exists?

Here is the schema code (with the non-relevant stuff removed) for the 
device and device type tables:



<?php

namespace Enterprise\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * device
 *
 * @ORM\Table(name="device")
 * @ORM\Entity
 */
class device
{
    /**
     * @var integer
     *
     * @ORM\Column(name="device_id", type="smallint", precision=0, scale=0, 
nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $device_id;

    /**
     * @var \Enterprise\Entity\deviceType
     *
     * @ORM\ManyToOne(targetEntity="Enterprise\Entity\deviceType")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="device_type_id", 
referencedColumnName="device_type_id", unique=true, nullable=true)
     * })
     */
    private $device_type;



/**
     * Set device_type_name
     *
     * @param string $deviceTypeName
     * @return deviceType
     */
    public function setDeviceTypeName($deviceTypeName)
    {
        $this->device_type_name = $deviceTypeName;

        return $this;
    }











<?php

namespace Enterprise\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * deviceType
 *
 * @ORM\Table(name="device_type")
 * @ORM\Entity
 */
class deviceType
{
    /**
     * @var integer
     *
     * @ORM\Column(name="device_type_id", type="smallint", precision=0, 
scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $device_type_id;

    /**
     * @var string
     *
     * @ORM\Column(name="device_type_name", type="string", length=50, 
precision=0, scale=0, nullable=false, unique=false)
     */
    private $device_type_name;


    /**
     * Get device_type_id
     *
     * @return integer 
     */
    public function getDeviceTypeId()
    {
        return $this->device_type_id;
    }

    /**
     * Set device_type_name
     *
     * @param string $deviceTypeName
     * @return deviceType
     */
    public function setDeviceTypeName($deviceTypeName)
    {
        $this->device_type_name = $deviceTypeName;

        return $this;
    }

    /**
     * Get device_type_name
     *
     * @return string 
     */
    public function getDeviceTypeName()
    {
        return $this->device_type_name;
    }
}




Again, I want to write a program to take a flat file of values and insert 
them into the database, and basically create a normalized databases, so 
only 2 values will exist in the device type table, and then the primary 
keys from this table will be foreign keys  in the device table.

I can get it to insert one record successfully, but then the code fails to 
insert the second record but a device type already exists in the device 
type table and it tries to put it in again.


I did try the following code:

if ($record = $type->findOneBy(array('device_type_name' => $info[3]))) {
               $device_type = $record;
}

to set the record to the already existing record, but I still got the error.

Any help??

Thanks!





-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to