Nevermind.. you advice was correct, but actually wasn't needed because I 
was doing a flush every loop iteration.

What I thought was occuring wasn't. When I looked up the record from the 
deviceType table and set it to the device table using the setter, Doctrine 
WAS NOT inserting a new record into the deviceType table.

The unique constraint violation came from the device table... somehow (and 
I believe it was through the Doctrine command line generate-entities 
command, the 3 foreign keys got set to unique in the main device table. I 
think tihs might have occurred because I originally set the relationship to 
be one to one, but then changed it (correctly) to many to one. However, 
Doctrine DID NOT pick this up and remove the UNIQUE constrains from the 3 
foreign keys. I mistakenly though the foriegn key constraing was on the 
deviceType table, but I was wrong.

Upon changing unique=false for those fields in the device table with the 
many to one relationship, the following code now works:


 $customer = 
$this->_entity_manager->getRepository('Enterprise\Entity\deviceCustomer');
 $model = 
$this->_entity_manager->getRepository('Enterprise\Entity\deviceModel');
 $type = 
$this->_entity_manager->getRepository('Enterprise\Entity\deviceType');

foreach ($device_info as $info) {
            $device_customer = new entityDeviceCustomer();
            $device = new entityDevice();
            $device_model = new entityDeviceModel();
            $device_type = new entityDeviceType();
            
            if ($record = $model->findOneBy(array('device_model_name' => 
$info[3]))) {
                $device_model = $record;
            } else {
                $device_model->setDeviceModelName($info[3]);
                
            }
            $this->_entity_manager->persist($device_model);
            
            if ($record = $type->findOneBy(array('device_type_name' => 
$device_type_name))) {
                $device_type = $record;
            } else {
                $device_type->setDeviceTypeName($device_type_name);
            }
            $this->_entity_manager->persist($device_type);
            
            if ($record = $customer->findOneBy(array('device_customer_name' 
=> $info[0]))) {
                $device_customer = $record;
            } else {
                $device_customer->setDeviceCustomerName($info[0]);
            }
            $this->_entity_manager->persist($device_customer);
            
            $device->setDeviceHostName($info[1]);
            $device->setDeviceSerialNumber($info[5]);
            $timestamp = strtotime($info[4]);
            $device->setExpirationDateDevice(date("Y-m-d", $timestamp));
            $this->_entity_manager->persist($device);
            
            // set our foreign relationships
            $device->setDeviceType($device_type);
            $device->setDeviceModel($device_model);
            $device->setDeviceCustomer($device_customer);
            
            // write changes to database
            $this->_entity_manager->flush();
            
            
        }

Thanks again for your help and feedback!

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