Symfony 2 ContextErrorException 在运行 Doctrine 迁移时,只应通过引用传递变量


Symfony 2 ContextErrorException Only variables should be passed by reference when running Doctrine migration

当我运行我的教义迁移时,我收到以下错误:

迁移20141217162533执行期间失败。错误运行时 注意:只有变量应该通过引用传递

[Symfony''Component''Debug''Exception''ContextErrorException] 运行时 注意:只有变量应该通过引用传递

$this->validateUsername线上。当我注释掉它时,它工作正常。

这似乎是一个非常奇怪的运行时错误,我不明白为什么会出现这种情况。

public function up(Schema $schema)
{
    foreach ($this->old_users as $old_user) {
        if ($this->validateUsername($old_user['email']) === false or $this->checkDNSRecord($old_user['email']) === false) {
            $this->addSql("INSERT INTO User (email, joined_on, unsubscribed) VALUES ('" . $old_user['email'] . "', '" . $old_user['joined_on'] . "', 0)");
        }
    }
}
/**
 * Validate the username/email address based on the structure (e.g. username@domain.com)
 *
 * @param $email
 *
 * @return bool
 */
public function validateUsername($email)
{
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // email is NOT valid
        return false;
    } else {
        // email is valid
        return true;
    }
}
/**
 * Validate the MX line of the DNS record for the host of the email address (e.g. @aol.co.uk)
 *
 * @param $email
 *
 * @return bool
 */
public function checkDNSRecord($email)
{
    if (checkdnsrr(array_pop(explode('@', $email)), 'MX')) {
        return true;
    } else {
        return false;
    }
}

问题是像 array_pop 这样的函数通过引用获取数组,因为它会更改数组的内容(从数组中删除最后一个元素(。现在,您将explode的结果直接传递到array_pop array_pop(explode('@', $email))因此,php 无法通过引用传递该结果,因为它不是变量。解决方案非常简单,只需将结果保留explode一个临时变量:

<?php
/* ... */
$mailParts = explode('@', $email);
checkdnsrr(array_pop($mailParts), 'MX');
/* ... */

这更好吗?

public function up(Schema $schema)
{
    foreach ($this->old_users as $old_user) {
        $old_email = $old_user['email'];
        if ($this->validateUsername($old_email) === false or $this->checkDNSRecord($old_user['email']) === false) {
            $this->addSql("INSERT INTO User (email, joined_on, unsubscribed) VALUES ('" . $old_user['email'] . "', '" . $old_user['joined_on'] . "', 0)");
        }
    }
}