Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony DataFixtures设置默认值_Php_Symfony_Datetime_Sql Update - Fatal编程技术网

Php Symfony DataFixtures设置默认值

Php Symfony DataFixtures设置默认值,php,symfony,datetime,sql-update,Php,Symfony,Datetime,Sql Update,我想在我的网站中设置一个初始管理员用户。当我将其加载到数据库时,由于updateAt字段,它无法上载。当我将setUpdatedAt留空时,出现以下错误: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'updatedAt' ca nnot be null 我在Fixtures文件中设置的任何值都失败,我得到以下错误:我在本例中尝试输入实际日期 [Symfony\Component\Debug\Exception\

我想在我的网站中设置一个初始管理员用户。当我将其加载到数据库时,由于updateAt字段,它无法上载。当我将setUpdatedAt留空时,出现以下错误:

 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'updatedAt' ca
 nnot be null
我在Fixtures文件中设置的任何值都失败,我得到以下错误:我在本例中尝试输入实际日期

[Symfony\Component\Debug\Exception\FatalErrorException]
Parse Error: syntax error, unexpected '"2014-12-26 21:01:40"' (T_CONSTANT_E
NCAPSED_STRING), expecting identifier (T_STRING)
以下是数据文件:

<?php

namespace Usuarios\AdminBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Usuarios\UsersBundle\Entity\User;

class LoadUserData implements FixtureInterface
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
    {
    $userAdmin = new User();
    $userAdmin->setUsername('Admin');
    $userAdmin->setPlainPassword('1234');
    $userAdmin->setEmail('admin@web.com');
    $userAdmin->setRoles(array('ROLE_ADMIN'));
    $userAdmin->setEnabled('1');
    $userAdmin->setUpdatedAt(\"2014-12-26 21:01:40");

    $manager->persist($userAdmin);
    $manager->flush();
    }
}
我试图在updatedAt中以多种不同的类型输入数据,但我无法获得正确的数据类型,也无法获得将其保留为空的代码

以下是UpdatedAt的实体代码:

<?php

namespace Usuarios\UsersBundle\Entity;

use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * User
 *
 * @ORM\Table(name="0_00_users")
 * @ORM\Entity
 */
class User extends BaseUser

/**
 * @var datetime $updatedAt
 *
 * @Gedmo\Timestampable(on="update")
 * @ORM\Column(name="updatedAt", type="datetime", nullable=true)
 */
protected $updatedAt;

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 * @return User
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime 
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

如何解决此问题?

您的日期必须是有效的\DateTime,而不是字符串。试试这个:

$userAdmin->setUpdatedAt(new \DateTime("2014-12-26 21:01:40"));

您的日期必须是有效的\DateTime,而不是字符串。试试这个:

$userAdmin->setUpdatedAt(new \DateTime("2014-12-26 21:01:40"));
您缺少$updatedAt值

这应该是做这件事的方法。删除不需要的逻辑

/**
 * //...
 * @ORM\HasLifecycleCallbacks
 */
class User extends BaseUser

    // ...

    /**
     * Tell doctrine that before we persist or update we call the updatedTimestamps() function.
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updatedTimestamps()
    {
        if ($this->getCreatedAt() == null) {
            $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
        } else {
            $this->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s')));
        }
    }
    // ...
}
您缺少$updatedAt值

这应该是做这件事的方法。删除不需要的逻辑

/**
 * //...
 * @ORM\HasLifecycleCallbacks
 */
class User extends BaseUser

    // ...

    /**
     * Tell doctrine that before we persist or update we call the updatedTimestamps() function.
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updatedTimestamps()
    {
        if ($this->getCreatedAt() == null) {
            $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
        } else {
            $this->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s')));
        }
    }
    // ...
}

非常感谢。我错过了“新”标签。谢谢!我丢失了“新”标签。