Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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
Symfony 我如何为Prope中的多行发出一个Insert语句_Symfony_Propel - Fatal编程技术网

Symfony 我如何为Prope中的多行发出一个Insert语句

Symfony 我如何为Prope中的多行发出一个Insert语句,symfony,propel,Symfony,Propel,有没有办法在每个对象上发出一条INSERT语句而不是调用save()方法?我可以对PropelObjectCollection调用save()吗?您可以从类本身对PropelObjectCollection调用save,但它将发出多个insert语句来执行这项工作 如果它们都包装在一个事务中,那么通过发出一次插入而不是多次插入,性能不会有太大的提高,而spreep默认情况下会这样做。另外,考虑到spreep递归保存相关对象的方式,我怀疑尝试这样做会增加很多复杂性 <?php /** *

有没有办法在每个对象上发出一条INSERT语句而不是调用save()方法?我可以对PropelObjectCollection调用save()吗?

您可以从类本身对PropelObjectCollection调用save,但它将发出多个insert语句来执行这项工作

如果它们都包装在一个事务中,那么通过发出一次插入而不是多次插入,性能不会有太大的提高,而spreep默认情况下会这样做。另外,考虑到spreep递归保存相关对象的方式,我怀疑尝试这样做会增加很多复杂性

<?php

/**
 * This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

/**
 * Class for iterating over a list of Propel objects
 *
 * @author     Francois Zaninotto
 * @package    propel.runtime.collection
 */
class PropelObjectCollection extends PropelCollection
{
    /**
     * Save all the elements in the collection
     *
     * @param PropelPDO $con
     *
     * @throws PropelException
     */
    public function save($con = null)
    {
        if (!method_exists($this->getModel(), 'save')) {
            throw new PropelException('Cannot save objects on a read-only model');
        }
        if (null === $con) {
            $con = $this->getConnection(Propel::CONNECTION_WRITE);
        }
        $con->beginTransaction();
        try {
            /** @var $element BaseObject */
            foreach ($this as $element) {
                $element->save($con);
            }
            $con->commit();
        } catch (PropelException $e) {
            $con->rollback();
            throw $e;
        }
    }

将所有save()调用放在一个事务中就足够了。在我的代码中,我必须在MySQL数据库中插入270条记录

无交易记录的结果: 实数0m15.127s 用户0.300s 系统0m0.056s

交易结果: 实0.687s 用户0m0.244s 系统0m0.036s

这是一个巨大的差异