Symfony2,Doctrine,fixtures,数组到字符串的转换

Posted

技术标签:

【中文标题】Symfony2,Doctrine,fixtures,数组到字符串的转换【英文标题】:Symfony2, Doctrine, fixtures, Array to string conversion 【发布时间】:2015-12-18 12:09:08 【问题描述】:

我已经使用我的数据库一个月了。突然我开始得到错误:

[Symfony\Component\Debug\Exception\ContextErrorException]
 Notice: Array to string conversion 

,加载夹具时。最后,我删除了所有数据库,重新运行迁移,只留下了 1 个夹具文件(因此我评论了我们的 OrderedFIxturesInterface,当然我清除了缓存,但错误仍然存​​在)。 C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj2_27\src\MeetingBundle\DataFixtures\ORM\LoadUsers01.php

<?php

namespace MeetingBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use MeetingBundle\Entity\User;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; 

class LoadUsers01 extends AbstractFixture 
#implements OrderedFixtureInterface

    public function load(ObjectManager $manager)
    
        for ($i = 1; $i <= 10; $i++) 
            $fuser = new User();
            $fuser->setUsername('name'.$i);
            $fuser->setEmail('g.statkute'.$i.'@gmail.com');
            $fuser->setIsActive(True); // i also used 1
            $fuser->setPassword('pswd'.$i);
            $manager->persist($fuser);
           # $this->addReference('fuser:name'.$i, $fuser);
        

        $manager->flush();
    

        public function getOrder()
    
        return 1;
    

C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj2_27\src\MeetingBundle\Entity\User.php

..
/**
 * User
 *
 * @ORM\Table(name="tuser")
 * @ORM\Entity()
 */
class User implements AdvancedUserInterface, \Serializable
..
    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=255, unique=true)
     */
    private $username;

    /**
     * @ORM\Column(name="email", type="string", nullable=true, unique=true)
     *
     * @var string
     */
    private $email;

    /**
     * @var bool
     *
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=255, unique=true)
     */
    private $password;

..

 /**
     * Set username
     *
     * @param string $username
     *
     * @return User
     */
    public function setUsername($username)
    
        $this->username = $username;

        return $this;
    
   /**
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    
        $this->email = $email;

        return $this;
    

    /**
     * @param boolean $isActive
     */
    public function setIsActive($isActive)
    
        $this->isActive = $isActive;
    

    /**
     * Set password
     *
     * @param string $password
     *
     * @return User
     */
    public function setPassword($password)
    
        $this->password = $password;

        return $this;
    

表包含 varchar 字段,但布尔 IsActive 的 tinyint 除外。只有一个表有名称用户。

--
-- Table structure for table `tuser`
--

CREATE TABLE IF NOT EXISTS `tuser` (
  `id` int(11) NOT NULL,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `phone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `roles` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `gravatar` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `webpages` longtext COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:json_array)',
  `createdAtInt` decimal(10,0) DEFAULT NULL,
  `loggedInInt` decimal(10,0) DEFAULT NULL,
  `loggedOutInt` decimal(10,0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tuser`
--
ALTER TABLE `tuser`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `UNIQ_66A7B847F85E0677` (`username`);

详细错误:

c:\Bitnami\wampstack-5.5.30-0\sym_prog\proj3_27>php app/console doctrine:fixture
s:load -vv
[2015-12-18 05:45:10] event.DEBUG: Notified event "console.command" to listener
"Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".

[2015-12-18 05:45:10] event.DEBUG: Notified event "console.command" to listener
"Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".
[2015-12-18 05:45:10] event.DEBUG: Notified event "console.command" to listener
"Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".
Careful, database will be purged. Do you want to continue y/N ?y
  > purging database
  > loading [1] MeetingBundle\DataFixtures\ORM\LoadUsers01



  [Symfony\Component\Debug\Exception\ContextErrorException]
  Notice: Array to string conversion



Exception trace:
 () at C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj3_27\vendor\doctrine\dbal\lib\
Doctrine\DBAL\Driver\PDOStatement.php:67
 Symfony\Component\Debug\ErrorHandler->handleError() at n/a:n/a
 PDOStatement->bindValue() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj3_27\ve
ndor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:67
 Doctrine\DBAL\Driver\PDOStatement->bindValue() at C:\Bitnami\wampstack-5.5.30-0
\sym_prog\proj3_27\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php:120
 Doctrine\DBAL\Statement->bindValue() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\
proj3_27\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersi
ster.php:277
 Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts() at C:\Bit
nami\wampstack-5.5.30-0\sym_prog\proj3_27\vendor\doctrine\orm\lib\Doctrine\ORM\U
nitOfWork.php:1018
 Doctrine\ORM\UnitOfWork->executeInserts() at C:\Bitnami\wampstack-5.5.30-0\sym_
prog\proj3_27\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php:378
 Doctrine\ORM\UnitOfWork->commit() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\pro
j3_27\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php:356
 Doctrine\ORM\EntityManager->flush() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\p
roj3_27\src\MeetingBundle\DataFixtures\ORM\LoadUsers01.php:32
 MeetingBundle\DataFixtures\ORM\LoadUsers01->load() at C:\Bitnami\wampstack-5.5.
30-0\sym_prog\proj3_27\vendor\doctrine\data-fixtures\lib\Doctrine\Common\DataFix
tures\Executor\AbstractExecutor.php:121
 Doctrine\Common\DataFixtures\Executor\AbstractExecutor->load() at C:\Bitnami\wa
mpstack-5.5.30-0\sym_prog\proj3_27\vendor\doctrine\data-fixtures\lib\Doctrine\Co
mmon\DataFixtures\Executor\ORMExecutor.php:83
 Doctrine\Common\DataFixtures\Executor\ORMExecutor->Doctrine\Common\DataFixtures
\Executor\closure() at n/a:n/a
 call_user_func() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj3_27\vendor\doct
rine\orm\lib\Doctrine\ORM\EntityManager.php:233
 Doctrine\ORM\EntityManager->transactional() at C:\Bitnami\wampstack-5.5.30-0\sy
m_prog\proj3_27\vendor\doctrine\data-fixtures\lib\Doctrine\Common\DataFixtures\E
xecutor\ORMExecutor.php:85
 Doctrine\Common\DataFixtures\Executor\ORMExecutor->execute() at C:\Bitnami\wamp
stack-5.5.30-0\sym_prog\proj3_27\vendor\doctrine\doctrine-fixtures-bundle\Comman
d\LoadDataFixturesDoctrineCommand.php:118
 Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand->execute
() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj3_27\vendor\symfony\symfony\src
\Symfony\Component\Console\Command\Command.php:256
 Symfony\Component\Console\Command\Command->run() at C:\Bitnami\wampstack-5.5.30
-0\sym_prog\proj3_27\vendor\symfony\symfony\src\Symfony\Component\Console\Applic
ation.php:846
 Symfony\Component\Console\Application->doRunCommand() at C:\Bitnami\wampstack-5
.5.30-0\sym_prog\proj3_27\vendor\symfony\symfony\src\Symfony\Component\Console\A
pplication.php:189
 Symfony\Component\Console\Application->doRun() at C:\Bitnami\wampstack-5.5.30-0
\sym_prog\proj3_27\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Con
sole\Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at C:\Bitnami\wamps
tack-5.5.30-0\sym_prog\proj3_27\vendor\symfony\symfony\src\Symfony\Component\Con
sole\Application.php:120
 Symfony\Component\Console\Application->run() at C:\Bitnami\wampstack-5.5.30-0\s
ym_prog\proj3_27\app\console:27

【问题讨论】:

在 25 个问题中,您还没有接受一个答案。奇怪。 我再次删除了所有数据库和所有迁移 - 也许原因是我有旧的迁移。从零开始重新生成所有内容,为表提供自定义名称。清除缓存。但比我尝试加载用户设备时,我得到了错误:[Symfony\Component\Debug\Exception\ContextErrorException] 注意:数组到字符串的转换 当我更新作曲家时。 最后我创建了一个新项目,新数据库,新包,复制实体,重新运行迁移。但比我尝试运行固定装置时,我得到了同样的错误:为表设置自定义名称。清除缓存。但比我尝试加载用户设备时,我得到了错误:[Symfony\Component\Debug\Exception\ContextErrorException] 注意:数组到字符串的转换 你可能错过了我评论的重点。到目前为止,您已经发布了几十个问题。其中没有一个得到您满意的答复。您为什么认为有人会花时间尝试回答这个问题?问题当然是网页数组不能为空,但告诉你真的没有意义。 【参考方案1】:

问题在于角色字段,我昨天晚上将其更改为字符串而不是数组。但我忘记删除初始化 $roles = array();

/**
 * @var string
 *
 * @ORM\Column(name="roles", type="string", nullable=true)
 */
private $roles; // it was $roles= array();

因此,如果您看到此错误,请在相关文件中搜索关键字“array”并检查分配变量是否应该等于数组等。

这是我第四次因为简单的错误重做所有项目。结论是,最好继续思考哪里可能是错误的。另一方面,当学说没有产生杂物时,用不同的 Symfony 版本重新安装项目会有所帮助。 私人$角色; // = 数组();

【讨论】:

以上是关于Symfony2,Doctrine,fixtures,数组到字符串的转换的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Doctrine Fixture 参考中获取实体?

Symfony 3.3 Doctrine Fixtures 加载跳过 SQL 视图

如何使用 Doctrine Fixture 加载测试数据库进行 PHP 单元测试?

Symfony2\Doctrine - 检索用户角色

Symfony2 和 Doctrine:一对多关系

如何在 Doctrine2 (Symfony2) 中按案例排序