Symfony2,Doctrine,fixture,Array to string conversion

时间:2015-12-18 12:09:09

标签: symfony doctrine-orm fixtures

我正在使用我的数据库一个月。突然间我开始收到错误:

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

,装载灯具时。最后,我删除了所有数据库,重新运行迁移,只剩下1个fixtures文件(因此我评论了我们的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 \实体\ 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字段,除了tinyint for boolean IsActive。只有一个包含名称用户的表格。

--
-- 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`);

VERBOSE错误:

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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