【问题标题】:DataFixtures Symfony 3.3 SQLSTATE[23000]DataFixtures Symfony 3.3 SQLSTATE[23000]
【发布时间】:2018-09-11 14:55:54
【问题描述】:

嗨, 我想在 Symfony 3.3 中从 Datafixtures 生成内容

如果我从 csv 文件中使用 Load 方法,我会遇到 SQLSTATE[23000] 错误,如果我在方法中使用字符串数据,它会很好地工作,但是一一...他们不感兴趣

<?php


namespace PasswordManager\Bundle\PlatformBundle\DataFixtures\ORM;


use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use PasswordManager\Bundle\PlatformBundle\Entity\Password as Password;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use PasswordManager\Bundle\PlatformBundle\Controller\PasswordController;



class LoadPassword extends PasswordController implements FixtureInterface
{

    public function load(ObjectManager $manager){



        $csv = fopen(dirname(__FILE__).'/loadexistPass/file.csv', 'r');
        $i = 0;
        while (!feof($csv)) {
            $line = fgetcsv($csv);
            $existantPass[$i] = new Password();
            $existantPass[$i]->setSlug($line[0]);
            $existantPass[$i]->setShared(1);
            $existantPass[$i]->setTitle($line[0]);
            $existantPass[$i]->setUrl($line[0]);
            $existantPass[$i]->setLogin($line[1]);
            $existantPass[$i]->setPassword($line[2]);
            $existantPass[$i]->setContent('my str');
            $manager->persist($existantPass[$i]);




            $i = $i ++;
        }

        fclose($csv);

        $manager->flush();
    }



}

我的例外:

 [Doctrine\DBAL\Exception\NotNullConstraintViolationException]             
  An exception occurred while executing 'INSERT INTO password (slug, share  
  d, date, nb_applications, title, url, login, password, content, updated_  
  at, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["-5"  
  , 1, "2018-09-11 16:46:04", 0, null, null, null, null, "my str", null, n  
  ull]:                                                                     
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' can  
  not be null  

你能帮帮我吗?

列'title'不能为空但是当我转储内容frym文件时它会显示内容

【问题讨论】:

  • 可以转储行的内容吗?看起来 $line[0] 是空的。
  • Column 'title' can not be null 确保您为标题传递的内容具有值。
  • Column 'title' can not be null 并且您将 null 传递给该参数
  • 您好 Gérald,您的固定装置的目标是在开发环境中运行您的应用程序,对吧?为什么要从.csv 获取数据?我希望这个文件不包含真实凭据并且没有版本化;)否则让我向您宣传这是非常糟糕/危险的做法
  • 转储 $line[0] display "URL " "myurl.com" null

标签: php symfony doctrine-orm fopen fixtures


【解决方案1】:

消息:

Column 'title' can not be null 

$existantPass[$i]->setTitle($line[0]?? 'some-value');

【讨论】:

  • dump of $line[0] display "URL" "myurl.com" null dump of $line[1] display "URL" "Account" "myAccount" null
猜你喜欢
  • 2016-03-03
  • 1970-01-01
  • 2020-06-10
  • 2018-02-15
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
  • 2020-09-30
相关资源
最近更新 更多