user2192227
user2192227

Reputation:

Error with flush, try catch and object

It is throwing an error

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'complete' cannot be null

How do I "tell" the Entity Manager not to consider the fruit object since it is throwing an error?

I have an object like:

class Fruit {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;
    /** @Column(type="string", length=10, nullable=true) */
    private $name;

    /** @Column(type="boolean") */
    private $complete;

    /* Omitted to make it simple */
}

And a method like:

public function uploadAction(){
    try{
       /* Omitted to make it simple */
       while ($data = fgetcsv($fic, 1024, $delimiter)) {
           try {
               $fruit = $fruitManager->createFruit($data);
               $outcome_fruit = $fruit->id;
           } catch (\Exception $e) {
               $outcome_details[] = $e->getMessage();
           }
       }

       fclose($fic);
       @unlink($file_path);

       $csv_data = $this->view->io->build_csv_file($outcome_details);
       $csv_import->output_csv = base64_encode(gzencode($csv_data));
       $this->em->persist($csv_import);

       // error here
       $this->em->flush();
       //

    } catch (\Exception $e) {
       print $e->getMessage();
       exit;
    }
}

My fruitManager

class FruitManager{
   public function createFruit($name){
      $fruit = new \Entities\Fruit;
      $fruit->name = $name;

      throw new Exception("Exception.");

      $fruit->complete = 1;
      $this->em->persist($fruit);
      $this->em->flush();
   }
} 

Upvotes: 0

Views: 1139

Answers (2)

Ilya Kovalyov
Ilya Kovalyov

Reputation: 484

You have few options:

  • update your entity allowing null values using ORM to allow null values for this field ie /** @Column(type="boolean", nullable=true) */
  • update your entity with default value using ORM ie /** @ORM\Column(type="boolean", nullable=false, options={"default" : false}) */
  • assign default value in the entity constructor
  • assign default value using private $complete = false; like Freelancer said

I'd prefer one of the first 2 options depending on logic needs. Thanks.

Upvotes: 2

Freelancer
Freelancer

Reputation: 4770

try by setting private $complete = false; or true as you want.

Upvotes: 0

Related Questions