Php 刷新出错,请尝试捕获和对象
这是一个错误 SQLSTATE[23000]:完整性约束冲突:1048列“complete”不能为null < >我如何告诉实体管理器不考虑水果对象,因为它正在抛出一个错误? 我有一个物体像:Php 刷新出错,请尝试捕获和对象,php,symfony,doctrine,Php,Symfony,Doctrine,这是一个错误 SQLSTATE[23000]:完整性约束冲突:1048列“complete”不能为null < >我如何告诉实体管理器不考虑水果对象,因为它正在抛出一个错误? 我有一个物体像: class Fruit { /** * @Id @Column(type="integer") * @GeneratedValue(strategy="AUTO") */ private $id; /** @Column(type="string",
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 */
}
还有一种方法,比如:
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;
}
}
我的水果经理
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();
}
}
尝试设置
private$complete=false
或true(根据需要)。尝试设置private$complete=false代码>或true(根据需要)。您几乎没有选择:
- 使用ORM更新允许空值的实体以允许此字段为空值,即
/**@列(type=“boolean”,nullable=true)*/
- 使用ORM ie
/**@ORM\Column(type=“boolean”,nullable=false,options={“default”:false})用默认值更新实体)*/
- 在实体构造函数中指定默认值
- 使用
private$complete=false分配默认值就像自由职业者说的那样
根据逻辑需求,我更喜欢前两个选项中的一个。
谢谢。您几乎没有选择:
- 使用ORM更新允许空值的实体以允许此字段为空值,即
/**@列(type=“boolean”,nullable=true)*/
- 使用ORM ie
/**@ORM\Column(type=“boolean”,nullable=false,options={“default”:false})用默认值更新实体)*/
- 在实体构造函数中指定默认值
- 使用
private$complete=false分配默认值就像自由职业者说的那样
根据逻辑需求,我更喜欢前两个选项中的一个。
谢谢。但是,我的意思是,一旦水果抛出错误,我不想保存它。我如何“告诉”刷新我只想持久化$csv\u导入对象。它非常脏,但你可以尝试在刷新之前分离对象的果实:$em->detach($fruit);但我建议你找一个更好的逻辑,而不是这样做。我同意,但我出于好奇尝试了一下,它仍然会产生同样的错误。但是,我的意思是,我不想在水果抛出错误时保存它。我如何“告诉”刷新我只想持久化$csv\u导入对象。它非常脏,但你可以尝试在刷新之前分离对象的果实:$em->detach($fruit);但我建议你找一个更好的逻辑,而不是这样做。我同意,但我出于好奇尝试了一下,它仍然会产生同样的错误。但是,我不想在它抛出错误时保存它。当发生错误时,是否有其他方法丢弃对象?嗯。。您已经准备好了这段代码:如果发生异常,您将无法保存,并转到catch块。你有什么问题吗?但是,我不想在水果抛出错误时保存它。当发生错误时,是否有其他方法丢弃对象?嗯。。您已经准备好了这段代码:如果发生异常,您将无法保存,并转到catch块。你有什么问题吗?