草庐IT

php - 无法在 symfony2 控制台命令中保留对象

coder 2024-04-20 原文

我制作了一个简单的 symfony2 控制台脚本,用于将数据从旧模型转换为新模型。 这是它的样子:

class ConvertScreenshotsCommand extends Command 
{
[...]
protected function execute(InputInterface $input, OutputInterface $output)
{
    $em = $this->getContainer()->get('doctrine')->getManager();

    $output->writeln('<info>Conversion started on ' . date(DATE_RSS) . "</info>");

    $output->writeln('Getting all reviews...');
    $reviews = $em->getRepository('ACCommonBundle:Review')->findAll(); // Putting all Review entities into an array
    $output->writeln('<info>Got ' . count($reviews) . ' reviews.</info>');

    foreach ($reviews as $review) {
        $output->writeln("<info>Screenshots for " . $review->getTitle() . "</info>");
        if ($review->getLegacyScreenshots()) {
            foreach ($review->getLegacyScreenshots() as $filename) { // fn returns array of strings
                $output->writeln("Found " . $filename);
                $screenshot = new ReviewScreenshot();        // new object

                $screenshot->setReview($review);     // review is object
                $screenshot->setFilename($filename); // filename is string

                $em->persist($screenshot);
                $em->flush();                        // this is where it dies
                $output->writeln("Successfully added to the database.");
            }
        } else $output->writeln("No legacy screenshots found.");
    }
    $output->writeln('<info>Conversion ended on ' . date(DATE_RSS) . "</info>");
 }
 }

脚本在 $em->flush() 上中断,出现以下错误:

 [ErrorException]                                                                                                                                                       
 Warning: spl_object_hash() expects parameter 1 to be object, string given in      
 /[...]/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1324     

显然我做错了什么,但无法弄清楚它是什么。提前致谢!

** 更新**

查看实体映射:

class Review
{
[...]

/**
 * @ORM\OneToMany(targetEntity="ReviewScreenshot", mappedBy="review")
 */
protected $screenshots;

/**
 * Won't be stored in the DB
 * @deprecated
 */
private $legacyScreenshots;

/**
 * New method to get screenshots, currently calls old method for the sake of compatibility
 * @return array Screenshot paths
 */

public function getScreenshots()
{
//        return $this->getLegacyScreenshots();  // Old method
    return $this->screenshots;                   // New method
}


/**
 * Get Screenshot paths
 * @return array Screenshot paths
 * @deprecated
 */
public function getLegacyScreenshots()
{
    $dir=$this->getUploadRootDir();
    if (file_exists($dir)) {
        $fileList = scandir($dir);

    $this->screenshots = array();
    foreach ($fileList as $fileName)
    {
        preg_match("/(screenshot-\d+.*)/", $fileName, $matches);
        if ($matches)
            $this->screenshots[]=$matches[1];
    }
    return $this->screenshots;
    }
    else return null;
}

ReviewScreenshot 映射:

class ReviewScreenshot
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

/**
 * @ORM\ManyToOne(targetEntity="Review", inversedBy="screenshots")
 * @ORM\JoinColumn(name="review_id", referencedColumnName="id")
 */
protected $review;

/**
 * @var integer $priority
 *
 * @ORM\Column(name="priority", type="integer", nullable=true)
 */
protected $priority;

/**
 * @var string $description
 *
 * @ORM\Column(name="description", type="string", nullable=true)
 */
protected $description;

/**
 * @Assert\File(maxSize="2097152")
 */
public $screenshot_file;

protected $webPath;

工作单元.php

/**
 * Gets the state of an entity with regard to the current unit of work.
 *
 * @param object $entity
 * @param integer $assume The state to assume if the state is not yet known (not MANAGED or REMOVED).
 *                        This parameter can be set to improve performance of entity state detection
 *                        by potentially avoiding a database lookup if the distinction between NEW and DETACHED
 *                        is either known or does not matter for the caller of the method.
 * @return int The entity state.
 */
public function getEntityState($entity, $assume = null)
{
    $oid = spl_object_hash($entity); // <-- Line 1324

    if (isset($this->entityStates[$oid])) {
        return $this->entityStates[$oid];
    }

    if ($assume !== null) {
        return $assume;
    }

    // State can only be NEW or DETACHED, because MANAGED/REMOVED states are known.
    // Note that you can not remember the NEW or DETACHED state in _entityStates since
    // the UoW does not hold references to such objects and the object hash can be reused.
    // More generally because the state may "change" between NEW/DETACHED without the UoW being aware of it.
    $class = $this->em->getClassMetadata(get_class($entity));
    $id    = $class->getIdentifierValues($entity);

    if ( ! $id) {
        return self::STATE_NEW;
    }

    switch (true) {
        case ($class->isIdentifierNatural());
            // Check for a version field, if available, to avoid a db lookup.
            if ($class->isVersioned) {
                return ($class->getFieldValue($entity, $class->versionField))
                    ? self::STATE_DETACHED
                    : self::STATE_NEW;
            }

            // Last try before db lookup: check the identity map.
            if ($this->tryGetById($id, $class->rootEntityName)) {
                return self::STATE_DETACHED;
            }

            // db lookup
            if ($this->getEntityPersister($class->name)->exists($entity)) {
                return self::STATE_DETACHED;
            }

            return self::STATE_NEW;

        case ( ! $class->idGenerator->isPostInsertGenerator()):
            // if we have a pre insert generator we can't be sure that having an id
            // really means that the entity exists. We have to verify this through
            // the last resort: a db lookup

            // Last try before db lookup: check the identity map.
            if ($this->tryGetById($id, $class->rootEntityName)) {
                return self::STATE_DETACHED;
            }

            // db lookup
            if ($this->getEntityPersister($class->name)->exists($entity)) {
                return self::STATE_DETACHED;
            }

            return self::STATE_NEW;

        default:
            return self::STATE_DETACHED;
    }
}

最佳答案

我认为问题在于Review::$screenshots:

您将其映射为 OneToMany 关联,因此该值应该是 ReviewScreenshot 实体的集合。但是 Review::getLegacyScreenshots() 方法会将其更改为字符串数组。

您可能正在使用更改跟踪策略 DEFERRED_IMPLICIT(默认)。因此,当属性 Review::$screenshots 发生变化时,Doctrine 将尝试保留该变化,在它期望实体的地方遇到字符串,因此抛出异常。

关于php - 无法在 symfony2 控制台命令中保留对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14610213/

有关php - 无法在 symfony2 控制台命令中保留对象的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  4. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  8. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  9. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  10. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

随机推荐